@@ -264,7 +264,7 @@ void _mi_thread_data_collect(void) {
264
264
}
265
265
266
266
// Initialize the thread local default heap, called from `mi_thread_init`
267
- static bool _mi_heap_init (void ) {
267
+ static bool _mi_thread_heap_init (void ) {
268
268
if (mi_heap_is_initialized (mi_prim_get_default_heap ())) return true;
269
269
if (_mi_is_main_thread ()) {
270
270
// mi_assert_internal(_mi_heap_main.thread_id != 0); // can happen on freeBSD where alloc is called before any initialization
@@ -280,25 +280,25 @@ static bool _mi_heap_init(void) {
280
280
281
281
mi_tld_t * tld = & td -> tld ;
282
282
mi_heap_t * heap = & td -> heap ;
283
- _mi_memcpy_aligned (heap , & _mi_heap_empty , sizeof (* heap ));
284
- heap -> thread_id = _mi_thread_id ();
285
- _mi_random_init (& heap -> random );
286
- heap -> cookie = _mi_heap_random_next (heap ) | 1 ;
287
- heap -> keys [0 ] = _mi_heap_random_next (heap );
288
- heap -> keys [1 ] = _mi_heap_random_next (heap );
289
- heap -> tld = tld ;
290
- tld -> heap_backing = heap ;
291
- tld -> heaps = heap ;
292
- tld -> segments .stats = & tld -> stats ;
293
- tld -> segments .os = & tld -> os ;
294
- tld -> os .stats = & tld -> stats ;
295
- _mi_heap_set_default_direct (heap );
283
+ _mi_tld_init (tld , heap ); // must be before `_mi_heap_init`
284
+ _mi_heap_init (heap , tld , _mi_arena_id_none ());
285
+ _mi_heap_set_default_direct (heap );
296
286
}
297
287
return false;
298
288
}
299
289
290
+ // initialize thread local data
291
+ void _mi_tld_init (mi_tld_t * tld , mi_heap_t * bheap ) {
292
+ _mi_memzero_aligned (tld ,sizeof (mi_tld_t ));
293
+ tld -> heap_backing = bheap ;
294
+ tld -> heaps = bheap ;
295
+ tld -> segments .stats = & tld -> stats ;
296
+ tld -> segments .os = & tld -> os ;
297
+ tld -> os .stats = & tld -> stats ;
298
+ }
299
+
300
300
// Free the thread local default heap (called from `mi_thread_done`)
301
- static bool _mi_heap_done (mi_heap_t * heap ) {
301
+ static bool _mi_thread_heap_done (mi_heap_t * heap ) {
302
302
if (!mi_heap_is_initialized (heap )) return true;
303
303
304
304
// reset default heap
@@ -392,7 +392,7 @@ void mi_thread_init(void) mi_attr_noexcept
392
392
// initialize the thread local default heap
393
393
// (this will call `_mi_heap_set_default_direct` and thus set the
394
394
// fiber/pthread key to a non-zero value, ensuring `_mi_thread_done` is called)
395
- if (_mi_heap_init ()) return ; // returns true if already initialized
395
+ if (_mi_thread_heap_init ()) return ; // returns true if already initialized
396
396
397
397
_mi_stat_increase (& _mi_stats_main .threads , 1 );
398
398
mi_atomic_increment_relaxed (& thread_count );
@@ -424,7 +424,7 @@ void _mi_thread_done(mi_heap_t* heap)
424
424
if (heap -> thread_id != _mi_thread_id ()) return ;
425
425
426
426
// abandon the thread local heap
427
- if (_mi_heap_done (heap )) return ; // returns true if already ran
427
+ if (_mi_thread_heap_done (heap )) return ; // returns true if already ran
428
428
}
429
429
430
430
void _mi_heap_set_default_direct (mi_heap_t * heap ) {
0 commit comments