do not automatically call mi_process_done if mi_option_destroy_on_exit > 1

This commit is contained in:
Daan Leijen 2025-06-07 10:12:53 -07:00
parent e7cbbbfb14
commit c1249a4b15
4 changed files with 25 additions and 25 deletions

View file

@ -63,7 +63,7 @@ terms of the MIT license. A copy of the license can be found in the file
#define mi_decl_noinline #define mi_decl_noinline
#define mi_decl_thread __thread // hope for the best :-) #define mi_decl_thread __thread // hope for the best :-)
#define mi_decl_align(a) #define mi_decl_align(a)
#define mi_decl_noreturn #define mi_decl_noreturn
#define mi_decl_weak #define mi_decl_weak
#define mi_decl_hidden #define mi_decl_hidden
#define mi_decl_cold #define mi_decl_cold
@ -135,8 +135,8 @@ static inline uintptr_t _mi_random_shuffle(uintptr_t x);
// init.c // init.c
extern mi_decl_hidden mi_decl_cache_align mi_stats_t _mi_stats_main; extern mi_decl_hidden mi_decl_cache_align mi_stats_t _mi_stats_main;
extern mi_decl_hidden mi_decl_cache_align const mi_page_t _mi_page_empty; extern mi_decl_hidden mi_decl_cache_align const mi_page_t _mi_page_empty;
void _mi_process_load(void); void _mi_auto_process_init(void);
void mi_cdecl _mi_process_done(void); void mi_cdecl _mi_auto_process_done(void) mi_attr_noexcept;
bool _mi_is_redirected(void); bool _mi_is_redirected(void);
bool _mi_allocator_init(const char** message); bool _mi_allocator_init(const char** message);
void _mi_allocator_done(void); void _mi_allocator_done(void);

View file

@ -323,7 +323,7 @@ static mi_thread_data_t* mi_thread_data_zalloc(void) {
return NULL; return NULL;
} }
} }
td->memid = memid; td->memid = memid;
return td; return td;
} }
@ -555,7 +555,7 @@ mi_decl_nodiscard bool mi_is_redirected(void) mi_attr_noexcept {
} }
// Called once by the process loader from `src/prim/prim.c` // Called once by the process loader from `src/prim/prim.c`
void _mi_process_load(void) { void _mi_auto_process_init(void) {
mi_heap_main_init(); mi_heap_main_init();
#if defined(__APPLE__) || defined(MI_TLS_RECURSE_GUARD) #if defined(__APPLE__) || defined(MI_TLS_RECURSE_GUARD)
volatile mi_heap_t* dummy = _mi_heap_default; // access TLS to allocate it before setting tls_initialized to true; volatile mi_heap_t* dummy = _mi_heap_default; // access TLS to allocate it before setting tls_initialized to true;
@ -642,12 +642,8 @@ void mi_process_init(void) mi_attr_noexcept {
} }
} }
void mi_cdecl mi_process_done(void) mi_attr_noexcept {
_mi_process_done();
}
// Called when the process is done (cdecl as it is used with `at_exit` on some platforms) // Called when the process is done (cdecl as it is used with `at_exit` on some platforms)
void mi_cdecl _mi_process_done(void) { void mi_cdecl mi_process_done(void) mi_attr_noexcept {
// only shutdown if we were initialized // only shutdown if we were initialized
if (!_mi_process_is_initialized) return; if (!_mi_process_is_initialized) return;
// ensure we are called once // ensure we are called once
@ -690,3 +686,7 @@ void mi_cdecl _mi_process_done(void) {
os_preloading = true; // don't call the C runtime anymore os_preloading = true; // don't call the C runtime anymore
} }
void mi_cdecl _mi_auto_process_done(void) mi_attr_noexcept {
if (_mi_option_get_fast(mi_option_destroy_on_exit)>1) return;
mi_process_done();
}

View file

@ -39,29 +39,29 @@ terms of the MIT license. A copy of the license can be found in the file
#define mi_attr_destructor __attribute__((destructor)) #define mi_attr_destructor __attribute__((destructor))
#endif #endif
static void mi_attr_constructor mi_process_attach(void) { static void mi_attr_constructor mi_process_attach(void) {
_mi_process_load(); _mi_auto_process_init();
} }
static void mi_attr_destructor mi_process_detach(void) { static void mi_attr_destructor mi_process_detach(void) {
_mi_process_done(); _mi_auto_process_done();
} }
#elif defined(__cplusplus) #elif defined(__cplusplus)
// C++: use static initialization to detect process start/end // C++: use static initialization to detect process start/end
// This is not guaranteed to be first/last but the best we can generally do? // This is not guaranteed to be first/last but the best we can generally do?
struct mi_init_done_t { struct mi_init_done_t {
mi_init_done_t() { mi_init_done_t() {
_mi_process_load(); _mi_auto_process_init();
} }
~mi_init_done_t() { ~mi_init_done_t() {
_mi_process_done(); _mi_auto_process_done();
} }
}; };
static mi_init_done_t mi_init_done; static mi_init_done_t mi_init_done;
#else #else
#pragma message("define a way to call _mi_process_load/done on your platform") #pragma message("define a way to call _mi_auto_process_init/done on your platform")
#endif #endif
#endif #endif
// Generic allocator init/done callback // Generic allocator init/done callback
#ifndef MI_PRIM_HAS_ALLOCATOR_INIT #ifndef MI_PRIM_HAS_ALLOCATOR_INIT
bool _mi_is_redirected(void) { bool _mi_is_redirected(void) {
return false; return false;

View file

@ -633,7 +633,7 @@ bool _mi_prim_random_buf(void* buf, size_t buf_len) {
//---------------------------------------------------------------- //----------------------------------------------------------------
#if MI_WIN_USE_FIXED_TLS==1 #if MI_WIN_USE_FIXED_TLS==1
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) { //static void mi_debug_out(const char* s) {
@ -654,14 +654,14 @@ static void mi_win_tls_init(DWORD reason) {
#endif #endif
#if MI_HAS_TLS_SLOT >= 2 // we must initialize the TLS slot before any allocation #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
void* const p = TlsGetValue((DWORD)(_mi_win_tls_offset / sizeof(void*))); void* const p = TlsGetValue((DWORD)(_mi_win_tls_offset / sizeof(void*)));
mi_assert_internal(p == (void*)&_mi_heap_empty); mi_assert_internal(p == (void*)&_mi_heap_empty);
#endif #endif
} }
#endif #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) {
@ -669,10 +669,10 @@ static void NTAPI mi_win_main(PVOID module, DWORD reason, LPVOID reserved) {
MI_UNUSED(module); MI_UNUSED(module);
mi_win_tls_init(reason); mi_win_tls_init(reason);
if (reason==DLL_PROCESS_ATTACH) { if (reason==DLL_PROCESS_ATTACH) {
_mi_process_load(); _mi_auto_process_init();
} }
else if (reason==DLL_PROCESS_DETACH) { else if (reason==DLL_PROCESS_DETACH) {
_mi_process_done(); _mi_auto_process_done();
} }
else if (reason==DLL_THREAD_DETACH && !_mi_is_redirected()) { else if (reason==DLL_THREAD_DETACH && !_mi_is_redirected()) {
_mi_thread_done(NULL); _mi_thread_done(NULL);
@ -684,7 +684,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;
} }
@ -762,7 +762,7 @@ static void NTAPI mi_win_main(PVOID module, DWORD reason, LPVOID reserved) {
static int mi_process_attach(void) { static int mi_process_attach(void) {
mi_win_main(NULL,DLL_PROCESS_ATTACH,NULL); mi_win_main(NULL,DLL_PROCESS_ATTACH,NULL);
atexit(&_mi_process_done); atexit(&_mi_auto_process_done);
return 0; return 0;
} }
typedef int(*mi_crt_callback_t)(void); typedef int(*mi_crt_callback_t)(void);