mirror of
https://github.com/microsoft/mimalloc.git
synced 2025-05-15 03:29:31 +03:00
add guarded TLS test for Windows fixed TLS
This commit is contained in:
parent
df3e191620
commit
41cc1bfe51
4 changed files with 21 additions and 19 deletions
|
@ -282,8 +282,8 @@
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="mimalloc-lib.vcxproj">
|
<ProjectReference Include="mimalloc-override-dll.vcxproj">
|
||||||
<Project>{abb5eae7-b3e6-432e-b636-333449892ea6}</Project>
|
<Project>{abb5eae7-b3e6-432e-b636-333449892ea7}</Project>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||||
|
|
|
@ -208,7 +208,7 @@ static inline void mi_prim_tls_slot_set(size_t slot, void* value) mi_attr_noexce
|
||||||
// thread-local initialization checks in the fast path.
|
// thread-local initialization checks in the fast path.
|
||||||
// We allocate a user TLS slot at process initialization (see `windows/prim.c`)
|
// We allocate a user TLS slot at process initialization (see `windows/prim.c`)
|
||||||
// and store the offset `_mi_win_tls_offset`.
|
// and store the offset `_mi_win_tls_offset`.
|
||||||
#define MI_HAS_TLS_SLOT 2 // 2 = we can reliably initialize the slot (saving a test on each malloc)
|
#define MI_HAS_TLS_SLOT 1 // 2 = we can reliably initialize the slot (saving a test on each malloc)
|
||||||
|
|
||||||
extern mi_decl_hidden size_t _mi_win_tls_offset;
|
extern mi_decl_hidden size_t _mi_win_tls_offset;
|
||||||
|
|
||||||
|
|
|
@ -225,7 +225,6 @@ mi_heap_t* _mi_heap_main_get(void) {
|
||||||
return &_mi_heap_main;
|
return &_mi_heap_main;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* -----------------------------------------------------------
|
/* -----------------------------------------------------------
|
||||||
Sub process
|
Sub process
|
||||||
----------------------------------------------------------- */
|
----------------------------------------------------------- */
|
||||||
|
|
|
@ -631,18 +631,23 @@ bool _mi_prim_random_buf(void* buf, size_t buf_len) {
|
||||||
mi_decl_cache_align size_t _mi_win_tls_offset = 0;
|
mi_decl_cache_align size_t _mi_win_tls_offset = 0;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
//static void mi_debug_out(const char* s) {
|
||||||
|
// HANDLE h = GetStdHandle(STD_ERROR_HANDLE);
|
||||||
|
// WriteConsole(h, s, (DWORD)_mi_strlen(s), NULL, NULL);
|
||||||
|
//}
|
||||||
|
|
||||||
static void mi_win_tls_init(DWORD reason) {
|
static void mi_win_tls_init(DWORD reason) {
|
||||||
#if MI_HAS_TLS_SLOT >= 2 // we must initialize the TLS slot before any allocation
|
|
||||||
#if MI_WIN_USE_FIXED_TLS==1
|
|
||||||
if (reason==DLL_PROCESS_ATTACH && _mi_win_tls_offset == 0) {
|
|
||||||
const DWORD tls_slot = TlsAlloc(); // usually returns slot 1
|
|
||||||
if (tls_slot == TLS_OUT_OF_INDEXES) {
|
|
||||||
_mi_error_message(EFAULT, "unable to allocate the a TLS slot (rebuild without MI_WIN_USE_FIXED_TLS?)\n");
|
|
||||||
}
|
|
||||||
_mi_win_tls_offset = (size_t)tls_slot * sizeof(void*);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
if (reason==DLL_PROCESS_ATTACH || reason==DLL_THREAD_ATTACH) {
|
if (reason==DLL_PROCESS_ATTACH || reason==DLL_THREAD_ATTACH) {
|
||||||
|
#if MI_WIN_USE_FIXED_TLS==1 // we must allocate a TLS slot dynamically
|
||||||
|
if (_mi_win_tls_offset == 0 && reason=DLL_PROCESS_ATTACH) {
|
||||||
|
const DWORD tls_slot = TlsAlloc(); // usually returns slot 1
|
||||||
|
if (tls_slot == TLS_OUT_OF_INDEXES) {
|
||||||
|
_mi_error_message(EFAULT, "unable to allocate the a TLS slot (rebuild without MI_WIN_USE_FIXED_TLS?)\n");
|
||||||
|
}
|
||||||
|
_mi_win_tls_offset = (size_t)tls_slot * sizeof(void*);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#if MI_HAS_TLS_SLOT >= 2 // we must initialize the TLS slot before any allocation
|
||||||
if (mi_prim_get_default_heap() == NULL) {
|
if (mi_prim_get_default_heap() == NULL) {
|
||||||
_mi_heap_set_default_direct((mi_heap_t*)&_mi_heap_empty);
|
_mi_heap_set_default_direct((mi_heap_t*)&_mi_heap_empty);
|
||||||
#if MI_DEBUG && MI_WIN_USE_FIXED_TLS==1
|
#if MI_DEBUG && MI_WIN_USE_FIXED_TLS==1
|
||||||
|
@ -650,10 +655,8 @@ static void mi_win_tls_init(DWORD reason) {
|
||||||
mi_assert_internal(p == (void*)&_mi_heap_empty);
|
mi_assert_internal(p == (void*)&_mi_heap_empty);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
}
|
#endif
|
||||||
#else
|
}
|
||||||
MI_UNUSED(reason);
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void NTAPI mi_win_main(PVOID module, DWORD reason, LPVOID reserved) {
|
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) {
|
||||||
#define MI_PRIM_HAS_PROCESS_ATTACH 1
|
#define MI_PRIM_HAS_PROCESS_ATTACH 1
|
||||||
|
|
||||||
// Windows DLL: easy to hook into process_init and thread_done
|
// Windows DLL: easy to hook into process_init and thread_done
|
||||||
BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) {
|
BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID reserved) {
|
||||||
mi_win_main((PVOID)inst,reason,reserved);
|
mi_win_main((PVOID)inst,reason,reserved);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue