Skip to content

Commit

Permalink
Switch to better initializer strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexodia committed Nov 26, 2024
1 parent 2373fca commit 7d3cc96
Showing 1 changed file with 27 additions and 43 deletions.
70 changes: 27 additions & 43 deletions src/glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,39 @@
#include "common.h"
#include "libimobiledevice-glue/thread.h"

extern void term_colors_init();

static void internal_glue_init(void)
{
term_colors_init();
}

static void internal_glue_deinit(void)
{

}

static thread_once_t init_once = THREAD_ONCE_INIT;
static thread_once_t deinit_once = THREAD_ONCE_INIT;
// Reference: https://stackoverflow.com/a/2390626/1806760
// Initializer/finalizer sample for MSVC and GCC/Clang.
// 2010-2016 Joe Lowe. Released into the public domain.

#ifndef HAVE_ATTRIBUTE_CONSTRUCTOR
#if defined(__llvm__) || defined(__GNUC__)
#define HAVE_ATTRIBUTE_CONSTRUCTOR
#endif
#ifdef __cplusplus
#define INITIALIZER(f) \
static void f(void); \
struct f##_t_ { f##_t_(void) { f(); } }; static f##_t_ f##_; \
static void f(void)
#elif defined(_MSC_VER)
#pragma section(".CRT$XCU",read)
#define INITIALIZER2_(f,p) \
static void f(void); \
__declspec(allocate(".CRT$XCU")) void (*f##_)(void) = f; \
__pragma(comment(linker,"/include:" p #f "_")) \
static void f(void)
#ifdef _WIN64
#define INITIALIZER(f) INITIALIZER2_(f,"")
#else
#define INITIALIZER(f) INITIALIZER2_(f,"_")
#endif
#else
#define INITIALIZER(f) \
static void f(void) __attribute__((__constructor__)); \
static void f(void)
#endif

#ifdef HAVE_ATTRIBUTE_CONSTRUCTOR
static void __attribute__((constructor)) limd_glue_initialize(void)
{
thread_once(&init_once, internal_glue_init);
}
extern void term_colors_init();

static void __attribute__((destructor)) limd_glue_deinitialize(void)
INITIALIZER(internal_glue_init)
{
thread_once(&deinit_once, internal_glue_deinit);
}
#elif defined(WIN32)
BOOL WINAPI DllMain(HINSTANCE hModule, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason) {
case DLL_PROCESS_ATTACH:
thread_once(&init_once, internal_glue_init);
break;
case DLL_PROCESS_DETACH:
thread_once(&deinit_once, internal_glue_deinit);
break;
default:
break;
}
return 1;
term_colors_init();
}
#else
#warning No compiler support for constructor/destructor attributes, some features might not be available.
#endif

const char* libimobiledevice_glue_version()
{
Expand Down

0 comments on commit 7d3cc96

Please sign in to comment.