|  | 
| 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(G2_FUNCNAME(my_constructor)) | 
| 12 | 12 |   #endif | 
| 13 |  | -  G_DEFINE_CONSTRUCTOR(my_constructor) | 
| 14 |  | -  static void my_constructor(void) { | 
|  | 13 | +  G2_DEFINE_CONSTRUCTOR(G2_FUNCNAME(my_constructor)) | 
|  | 14 | +  static void G2_FUNCNAME(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 | 
|  | 38 | + | 
|  | 39 | +#define G2_DEFINE_CONSTRUCTOR(_func) G2_CXX_CTOR(_func) | 
|  | 40 | +#define G2_DEFINE_DESTRUCTOR(_func) G2_CXX_DTOR(_func) | 
| 26 | 41 | 
 | 
| 27 |  | -#define G_DEFINE_CONSTRUCTOR(_func) \ | 
|  | 42 | +#define G2_CXX_CTOR(_func) \ | 
| 28 | 43 |   static void _func(void); \ | 
| 29 | 44 |   struct _func ## _wrapper_struct { _func ## _wrapper_struct() { _func(); } }; \ | 
| 30 | 45 |   static _func ## _wrapper_struct _func ## _wrapper; | 
| 31 | 46 | 
 | 
| 32 |  | -#define G_DEFINE_DESTRUCTOR(_func) \ | 
|  | 47 | +#define G2_CXX_DTOR(_func) \ | 
| 33 | 48 |   static void _func(void); \ | 
| 34 | 49 |   struct _func ## _wrapper_struct2 { ~_func ## _wrapper_struct2() { _func(); } }; \ | 
| 35 | 50 |   static _func ## _wrapper_struct2 _func ## _wrapper2; | 
| 36 | 51 | 
 | 
| 37 | 52 | #elif (defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7))) || \ | 
| 38 | 53 |        defined(__clang__) | 
| 39 | 54 | 
 | 
| 40 |  | -#define G_HAS_CONSTRUCTORS 1 | 
|  | 55 | +#define G2_HAS_CONSTRUCTORS 1 | 
| 41 | 56 | 
 | 
| 42 |  | -#define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void); | 
| 43 |  | -#define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void); | 
|  | 57 | +#define G2_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void); | 
|  | 58 | +#define G2_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void); | 
| 44 | 59 | 
 | 
| 45 | 60 | #elif defined(_MSC_VER) && (_MSC_VER >= 1500) | 
| 46 | 61 | /* Visual Studio 2008 and later has _pragma */ | 
| 47 | 62 | 
 | 
| 48 |  | -#define G_HAS_CONSTRUCTORS 1 | 
|  | 63 | +#define G2_HAS_CONSTRUCTORS 1 | 
| 49 | 64 | 
 | 
| 50 | 65 | #ifdef _WIN64 | 
| 51 |  | -#define G_MSVC_SYMBOL_PREFIX "" | 
|  | 66 | +#define G2_MSVC_SYMBOL_PREFIX "" | 
| 52 | 67 | #else | 
| 53 |  | -#define G_MSVC_SYMBOL_PREFIX "_" | 
|  | 68 | +#define G2_MSVC_SYMBOL_PREFIX "_" | 
| 54 | 69 | #endif | 
| 55 | 70 | 
 | 
| 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) | 
|  | 71 | +#define G2_DEFINE_CONSTRUCTOR(_func) G2_MSVC_CTOR(_func, G2_MSVC_SYMBOL_PREFIX) | 
|  | 72 | +#define G2_DEFINE_DESTRUCTOR(_func) G2_MSVC_DTOR(_func, G2_MSVC_SYMBOL_PREFIX) | 
| 58 | 73 | 
 | 
| 59 |  | -#define G_MSVC_CTOR(_func,_sym_prefix) \ | 
|  | 74 | +#define G2_MSVC_CTOR(_func, _sym_prefix) \ | 
| 60 | 75 |   static void _func(void); \ | 
| 61 | 76 |   extern int (* _array ## _func)(void); \ | 
| 62 | 77 |   int _func ## _wrapper(void) { _func(); return _array ## _func == NULL; } \ | 
| 63 | 78 |   __pragma(comment(linker,"/include:" _sym_prefix # _func "_wrapper")) \ | 
| 64 | 79 |   __pragma(section(".CRT$XCU",read)) \ | 
| 65 | 80 |   __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _wrapper; | 
| 66 | 81 | 
 | 
| 67 |  | -#define G_MSVC_DTOR(_func,_sym_prefix) \ | 
|  | 82 | +#define G2_MSVC_DTOR(_func, _sym_prefix) \ | 
| 68 | 83 |   static void _func(void); \ | 
| 69 | 84 |   extern int (* _array ## _func)(void); \ | 
| 70 | 85 |   int _func ## _constructor(void) { atexit (_func); return _array ## _func == NULL; } \ | 
|  | 
| 74 | 89 | 
 | 
| 75 | 90 | #elif defined(_MSC_VER) && (_MSC_VER >= 1400) | 
| 76 | 91 | 
 | 
| 77 |  | -#define G_HAS_CONSTRUCTORS 1 | 
|  | 92 | +#define G2_HAS_CONSTRUCTORS 1 | 
| 78 | 93 | 
 | 
| 79 | 94 | /* Pre Visual Studio 2008 must use #pragma section */ | 
| 80 |  | -#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 | 
| 81 |  | -#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 | 
|  | 95 | +#define G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 | 
|  | 96 | +#define G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 | 
| 82 | 97 | 
 | 
| 83 |  | -#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ | 
|  | 98 | +#define G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ | 
| 84 | 99 |   section(".CRT$XCU",read) | 
| 85 |  | -#define G_DEFINE_CONSTRUCTOR(_func) \ | 
|  | 100 | +#define G2_DEFINE_CONSTRUCTOR(_func) \ | 
| 86 | 101 |   static void _func(void); \ | 
| 87 | 102 |   static int _func ## _wrapper(void) { _func(); return 0; } \ | 
| 88 | 103 |   __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper; | 
| 89 | 104 | 
 | 
| 90 |  | -#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ | 
|  | 105 | +#define G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ | 
| 91 | 106 |   section(".CRT$XCU",read) | 
| 92 |  | -#define G_DEFINE_DESTRUCTOR(_func) \ | 
|  | 107 | +#define G2_DEFINE_DESTRUCTOR(_func) \ | 
| 93 | 108 |   static void _func(void); \ | 
| 94 | 109 |   static int _func ## _constructor(void) { atexit (_func); return 0; } \ | 
| 95 | 110 |   __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor; | 
|  | 
| 100 | 115 |  * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c | 
| 101 | 116 |  */ | 
| 102 | 117 | 
 | 
| 103 |  | -#define G_HAS_CONSTRUCTORS 1 | 
|  | 118 | +#define G2_HAS_CONSTRUCTORS 1 | 
| 104 | 119 | 
 | 
| 105 |  | -#define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 | 
| 106 |  | -#define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 | 
|  | 120 | +#define G2_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1 | 
|  | 121 | +#define G2_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1 | 
| 107 | 122 | 
 | 
| 108 |  | -#define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ | 
|  | 123 | +#define G2_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \ | 
| 109 | 124 |   init(_func) | 
| 110 |  | -#define G_DEFINE_CONSTRUCTOR(_func) \ | 
|  | 125 | +#define G2_DEFINE_CONSTRUCTOR(_func) \ | 
| 111 | 126 |   static void _func(void); | 
| 112 | 127 | 
 | 
| 113 |  | -#define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ | 
|  | 128 | +#define G2_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \ | 
| 114 | 129 |   fini(_func) | 
| 115 |  | -#define G_DEFINE_DESTRUCTOR(_func) \ | 
|  | 130 | +#define G2_DEFINE_DESTRUCTOR(_func) \ | 
| 116 | 131 |   static void _func(void); | 
| 117 | 132 | 
 | 
| 118 | 133 | #else | 
|  | 
0 commit comments