MCPcopy Create free account
hub / github.com/CppCXY/EmmyLuaCodeStyle / _mi_heap_init

Function _mi_heap_init

3rd/mimalloc-2.0.9/src/init.c:261–294  ·  view source on GitHub ↗

Initialize the thread local default heap, called from `mi_thread_init`

Source from the content-addressed store, hash-verified

259
260// Initialize the thread local default heap, called from `mi_thread_init`
261static bool _mi_heap_init(void) {
262 if (mi_heap_is_initialized(mi_get_default_heap())) return true;
263 if (_mi_is_main_thread()) {
264 // mi_assert_internal(_mi_heap_main.thread_id != 0); // can happen on freeBSD where alloc is called before any initialization
265 // the main heap is statically allocated
266 mi_heap_main_init();
267 _mi_heap_set_default_direct(&_mi_heap_main);
268 //mi_assert_internal(_mi_heap_default->tld->heap_backing == mi_get_default_heap());
269 }
270 else {
271 // use `_mi_os_alloc` to allocate directly from the OS
272 mi_thread_data_t* td = mi_thread_data_alloc();
273 if (td == NULL) return false;
274
275 // OS allocated so already zero initialized
276 mi_tld_t* tld = &td->tld;
277 mi_heap_t* heap = &td->heap;
278 _mi_memcpy_aligned(tld, &tld_empty, sizeof(*tld));
279 _mi_memcpy_aligned(heap, &_mi_heap_empty, sizeof(*heap));
280 heap->thread_id = _mi_thread_id();
281 _mi_random_init(&heap->random);
282 heap->cookie = _mi_heap_random_next(heap) | 1;
283 heap->keys[0] = _mi_heap_random_next(heap);
284 heap->keys[1] = _mi_heap_random_next(heap);
285 heap->tld = tld;
286 tld->heap_backing = heap;
287 tld->heaps = heap;
288 tld->segments.stats = &tld->stats;
289 tld->segments.os = &tld->os;
290 tld->os.stats = &tld->stats;
291 _mi_heap_set_default_direct(heap);
292 }
293 return false;
294}
295
296// Free the thread local default heap (called from `mi_thread_done`)
297static bool _mi_heap_done(mi_heap_t* heap) {

Callers 1

mi_thread_initFunction · 0.85

Calls 10

mi_heap_is_initializedFunction · 0.85
mi_get_default_heapFunction · 0.85
_mi_is_main_threadFunction · 0.85
mi_heap_main_initFunction · 0.85
mi_thread_data_allocFunction · 0.85
_mi_memcpy_alignedFunction · 0.85
_mi_thread_idFunction · 0.85
_mi_random_initFunction · 0.85
_mi_heap_random_nextFunction · 0.85

Tested by

no test coverage detected