Initialize the thread local default heap, called from `mi_thread_init`
| 259 | |
| 260 | // Initialize the thread local default heap, called from `mi_thread_init` |
| 261 | static 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`) |
| 297 | static bool _mi_heap_done(mi_heap_t* heap) { |
no test coverage detected