[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / glib / gconstructor.h
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 #if  __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
20
21 #define G_HAS_CONSTRUCTORS 1
22
23 #define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
24 #define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
25
26 #elif defined (_MSC_VER) && (_MSC_VER >= 1500)
27 /* Visual studio 2008 and later has _Pragma */
28
29 #define G_HAS_CONSTRUCTORS 1
30
31 #define G_DEFINE_CONSTRUCTOR(_func) \
32   static void _func(void); \
33   static int _func ## _wrapper(void) { _func(); return 0; } \
34   __pragma(section(".CRT$XCU",read)) \
35   __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _wrapper;
36
37 #define G_DEFINE_DESTRUCTOR(_func) \
38   static void _func(void); \
39   static int _func ## _constructor(void) { atexit (_func); return 0; } \
40   __pragma(section(".CRT$XCU",read)) \
41   __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
42
43 #elif defined (_MSC_VER)
44
45 #define G_HAS_CONSTRUCTORS 1
46
47 /* Pre Visual studio 2008 must use #pragma section */
48 #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
49 #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
50
51 #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
52   section(".CRT$XCU",read)
53 #define G_DEFINE_CONSTRUCTOR(_func) \
54   static void _func(void); \
55   static int _func ## _wrapper(void) { _func(); return 0; } \
56   __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
57
58 #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
59   section(".CRT$XCU",read)
60 #define G_DEFINE_DESTRUCTOR(_func) \
61   static void _func(void); \
62   static int _func ## _constructor(void) { atexit (_func); return 0; } \
63   __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
64
65 #elif defined(__SUNPRO_C)
66
67 /* This is not tested, but i believe it should work, based on:
68  * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
69  */
70
71 #define G_HAS_CONSTRUCTORS 1
72
73 #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
74 #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
75
76 #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
77   init(_func)
78 #define G_DEFINE_CONSTRUCTOR(_func) \
79   static void _func(void);
80
81 #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
82   fini(_func)
83 #define G_DEFINE_DESTRUCTOR(_func) \
84   static void _func(void);
85
86 #else
87
88 /* constructors not supported for this compiler */
89
90 #endif
91