gconstructor.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
  3. destructors, in a sane way, including e.g. on library unload. If not you're on
  4. your own.
  5. Some compilers need #pragma to handle this, which does not work with macros,
  6. so the way you need to use this is (for constructors):
  7. #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
  8. #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
  9. #endif
  10. G_DEFINE_CONSTRUCTOR(my_constructor)
  11. static void my_constructor(void) {
  12. ...
  13. }
  14. */
  15. #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
  16. #define G_HAS_CONSTRUCTORS 1
  17. #define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
  18. #define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
  19. #elif defined (_MSC_VER) && (_MSC_VER >= 1500)
  20. /* Visual Studio 2008 and later has _pragma */
  21. /* if defined, simulation with OPC crashes without traces for Windows 10 (but not Windows 7) */
  22. #ifndef DYMOSIM_OPC_SERVER
  23. #define G_HAS_CONSTRUCTORS 1
  24. #endif /* DYMOSIM_OPC_SERVER */
  25. #define G_DEFINE_CONSTRUCTOR(_func) \
  26. static void _func(void); \
  27. static int _func ## _wrapper(void) { _func(); return 0; } \
  28. __pragma(section(".CRT$XCU",read)) \
  29. __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _wrapper;
  30. #define G_DEFINE_DESTRUCTOR(_func) \
  31. static void _func(void); \
  32. static int _func ## _constructor(void) { atexit (_func); return 0; } \
  33. __pragma(section(".CRT$XCU",read)) \
  34. __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
  35. #elif defined (_MSC_VER) && (_MSC_VER >= 1400)
  36. #define G_HAS_CONSTRUCTORS 1
  37. /* Pre Visual Studio 2008 must use #pragma section */
  38. #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
  39. #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
  40. #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
  41. section(".CRT$XCU",read)
  42. #define G_DEFINE_CONSTRUCTOR(_func) \
  43. static void _func(void); \
  44. static int _func ## _wrapper(void) { _func(); return 0; } \
  45. __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
  46. #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
  47. section(".CRT$XCU",read)
  48. #define G_DEFINE_DESTRUCTOR(_func) \
  49. static void _func(void); \
  50. static int _func ## _constructor(void) { atexit (_func); return 0; } \
  51. __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
  52. #elif defined(__SUNPRO_C)
  53. /* This is not tested, but I believe it should work, based on:
  54. * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
  55. */
  56. #define G_HAS_CONSTRUCTORS 1
  57. #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
  58. #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
  59. #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
  60. init(_func)
  61. #define G_DEFINE_CONSTRUCTOR(_func) \
  62. static void _func(void);
  63. #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
  64. fini(_func)
  65. #define G_DEFINE_DESTRUCTOR(_func) \
  66. static void _func(void);
  67. #else
  68. /* constructors not supported for this compiler */
  69. #endif