1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
27 #ifndef __G_CONSTRUCTOR_H__
28 #define __G_CONSTRUCTOR_H__
31 If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
32 destructors, in a usable way, including e.g. on library unload. If not you're on
35 Some compilers need #pragma to handle this, which does not work with macros,
36 so the way you need to use this is (for constructors):
38 #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
39 #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
41 G_DEFINE_CONSTRUCTOR(my_constructor)
42 static void my_constructor(void) {
48 #ifndef __GTK_DOC_IGNORE__
50 #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
52 #define G_HAS_CONSTRUCTORS 1
54 #define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
55 #define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
57 #elif defined (_MSC_VER) && (_MSC_VER >= 1500)
58 /* Visual studio 2008 and later has _Pragma */
61 * Only try to include gslist.h if not already included via glib.h,
62 * so that items using gconstructor.h outside of GLib (such as
63 * GResources) continue to build properly.
71 #define G_HAS_CONSTRUCTORS 1
73 /* We do some weird things to avoid the constructors being optimized
74 * away on VS2015 if WholeProgramOptimization is enabled. First we
75 * make a reference to the array from the wrapper to make sure its
76 * references. Then we use a pragma to make sure the wrapper function
77 * symbol is always included at the link stage. Also, the symbols
78 * need to be extern (but not dllexport), even though they are not
79 * really used from another object file.
82 /* We need to account for differences between the mangling of symbols
83 * for x86 and x64/ARM/ARM64 programs, as symbols on x86 are prefixed
84 * with an underscore but symbols on x64/ARM/ARM64 are not.
87 #define G_MSVC_SYMBOL_PREFIX "_"
89 #define G_MSVC_SYMBOL_PREFIX ""
92 #define G_DEFINE_CONSTRUCTOR(_func) G_MSVC_CTOR (_func, G_MSVC_SYMBOL_PREFIX)
93 #define G_DEFINE_DESTRUCTOR(_func) G_MSVC_DTOR (_func, G_MSVC_SYMBOL_PREFIX)
95 #define G_MSVC_CTOR(_func,_sym_prefix) \
96 static void _func(void); \
97 extern int (* _array ## _func)(void); \
98 int _func ## _wrapper(void); \
99 int _func ## _wrapper(void) { _func(); g_slist_find (NULL, _array ## _func); return 0; } \
100 __pragma(comment(linker,"/include:" _sym_prefix # _func "_wrapper")) \
101 __pragma(section(".CRT$XCU",read)) \
102 __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _wrapper;
104 #define G_MSVC_DTOR(_func,_sym_prefix) \
105 static void _func(void); \
106 extern int (* _array ## _func)(void); \
107 int _func ## _constructor(void); \
108 int _func ## _constructor(void) { atexit (_func); g_slist_find (NULL, _array ## _func); return 0; } \
109 __pragma(comment(linker,"/include:" _sym_prefix # _func "_constructor")) \
110 __pragma(section(".CRT$XCU",read)) \
111 __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _constructor;
113 #elif defined (_MSC_VER)
115 #define G_HAS_CONSTRUCTORS 1
117 /* Pre Visual studio 2008 must use #pragma section */
118 #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
119 #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
121 #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
122 section(".CRT$XCU",read)
123 #define G_DEFINE_CONSTRUCTOR(_func) \
124 static void _func(void); \
125 static int _func ## _wrapper(void) { _func(); return 0; } \
126 __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
128 #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
129 section(".CRT$XCU",read)
130 #define G_DEFINE_DESTRUCTOR(_func) \
131 static void _func(void); \
132 static int _func ## _constructor(void) { atexit (_func); return 0; } \
133 __declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
135 #elif defined(__SUNPRO_C)
137 /* This is not tested, but i believe it should work, based on:
138 * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
141 #define G_HAS_CONSTRUCTORS 1
143 #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
144 #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
146 #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
148 #define G_DEFINE_CONSTRUCTOR(_func) \
149 static void _func(void);
151 #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
153 #define G_DEFINE_DESTRUCTOR(_func) \
154 static void _func(void);
158 /* constructors not supported for this compiler */
162 #endif /* __GTK_DOC_IGNORE__ */
163 #endif /* __G_CONSTRUCTOR_H__ */