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)
60 * Only try to include gslist.h if not already included via glib.h,
61 * so that items using gconstructor.h outside of GLib (such as
62 * GResources) continue to build properly.
70 #define G_HAS_CONSTRUCTORS 1
72 /* We do some weird things to avoid the constructors being optimized
73 * away on VS2015 if WholeProgramOptimization is enabled. First we
74 * make a reference to the array from the wrapper to make sure its
75 * references. Then we use a pragma to make sure the wrapper function
76 * symbol is always included at the link stage. Also, the symbols
77 * need to be extern (but not dllexport), even though they are not
78 * really used from another object file.
81 /* We need to account for differences between the mangling of symbols
82 * for x86 and x64/ARM/ARM64 programs, as symbols on x86 are prefixed
83 * with an underscore but symbols on x64/ARM/ARM64 are not.
86 #define G_MSVC_SYMBOL_PREFIX "_"
88 #define G_MSVC_SYMBOL_PREFIX ""
91 #define G_DEFINE_CONSTRUCTOR(_func) G_MSVC_CTOR (_func, G_MSVC_SYMBOL_PREFIX)
92 #define G_DEFINE_DESTRUCTOR(_func) G_MSVC_DTOR (_func, G_MSVC_SYMBOL_PREFIX)
94 #define G_MSVC_CTOR(_func,_sym_prefix) \
95 static void _func(void); \
96 extern int (* _array ## _func)(void); \
97 int _func ## _wrapper(void); \
98 int _func ## _wrapper(void) { _func(); g_slist_find (NULL, _array ## _func); return 0; } \
99 __pragma(comment(linker,"/include:" _sym_prefix # _func "_wrapper")) \
100 __pragma(section(".CRT$XCU",read)) \
101 __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _wrapper;
103 #define G_MSVC_DTOR(_func,_sym_prefix) \
104 static void _func(void); \
105 extern int (* _array ## _func)(void); \
106 int _func ## _constructor(void); \
107 int _func ## _constructor(void) { atexit (_func); g_slist_find (NULL, _array ## _func); return 0; } \
108 __pragma(comment(linker,"/include:" _sym_prefix # _func "_constructor")) \
109 __pragma(section(".CRT$XCU",read)) \
110 __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _constructor;
112 #elif defined(__SUNPRO_C)
114 /* This is not tested, but i believe it should work, based on:
115 * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
118 #define G_HAS_CONSTRUCTORS 1
120 #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
121 #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
123 #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
125 #define G_DEFINE_CONSTRUCTOR(_func) \
126 static void _func(void);
128 #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
130 #define G_DEFINE_DESTRUCTOR(_func) \
131 static void _func(void);
135 /* constructors not supported for this compiler */
139 #endif /* __GTK_DOC_IGNORE__ */
140 #endif /* __G_CONSTRUCTOR_H__ */