@@ -631,29 +631,32 @@ bool _mi_prim_random_buf(void* buf, size_t buf_len) {
631631mi_decl_cache_align size_t _mi_win_tls_offset = 0 ;
632632#endif
633633
634+ //static void mi_debug_out(const char* s) {
635+ // HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
636+ // WriteConsole(h, s, (DWORD)_mi_strlen(s), NULL, NULL);
637+ //}
638+
634639static void mi_win_tls_init (DWORD reason ) {
635- #if MI_HAS_TLS_SLOT >= 2 // we must initialize the TLS slot before any allocation
636- #if MI_WIN_USE_FIXED_TLS == 1
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" );
641- }
642- _mi_win_tls_offset = (size_t )tls_slot * sizeof (void * );
643- }
644- #endif
645640 if (reason == DLL_PROCESS_ATTACH || reason == DLL_THREAD_ATTACH ) {
641+ #if MI_WIN_USE_FIXED_TLS == 1 // we must allocate a TLS slot dynamically
642+ if (_mi_win_tls_offset == 0 && reason = DLL_PROCESS_ATTACH ) {
643+ const DWORD tls_slot = TlsAlloc (); // usually returns slot 1
644+ if (tls_slot == TLS_OUT_OF_INDEXES ) {
645+ _mi_error_message (EFAULT , "unable to allocate the a TLS slot (rebuild without MI_WIN_USE_FIXED_TLS?)\n" );
646+ }
647+ _mi_win_tls_offset = (size_t )tls_slot * sizeof (void * );
648+ }
649+ #endif
650+ #if MI_HAS_TLS_SLOT >= 2 // we must initialize the TLS slot before any allocation
646651 if (mi_prim_get_default_heap () == NULL ) {
647652 _mi_heap_set_default_direct ((mi_heap_t * )& _mi_heap_empty );
648653 #if MI_DEBUG && MI_WIN_USE_FIXED_TLS == 1
649654 void * const p = TlsGetValue ((DWORD )(_mi_win_tls_offset / sizeof (void * )));
650655 mi_assert_internal (p == (void * )& _mi_heap_empty );
651656 #endif
652657 }
653- }
654- #else
655- MI_UNUSED (reason );
656- #endif
658+ #endif
659+ }
657660}
658661
659662static void NTAPI mi_win_main (PVOID module , DWORD reason , LPVOID reserved ) {
@@ -676,7 +679,7 @@ static void NTAPI mi_win_main(PVOID module, DWORD reason, LPVOID reserved) {
676679 #define MI_PRIM_HAS_PROCESS_ATTACH 1
677680
678681 // Windows DLL: easy to hook into process_init and thread_done
679- BOOL WINAPI DllMain (HINSTANCE inst , DWORD reason , LPVOID reserved ) {
682+ BOOL WINAPI DllMain (HINSTANCE inst , DWORD reason , LPVOID reserved ) {
680683 mi_win_main ((PVOID )inst ,reason ,reserved );
681684 return TRUE;
682685 }
0 commit comments