12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- /*
- If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
- destructors, in a sane way, including e.g. on library unload. If not you're on
- your own.
- Some compilers need #pragma to handle this, which does not work with macros,
- so the way you need to use this is (for constructors):
- #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
- #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
- #endif
- G_DEFINE_CONSTRUCTOR(my_constructor)
- static void my_constructor(void) {
- ...
- }
- */
- #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
- #define G_HAS_CONSTRUCTORS 1
- #define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
- #define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
- #elif defined (_MSC_VER) && (_MSC_VER >= 1500)
- /* Visual Studio 2008 and later has _pragma */
- /* if defined, simulation with OPC crashes without traces for Windows 10 (but not Windows 7) */
- #ifndef DYMOSIM_OPC_SERVER
- #define G_HAS_CONSTRUCTORS 1
- #endif /* DYMOSIM_OPC_SERVER */
- #define G_DEFINE_CONSTRUCTOR(_func) \
- static void _func(void); \
- static int _func ## _wrapper(void) { _func(); return 0; } \
- __pragma(section(".CRT$XCU",read)) \
- __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _wrapper;
- #define G_DEFINE_DESTRUCTOR(_func) \
- static void _func(void); \
- static int _func ## _constructor(void) { atexit (_func); return 0; } \
- __pragma(section(".CRT$XCU",read)) \
- __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
- #elif defined (_MSC_VER) && (_MSC_VER >= 1400)
- #define G_HAS_CONSTRUCTORS 1
- /* Pre Visual Studio 2008 must use #pragma section */
- #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
- #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
- #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
- section(".CRT$XCU",read)
- #define G_DEFINE_CONSTRUCTOR(_func) \
- static void _func(void); \
- static int _func ## _wrapper(void) { _func(); return 0; } \
- __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
- #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
- section(".CRT$XCU",read)
- #define G_DEFINE_DESTRUCTOR(_func) \
- static void _func(void); \
- static int _func ## _constructor(void) { atexit (_func); return 0; } \
- __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
- #elif defined(__SUNPRO_C)
- /* This is not tested, but I believe it should work, based on:
- * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
- */
- #define G_HAS_CONSTRUCTORS 1
- #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
- #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
- #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
- init(_func)
- #define G_DEFINE_CONSTRUCTOR(_func) \
- static void _func(void);
- #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
- fini(_func)
- #define G_DEFINE_DESTRUCTOR(_func) \
- static void _func(void);
- #else
- /* constructors not supported for this compiler */
- #endif
|