cleanup process init/done

This commit is contained in:
Daan 2024-10-22 06:52:34 -07:00
parent 104e821709
commit dfdb9cb877
2 changed files with 22 additions and 10 deletions

View file

@ -29,7 +29,8 @@ terms of the MIT license. A copy of the license can be found in the file
// Generic process initialization // Generic process initialization
#ifndef MI_PRIM_HAS_PROCESS_ATTACH #ifndef MI_PRIM_HAS_PROCESS_ATTACH
#if defined(__GNUC__) || defined(__clang__) #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__) #if defined(__clang__)
#define mi_attr_constructor __attribute__((constructor(101))) #define mi_attr_constructor __attribute__((constructor(101)))
#define mi_attr_destructor __attribute__((destructor(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(); _mi_process_done();
} }
#elif defined(__cplusplus) #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; extern mi_heap_t _mi_heap_main;
static bool mi_process_attach(void) { static bool mi_process_attach(void) {
_mi_process_load(); _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); return (_mi_heap_main.thread_id != 0);
} }
static bool mi_initialized = mi_process_attach(); static bool mi_initialized = mi_process_attach();
#else */
#else
#pragma message("define a way to call _mi_process_load/done on your platform") #pragma message("define a way to call _mi_process_load/done on your platform")
#endif #endif
#endif #endif
@ -63,7 +75,7 @@ bool _mi_is_redirected(void) {
return false; return false;
} }
bool _mi_allocator_init(const char** message) { bool _mi_allocator_init(const char** message) {
if (message != NULL) *message = NULL; if (message != NULL) { *message = NULL; }
return true; return true;
} }
void _mi_allocator_done(void) { void _mi_allocator_done(void) {

View file

@ -674,7 +674,7 @@ static void NTAPI mi_win_main(PVOID module, DWORD reason, LPVOID reserved) {
MI_UNUSED(heap); 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`) #if defined(_MSC_VER) // on clang/gcc use the constructor attribute (in `src/prim/prim.c`)
// MSVC: use data section magic for static libraries // 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); atexit(&_mi_process_done);
return 0; return 0;
} }
typedef int(*_mi_crt_callback_t)(void); typedef int(*mi_crt_callback_t)(void);
#if defined(_M_X64) || defined(_M_ARM64) #if defined(_WIN64)
__pragma(comment(linker, "/include:" "_mi_msvc_initu")) #pragma comment(linker, "/INCLUDE:_mi_tls_callback")
#pragma section(".CRT$XIU", long, read) #pragma section(".CRT$XIU", long, read)
#else #else
__pragma(comment(linker, "/include:" "__mi_msvc_initu")) #pragma comment(linker, "/INCLUDE:__mi_tls_callback")
#endif #endif
#pragma data_seg(".CRT$XIU") #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() #pragma data_seg()
#endif #endif