Skip to content

Commit 5d9cb38

Browse files
committed
remove static main theap and tld
1 parent d3eb597 commit 5d9cb38

7 files changed

Lines changed: 123 additions & 149 deletions

File tree

include/mimalloc/internal.h

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ void mi_cdecl _mi_auto_process_done(void) mi_attr_noexcept;
168168
bool _mi_is_redirected(void);
169169
bool _mi_allocator_init(const char** message);
170170
void _mi_allocator_done(void);
171-
bool _mi_is_main_thread(void);
171+
// bool _mi_is_main_thread(void);
172172
bool _mi_is_process_heap_main(const mi_heap_t* heap);
173173
bool _mi_preloading(void); // true while the C runtime is not initialized yet
174174
void _mi_thread_done(mi_theap_t* theap);
@@ -255,9 +255,9 @@ void _mi_arenas_page_abandon(mi_page_t* page, mi_theap_t* current_theap
255255
void _mi_arenas_page_unabandon(mi_page_t* page, mi_theap_t* current_theapx /* can be NULL */);
256256
bool _mi_arenas_page_try_reabandon_to_mapped(mi_page_t* page);
257257

258-
// arena-meta.c
258+
// init.c
259259
void* _mi_meta_zalloc( mi_subproc_t* subproc, size_t size, mi_memid_t* memid );
260-
void _mi_meta_free(mi_subproc_t* subproc, void* p, size_t size, mi_memid_t memid);
260+
void _mi_meta_free(mi_subproc_t* subproc, void* p, mi_memid_t memid);
261261
bool _mi_meta_is_meta_page(mi_subproc_t* subproc, void* p);
262262

263263
// "page-map.c"
@@ -1217,17 +1217,40 @@ static inline mi_memid_t _mi_memid_create_os(void* base, size_t size, bool commi
12171217
return memid;
12181218
}
12191219

1220-
static inline mi_memid_t _mi_memid_create_meta(mi_meta_page_t* mpage, size_t block_idx, size_t block_count) {
1221-
mi_memid_t memid = _mi_memid_create(MI_MEM_META);
1222-
memid.mem.meta.meta_page = mpage;
1223-
memid.mem.meta.block_index = (uint32_t)block_idx;
1224-
memid.mem.meta.block_count = (uint32_t)block_count;
1220+
static inline mi_memid_t _mi_memid_create_static(void* p, size_t size) {
1221+
mi_memid_t memid = _mi_memid_create(MI_MEM_STATIC);
1222+
memid.mem.malloc.base = p;
1223+
memid.mem.malloc.size = size;
1224+
memid.initially_committed = true;
1225+
memid.is_pinned = true;
1226+
return memid;
1227+
}
1228+
1229+
static inline mi_memid_t _mi_memid_create_malloc(void* p, size_t size, bool iszero) {
1230+
mi_memid_t memid = _mi_memid_create(MI_MEM_MALLOC);
1231+
memid.mem.malloc.base = p;
1232+
memid.mem.malloc.size = size;
12251233
memid.initially_committed = true;
1226-
memid.initially_zero = true;
1234+
memid.initially_zero = iszero;
12271235
memid.is_pinned = true;
12281236
return memid;
12291237
}
12301238

1239+
static inline size_t _mi_memid_size(mi_memid_t memid) {
1240+
if (mi_memid_is_os(memid)) {
1241+
return memid.mem.os.size;
1242+
}
1243+
else if (memid.memkind == MI_MEM_ARENA) {
1244+
return mi_size_of_slices(memid.mem.arena.slice_count);
1245+
}
1246+
else if (memid.memkind == MI_MEM_MALLOC) {
1247+
return memid.mem.malloc.size;
1248+
}
1249+
else {
1250+
mi_assert_internal(mi_memid_needs_no_free(memid));
1251+
return 0;
1252+
}
1253+
}
12311254

12321255
// -------------------------------------------------------------------
12331256
// Fast "random" shuffle

include/mimalloc/types.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ typedef enum mi_memkind_e {
251251
MI_MEM_NONE, // not allocated (or static)
252252
MI_MEM_EXTERNAL, // not owned by mimalloc but provided externally (via `mi_manage_os_memory` for example)
253253
MI_MEM_STATIC, // allocated in a static area and should not be freed (the initial main theap data for example (`init.c`))
254-
MI_MEM_META, // allocated with the meta data allocator (`arena-meta.c`)
255254
MI_MEM_OS, // allocated from the OS
256255
MI_MEM_OS_HUGE, // allocated as huge OS pages (usually 1GiB, pinned to physical memory)
257256
MI_MEM_OS_REMAP, // allocated in a remapable area (i.e. using `mremap`)
@@ -281,17 +280,16 @@ typedef struct mi_memid_arena_info {
281280
uint32_t slice_count; // allocated slices
282281
} mi_memid_arena_info_t;
283282

284-
typedef struct mi_memid_meta_info {
285-
mi_meta_page_t* meta_page; // meta-page that contains the block
286-
uint32_t block_index; // block index in the meta-data page
287-
uint32_t block_count; // allocated blocks
288-
} mi_memid_meta_info_t;
283+
typedef struct mi_memid_malloc_info {
284+
void* base; // returned pointer
285+
size_t size; // allocated size
286+
} mi_memid_malloc_info_t;
289287

290288
typedef struct mi_memid_s {
291289
union {
292-
mi_memid_os_info_t os; // only used for MI_MEM_OS
293-
mi_memid_arena_info_t arena; // only used for MI_MEM_ARENA
294-
mi_memid_meta_info_t meta; // only used for MI_MEM_META
290+
mi_memid_os_info_t os; // only used for MI_MEM_OS(_HUGE/_REMAP)
291+
mi_memid_arena_info_t arena; // only used for MI_MEM_ARENA
292+
mi_memid_malloc_info_t malloc; // only used for MI_MEM_MALLOC
295293
} mem;
296294
mi_memkind_t memkind;
297295
bool is_pinned; // `true` if we cannot decommit/reset/protect in this memory (e.g. when allocated using large (2Mib) or huge (1GiB) OS pages)

src/arena.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,8 +1352,8 @@ void _mi_arenas_free(mi_subproc_t* subproc, void* p, size_t size, mi_memid_t mem
13521352
return;
13531353
};
13541354
}
1355-
else if (memid.memkind == MI_MEM_META) {
1356-
_mi_meta_free(subproc, p, size, memid);
1355+
else if (memid.memkind == MI_MEM_MALLOC) {
1356+
_mi_free_subproc_safe(p);
13571357
}
13581358
else {
13591359
// arena was none, external, or static; nothing to do

0 commit comments

Comments
 (0)