| 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 |
|
| 6 |
Some compilers need #pragma to handle this, which does not work with macros, |
| 7 |
so the way you need to use this is (for constructors): |
| 8 |
|
| 9 |
#ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA |
| 10 |
#pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor) |
| 11 |
#endif |
| 12 |
G_DEFINE_CONSTRUCTOR(my_constructor) |
| 13 |
static void my_constructor(void) { |
| 14 |
... |
| 15 |
} |
| 16 |
|
| 17 |
*/ |
| 18 |
|
| 19 |
#ifndef __GTK_DOC_IGNORE__ |
| 20 |
|
| 21 |
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7) |
| 22 |
|
| 23 |
#define G_HAS_CONSTRUCTORS 1 |
| 24 |
|
| 25 |
#define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void); |
| 26 |
#define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void); |
| 27 |
|
| 28 |
#elif defined (_MSC_VER) && (_MSC_VER >= 1500) |
| 29 |
/* Visual studio 2008 and later has _Pragma */ |
| 30 |
|
| 31 |
#define G_HAS_CONSTRUCTORS 1 |
| 32 |
|
| 33 |
#define G_DEFINE_CONSTRUCTOR(_func) \ |
| 34 |
static void _func(void); \ |
| 35 |
static int _func ## _wrapper(void) { _func(); return 0; } \ |
| 36 |
__pragma(section(".CRT$XCU",read)) \ |
| 37 |
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _wrapper; |
| 38 |
|
| 39 |
#define G_DEFINE_DESTRUCTOR(_func) \ |
| 40 |
static void _func(void); \ |
| 41 |
static int _func ## _constructor(void) { atexit (_func); return 0; } \ |
| 42 |
__pragma(section(".CRT$XCU",read)) \ |
| 43 |
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor; |
| 44 |
|
| 45 |
#elif defined (_MSC_VER) |
| 46 |
|
| 47 |
#define G_HAS_CONSTRUCTORS 1 |
| 48 |
|
| 49 |
/* Pre Visual studio 2008 must use #pragma section */ |
| 50 |
#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 |
| 51 |
#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 |
| 52 |
|
| 53 |
#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ |
| 54 |
section(".CRT$XCU",read) |
| 55 |
#define G_DEFINE_CONSTRUCTOR(_func) \ |
| 56 |
static void _func(void); \ |
| 57 |
static int _func ## _wrapper(void) { _func(); return 0; } \ |
| 58 |
__declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper; |
| 59 |
|
| 60 |
#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ |
| 61 |
section(".CRT$XCU",read) |
| 62 |
#define G_DEFINE_DESTRUCTOR(_func) \ |
| 63 |
static void _func(void); \ |
| 64 |
static int _func ## _constructor(void) { atexit (_func); return 0; } \ |
| 65 |
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor; |
| 66 |
|
| 67 |
#elif defined(__SUNPRO_C) |
| 68 |
|
| 69 |
/* This is not tested, but i believe it should work, based on: |
| 70 |
* http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c |
| 71 |
*/ |
| 72 |
|
| 73 |
#define G_HAS_CONSTRUCTORS 1 |
| 74 |
|
| 75 |
#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 |
| 76 |
#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 |
| 77 |
|
| 78 |
#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ |
| 79 |
init(_func) |
| 80 |
#define G_DEFINE_CONSTRUCTOR(_func) \ |
| 81 |
static void _func(void); |
| 82 |
|
| 83 |
#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ |
| 84 |
fini(_func) |
| 85 |
#define G_DEFINE_DESTRUCTOR(_func) \ |
| 86 |
static void _func(void); |
| 87 |
|
| 88 |
#else |
| 89 |
|
| 90 |
/* constructors not supported for this compiler */ |
| 91 |
|
| 92 |
#endif |
| 93 |
|
| 94 |
#endif /* __GTK_DOC_IGNORE__ */ |