@@ -628,18 +628,16 @@ bool _mi_prim_random_buf(void* buf, size_t buf_len) {
628
628
//----------------------------------------------------------------
629
629
630
630
#if MI_WIN_USE_FIXED_TLS == 1
631
- mi_decl_cache_align size_t _mi_win_tls_offset = sizeof ( void * ) ; // use 2nd slot by default
631
+ mi_decl_cache_align size_t _mi_win_tls_offset = 0 ;
632
632
#endif
633
633
634
- static void NTAPI mi_win_main (PVOID module , DWORD reason , LPVOID reserved ) {
635
- MI_UNUSED (reserved );
636
- MI_UNUSED (module );
634
+ static void mi_win_tls_init (DWORD reason ) {
637
635
#if MI_HAS_TLS_SLOT >= 2 // we must initialize the TLS slot before any allocation
638
636
#if MI_WIN_USE_FIXED_TLS == 1
639
- if (reason == DLL_PROCESS_ATTACH ) {
640
- const DWORD tls_slot = TlsAlloc ();
641
- if (tls_slot == TLS_OUT_OF_INDEXES ) {
642
- _mi_error_message (EFAULT , "unable to allocate the a TLS slot (rebuild without MI_WIN_USE_FIXED_TLS?)\n" );
637
+ if (reason == DLL_PROCESS_ATTACH && _mi_win_tls_offset == 0 ) {
638
+ const DWORD tls_slot = TlsAlloc (); // usually returns slot 1
639
+ if (tls_slot == TLS_OUT_OF_INDEXES ) {
640
+ _mi_error_message (EFAULT , "unable to allocate the a TLS slot (rebuild without MI_WIN_USE_FIXED_TLS?)\n" );
643
641
}
644
642
_mi_win_tls_offset = (size_t )tls_slot * sizeof (void * );
645
643
}
@@ -653,7 +651,15 @@ static void NTAPI mi_win_main(PVOID module, DWORD reason, LPVOID reserved) {
653
651
mi_assert_internal (p == (void * )& _mi_heap_empty );
654
652
#endif
655
653
}
654
+ #else
655
+ MI_UNUSED (reason );
656
656
#endif
657
+ }
658
+
659
+ static void NTAPI mi_win_main (PVOID module , DWORD reason , LPVOID reserved ) {
660
+ MI_UNUSED (reserved );
661
+ MI_UNUSED (module );
662
+ mi_win_tls_init (reason );
657
663
if (reason == DLL_PROCESS_ATTACH ) {
658
664
_mi_process_load ();
659
665
}
@@ -815,11 +821,7 @@ static void NTAPI mi_win_main(PVOID module, DWORD reason, LPVOID reserved) {
815
821
#endif
816
822
mi_decl_export void _mi_redirect_entry (DWORD reason ) {
817
823
// called on redirection; careful as this may be called before DllMain
818
- #if MI_HAS_TLS_SLOT >= 2 // we must initialize the TLS slot before any allocation
819
- if ((reason == DLL_PROCESS_ATTACH || reason == DLL_THREAD_ATTACH ) && mi_prim_get_default_heap () == NULL ) {
820
- _mi_heap_set_default_direct ((mi_heap_t * )& _mi_heap_empty );
821
- }
822
- #endif
824
+ mi_win_tls_init (reason );
823
825
if (reason == DLL_PROCESS_ATTACH ) {
824
826
mi_redirected = true;
825
827
}
0 commit comments