Skip to content

Commit c84d996

Browse files
committed
fix TLS initialization for MI_WIN_USE_FIXED_TLS with redirection
1 parent e2d7c24 commit c84d996

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

include/mimalloc/prim.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ void _mi_prim_thread_associate_default_heap(mi_heap_t* heap);
119119

120120

121121

122-
122+
#define MI_WIN_USE_FIXED_TLS 1
123123

124124
//-------------------------------------------------------------------
125125
// Access to TLS (thread local storage) slots.

src/prim/windows/prim.c

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -628,18 +628,16 @@ bool _mi_prim_random_buf(void* buf, size_t buf_len) {
628628
//----------------------------------------------------------------
629629

630630
#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;
632632
#endif
633633

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) {
637635
#if MI_HAS_TLS_SLOT >= 2 // we must initialize the TLS slot before any allocation
638636
#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");
643641
}
644642
_mi_win_tls_offset = (size_t)tls_slot * sizeof(void*);
645643
}
@@ -653,7 +651,15 @@ static void NTAPI mi_win_main(PVOID module, DWORD reason, LPVOID reserved) {
653651
mi_assert_internal(p == (void*)&_mi_heap_empty);
654652
#endif
655653
}
654+
#else
655+
MI_UNUSED(reason);
656656
#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);
657663
if (reason==DLL_PROCESS_ATTACH) {
658664
_mi_process_load();
659665
}
@@ -815,11 +821,7 @@ static void NTAPI mi_win_main(PVOID module, DWORD reason, LPVOID reserved) {
815821
#endif
816822
mi_decl_export void _mi_redirect_entry(DWORD reason) {
817823
// 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);
823825
if (reason == DLL_PROCESS_ATTACH) {
824826
mi_redirected = true;
825827
}

0 commit comments

Comments
 (0)