|
1 |
| -/* gconstructor.h - Module constructor and destructor helper header |
| 1 | +/* g2constructor.h - Module constructor and destructor helper header |
2 | 2 |
|
3 |
| - If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and |
| 3 | + If G2_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and |
4 | 4 | destructors, in a sane way, including e.g. on library unload. If not you're on
|
5 | 5 | your own.
|
6 | 6 |
|
7 | 7 | Some compilers need #pragma to handle this, which does not work with macros,
|
8 | 8 | so the way you need to use this is (for constructors):
|
9 | 9 |
|
10 |
| - #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA |
11 |
| - #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor) |
| 10 | + #ifdef G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA |
| 11 | + #pragma G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor) |
12 | 12 | #endif
|
13 |
| - G_DEFINE_CONSTRUCTOR(my_constructor) |
| 13 | + G2_DEFINE_CONSTRUCTOR(my_constructor) |
14 | 14 | static void my_constructor(void) {
|
15 | 15 | ...
|
16 | 16 | }
|
17 | 17 |
|
18 | 18 | */
|
19 | 19 |
|
20 |
| -#ifndef G_CONSTRUCTOR_H_ |
21 |
| -#define G_CONSTRUCTOR_H_ |
| 20 | +#ifndef G2_CONSTRUCTOR_H_ |
| 21 | +#define G2_CONSTRUCTOR_H_ |
| 22 | + |
| 23 | +#ifndef G2_MODEL_PREFIX |
| 24 | +#ifdef MODEL_IDENTIFIER |
| 25 | +#define G2_MODEL_PREFIX MODEL_IDENTIFIER |
| 26 | +#else |
| 27 | +#define G2_MODEL_PREFIX |
| 28 | +#endif |
| 29 | +#endif |
| 30 | + |
| 31 | +#define G2_CONCAT(a, b) a ## b |
| 32 | +#define G2_CONCAT_(a, b) G2_CONCAT(a, b) |
| 33 | +#define G2_FUNCNAME(name) G2_CONCAT_(G2_MODEL_PREFIX, name) |
22 | 34 |
|
23 | 35 | #if defined(__cplusplus)
|
24 | 36 |
|
25 |
| -#define G_HAS_CONSTRUCTORS 1 |
| 37 | +#define G2_HAS_CONSTRUCTORS 1 |
26 | 38 |
|
27 |
| -#define G_DEFINE_CONSTRUCTOR(_func) \ |
| 39 | +#define G2_DEFINE_CONSTRUCTOR(_func) \ |
28 | 40 | static void _func(void); \
|
29 | 41 | struct _func ## _wrapper_struct { _func ## _wrapper_struct() { _func(); } }; \
|
30 | 42 | static _func ## _wrapper_struct _func ## _wrapper;
|
31 | 43 |
|
32 |
| -#define G_DEFINE_DESTRUCTOR(_func) \ |
| 44 | +#define G2_DEFINE_DESTRUCTOR(_func) \ |
33 | 45 | static void _func(void); \
|
34 | 46 | struct _func ## _wrapper_struct2 { ~_func ## _wrapper_struct2() { _func(); } }; \
|
35 | 47 | static _func ## _wrapper_struct2 _func ## _wrapper2;
|
36 | 48 |
|
37 | 49 | #elif (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))) || \
|
38 | 50 | defined(__clang__)
|
39 | 51 |
|
40 |
| -#define G_HAS_CONSTRUCTORS 1 |
| 52 | +#define G2_HAS_CONSTRUCTORS 1 |
41 | 53 |
|
42 |
| -#define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void); |
43 |
| -#define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void); |
| 54 | +#define G2_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void); |
| 55 | +#define G2_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void); |
44 | 56 |
|
45 | 57 | #elif defined(_MSC_VER) && (_MSC_VER >= 1500)
|
46 | 58 | /* Visual Studio 2008 and later has _pragma */
|
47 | 59 |
|
48 |
| -#define G_HAS_CONSTRUCTORS 1 |
| 60 | +#define G2_HAS_CONSTRUCTORS 1 |
49 | 61 |
|
50 | 62 | #ifdef _WIN64
|
51 |
| -#define G_MSVC_SYMBOL_PREFIX "" |
| 63 | +#define G2_MSVC_SYMBOL_PREFIX "" |
52 | 64 | #else
|
53 |
| -#define G_MSVC_SYMBOL_PREFIX "_" |
| 65 | +#define G2_MSVC_SYMBOL_PREFIX "_" |
54 | 66 | #endif
|
55 | 67 |
|
56 |
| -#define G_DEFINE_CONSTRUCTOR(_func) G_MSVC_CTOR (_func, G_MSVC_SYMBOL_PREFIX) |
57 |
| -#define G_DEFINE_DESTRUCTOR(_func) G_MSVC_DTOR (_func, G_MSVC_SYMBOL_PREFIX) |
| 68 | +#define G2_DEFINE_CONSTRUCTOR(_func) G2_MSVC_CTOR(_func, G2_MSVC_SYMBOL_PREFIX) |
| 69 | +#define G2_DEFINE_DESTRUCTOR(_func) G2_MSVC_DTOR(_func, G2_MSVC_SYMBOL_PREFIX) |
58 | 70 |
|
59 |
| -#define G_MSVC_CTOR(_func,_sym_prefix) \ |
| 71 | +#define G2_MSVC_CTOR(_func, _sym_prefix) \ |
60 | 72 | static void _func(void); \
|
61 | 73 | extern int (* _array ## _func)(void); \
|
62 | 74 | int _func ## _wrapper(void) { _func(); return _array ## _func == NULL; } \
|
63 | 75 | __pragma(comment(linker,"/include:" _sym_prefix # _func "_wrapper")) \
|
64 | 76 | __pragma(section(".CRT$XCU",read)) \
|
65 | 77 | __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _wrapper;
|
66 | 78 |
|
67 |
| -#define G_MSVC_DTOR(_func,_sym_prefix) \ |
| 79 | +#define G2_MSVC_DTOR(_func, _sym_prefix) \ |
68 | 80 | static void _func(void); \
|
69 | 81 | extern int (* _array ## _func)(void); \
|
70 | 82 | int _func ## _constructor(void) { atexit (_func); return _array ## _func == NULL; } \
|
|
74 | 86 |
|
75 | 87 | #elif defined(_MSC_VER) && (_MSC_VER >= 1400)
|
76 | 88 |
|
77 |
| -#define G_HAS_CONSTRUCTORS 1 |
| 89 | +#define G2_HAS_CONSTRUCTORS 1 |
78 | 90 |
|
79 | 91 | /* Pre Visual Studio 2008 must use #pragma section */
|
80 |
| -#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 |
81 |
| -#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 |
| 92 | +#define G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 |
| 93 | +#define G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 |
82 | 94 |
|
83 |
| -#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ |
| 95 | +#define G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ |
84 | 96 | section(".CRT$XCU",read)
|
85 |
| -#define G_DEFINE_CONSTRUCTOR(_func) \ |
| 97 | +#define G2_DEFINE_CONSTRUCTOR(_func) \ |
86 | 98 | static void _func(void); \
|
87 | 99 | static int _func ## _wrapper(void) { _func(); return 0; } \
|
88 | 100 | __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
|
89 | 101 |
|
90 |
| -#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ |
| 102 | +#define G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ |
91 | 103 | section(".CRT$XCU",read)
|
92 |
| -#define G_DEFINE_DESTRUCTOR(_func) \ |
| 104 | +#define G2_DEFINE_DESTRUCTOR(_func) \ |
93 | 105 | static void _func(void); \
|
94 | 106 | static int _func ## _constructor(void) { atexit (_func); return 0; } \
|
95 | 107 | __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
|
|
100 | 112 | * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
|
101 | 113 | */
|
102 | 114 |
|
103 |
| -#define G_HAS_CONSTRUCTORS 1 |
| 115 | +#define G2_HAS_CONSTRUCTORS 1 |
104 | 116 |
|
105 |
| -#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 |
106 |
| -#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 |
| 117 | +#define G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 |
| 118 | +#define G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 |
107 | 119 |
|
108 |
| -#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ |
| 120 | +#define G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ |
109 | 121 | init(_func)
|
110 |
| -#define G_DEFINE_CONSTRUCTOR(_func) \ |
| 122 | +#define G2_DEFINE_CONSTRUCTOR(_func) \ |
111 | 123 | static void _func(void);
|
112 | 124 |
|
113 |
| -#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ |
| 125 | +#define G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ |
114 | 126 | fini(_func)
|
115 |
| -#define G_DEFINE_DESTRUCTOR(_func) \ |
| 127 | +#define G2_DEFINE_DESTRUCTOR(_func) \ |
116 | 128 | static void _func(void);
|
117 | 129 |
|
118 | 130 | #else
|
|
0 commit comments