Skip to content

Commit 41cc1bf

Browse files
committed
add guarded TLS test for Windows fixed TLS
1 parent df3e191 commit 41cc1bf

File tree

4 files changed

+21
-19
lines changed

4 files changed

+21
-19
lines changed

ide/vs2022/mimalloc-test-stress.vcxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@
282282
</ClCompile>
283283
</ItemGroup>
284284
<ItemGroup>
285-
<ProjectReference Include="mimalloc-lib.vcxproj">
286-
<Project>{abb5eae7-b3e6-432e-b636-333449892ea6}</Project>
285+
<ProjectReference Include="mimalloc-override-dll.vcxproj">
286+
<Project>{abb5eae7-b3e6-432e-b636-333449892ea7}</Project>
287287
</ProjectReference>
288288
</ItemGroup>
289289
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

include/mimalloc/prim.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static inline void mi_prim_tls_slot_set(size_t slot, void* value) mi_attr_noexce
208208
// thread-local initialization checks in the fast path.
209209
// We allocate a user TLS slot at process initialization (see `windows/prim.c`)
210210
// and store the offset `_mi_win_tls_offset`.
211-
#define MI_HAS_TLS_SLOT 2 // 2 = we can reliably initialize the slot (saving a test on each malloc)
211+
#define MI_HAS_TLS_SLOT 1 // 2 = we can reliably initialize the slot (saving a test on each malloc)
212212

213213
extern mi_decl_hidden size_t _mi_win_tls_offset;
214214

src/init.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ mi_heap_t* _mi_heap_main_get(void) {
225225
return &_mi_heap_main;
226226
}
227227

228-
229228
/* -----------------------------------------------------------
230229
Sub process
231230
----------------------------------------------------------- */

src/prim/windows/prim.c

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -631,29 +631,32 @@ bool _mi_prim_random_buf(void* buf, size_t buf_len) {
631631
mi_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+
634639
static 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

659662
static 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

Comments
 (0)