From dfdb9cb8772f8fabf63ce81aaa7e2aac9621a1e3 Mon Sep 17 00:00:00 2001 From: Daan Date: Tue, 22 Oct 2024 06:52:34 -0700 Subject: [PATCH] cleanup process init/done --- src/prim/prim.c | 20 ++++++++++++++++---- src/prim/windows/prim.c | 12 ++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/prim/prim.c b/src/prim/prim.c index 8a2400c8..a4d3814f 100644 --- a/src/prim/prim.c +++ b/src/prim/prim.c @@ -29,7 +29,8 @@ terms of the MIT license. A copy of the license can be found in the file // Generic process initialization #ifndef MI_PRIM_HAS_PROCESS_ATTACH #if defined(__GNUC__) || defined(__clang__) - // GCC,Clang: use the constructor attribute + // gcc,clang: use the constructor/destructor attribute + // which for both seem to run before regular constructors/destructors #if defined(__clang__) #define mi_attr_constructor __attribute__((constructor(101))) #define mi_attr_destructor __attribute__((destructor(101))) @@ -44,7 +45,17 @@ terms of the MIT license. A copy of the license can be found in the file _mi_process_done(); } #elif defined(__cplusplus) - // C++: use static initialization to detect process start + // C++: use static initialization to detect process start/end + struct mi_init_done_t { + mi_init_done_t() { + _mi_process_load(); + } + ~mi_init_done_t() { + _mi_process_done(); + } + }; + static mi_init_done_t mi_init_done; + /* extern mi_heap_t _mi_heap_main; static bool mi_process_attach(void) { _mi_process_load(); @@ -52,7 +63,8 @@ terms of the MIT license. A copy of the license can be found in the file return (_mi_heap_main.thread_id != 0); } static bool mi_initialized = mi_process_attach(); -#else + */ + #else #pragma message("define a way to call _mi_process_load/done on your platform") #endif #endif @@ -63,7 +75,7 @@ bool _mi_is_redirected(void) { return false; } bool _mi_allocator_init(const char** message) { - if (message != NULL) *message = NULL; + if (message != NULL) { *message = NULL; } return true; } void _mi_allocator_done(void) { diff --git a/src/prim/windows/prim.c b/src/prim/windows/prim.c index 7ba1f1de..b0631b81 100644 --- a/src/prim/windows/prim.c +++ b/src/prim/windows/prim.c @@ -674,7 +674,7 @@ static void NTAPI mi_win_main(PVOID module, DWORD reason, LPVOID reserved) { MI_UNUSED(heap); } -#else // statically linked, use fiber api +#else // deprecated: statically linked, use fiber api #if defined(_MSC_VER) // on clang/gcc use the constructor attribute (in `src/prim/prim.c`) // MSVC: use data section magic for static libraries @@ -686,15 +686,15 @@ static void NTAPI mi_win_main(PVOID module, DWORD reason, LPVOID reserved) { atexit(&_mi_process_done); return 0; } - typedef int(*_mi_crt_callback_t)(void); - #if defined(_M_X64) || defined(_M_ARM64) - __pragma(comment(linker, "/include:" "_mi_msvc_initu")) + typedef int(*mi_crt_callback_t)(void); + #if defined(_WIN64) + #pragma comment(linker, "/INCLUDE:_mi_tls_callback") #pragma section(".CRT$XIU", long, read) #else - __pragma(comment(linker, "/include:" "__mi_msvc_initu")) + #pragma comment(linker, "/INCLUDE:__mi_tls_callback") #endif #pragma data_seg(".CRT$XIU") - mi_decl_externc _mi_crt_callback_t _mi_msvc_initu[] = { &mi_process_attach }; + mi_decl_externc mi_crt_callback_t _mi_tls_callback[] = { &mi_process_attach }; #pragma data_seg() #endif