GNU libmicrohttpd  0.9.72
autoinit_funcs.h
Go to the documentation of this file.
1 /*
2  * AutoinitFuncs: Automatic Initialization and Deinitialization Functions
3  * Copyright(C) 2014-2017 Karlson2k (Evgeny Grin)
4  *
5  * This header is free software; you can redistribute it and / or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This header is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this header; if not, see
17  * <http://www.gnu.org/licenses/>.
18  */
19 
20 /*
21  General usage is simple: include this header, declare or define two
22  functions with zero parameters (void) and any return type: one for
23  initialization and one for deinitialization, add
24  _SET_INIT_AND_DEINIT_FUNCS(FuncInitName, FuncDeInitName) to the code
25  and functions will be automatically called during application startup
26  and shutdown.
27  This is useful for libraries as libraries doesn't have direct access
28  to main() functions.
29  Example:
30  -------------------------------------------------
31  #include <stdlib.h>
32  #include "autoinit_funcs.h"
33 
34  int someVar;
35  void* somePtr;
36 
37  void libInit(void)
38  {
39  someVar = 3;
40  somePtr = malloc(100);
41  }
42 
43  void libDeinit(void)
44  {
45  free(somePtr);
46  }
47 
48  _SET_INIT_AND_DEINIT_FUNCS(libInit,libDeinit);
49  -------------------------------------------------
50 
51  If initializer or deinitializer function is not needed, just define
52  it as empty function.
53 
54  This header should work with GCC, clang, MSVC (2010 or later) and
55  SunPro / Sun Studio / Oracle Solaris Studio / Oracle Developer Studio
56  compiler.
57  Supported C and C++ languages; application, static and dynamic (DLL)
58  libraries; non-optimized (Debug) and optimized (Release) compilation
59  and linking.
60 
61  For more information see header code and comments in code.
62  */
63 #ifndef AUTOINIT_FUNCS_INCLUDED
64 #define AUTOINIT_FUNCS_INCLUDED 1
65 
70 #define AUTOINIT_FUNCS_VERSION 0x01000100
71 
72 #if defined(__GNUC__)
73 /* if possible - check for supported attribute */
74 #ifdef __has_attribute
75 #if ! __has_attribute (constructor) || ! __has_attribute (destructor)
76 #define _GNUC_ATTR_CONSTR_NOT_SUPPORTED 1
77 #endif /* !__has_attribute(constructor) || !__has_attribute(destructor) */
78 #endif /* __has_attribute */
79 #endif /* __GNUC__ */
80 
81 /* "_attribute__ ((constructor))" is supported by GCC, clang and
82  Sun/Oracle compiler starting from version 12.1. */
83 #if (defined(__GNUC__) && ! defined(_GNUC_ATTR_CONSTR_NOT_SUPPORTED)) || \
84  (defined(__SUNPRO_C) && __SUNPRO_C + 0 >= 0x5100)
85 
86 #define GNUC_SET_INIT_AND_DEINIT(FI,FD) \
87  void __attribute__ ((constructor)) _GNUC_init_helper_ ## FI (void) \
88  { (void) (FI) (); } \
89  void __attribute__ ((destructor)) _GNUC_deinit_helper_ ## FD (void) \
90  { (void) (FD) (); } \
91  struct _GNUC_dummy_str_ ## FI {int i;}
92 
93 #define _SET_INIT_AND_DEINIT_FUNCS(FI,FD) GNUC_SET_INIT_AND_DEINIT (FI,FD)
94 #define _AUTOINIT_FUNCS_ARE_SUPPORTED 1
95 
96 #elif defined (_MSC_FULL_VER) && _MSC_VER + 0 >= 1600
97 
98 /* Make sure that your project/sources define:
99  _LIB if building a static library (_LIB is ignored if _CONSOLE is defined);
100  _USRDLL if building DLL-library;
101  not defined both _LIB and _USRDLL if building an application */
102 
103 /* Define AUTOINIT_FUNCS_DECLARE_STATIC_REG if you need macro declaration
104  for registering static initialization functions even if you building DLL */
105 /* Define AUTOINIT_FUNCS_FORCE_STATIC_REG if you want to set main macro
106  _SET_INIT_AND_DEINIT_FUNCS to static version even if building a DLL*/
107 
108 /* Stringify macros */
109 #define _INSTRMACRO(a) #a
110 #define _STRMACRO(a) _INSTRMACRO (a)
111 
112 #if ! defined(_USRDLL) || defined(AUTOINIT_FUNCS_DECLARE_STATIC_REG)
113 
114 /* required for atexit() */
115 #include <stdlib.h>
116 
117 /* Use "C" linkage for variable to simplify variable decoration */
118 #ifdef __cplusplus
119 #define W32_INITVARDECL extern "C"
120 #else
121 #define W32_INITVARDECL extern
122 #endif
123 
124 /* How variable is decorated by compiler */
125 #if defined(_M_X64) || defined(_M_AMD64)
126 #define W32_VARDECORPREFIX
127 #define W32_DECORVARNAME(v) v
128 #define W32_VARDECORPEFIXSTR ""
129 #elif defined(_M_IX86) || defined(_X86_)
130 #define W32_VARDECORPREFIX _
131 #define W32_DECORVARNAME(v) _ ## v
132 #define W32_VARDECORPEFIXSTR "_"
133 #else
134 #error Do not know how to decorate symbols for this architecture
135 #endif
136 
137 /* Internal variable prefix (can be any) */
138 #define W32_INITHELPERVARNAME(f) _initHelperDummy_ ## f
139 #define W32_INITHELPERVARNAMEDECORSTR(f) W32_VARDECORPEFIXSTR _STRMACRO ( \
140  W32_INITHELPERVARNAME (f))
141 
142 /* Declare section (segment), put variable pointing to init function to chosen segment,
143  force linker to include variable to avoid omitting by optimizer */
144 /* Initialization function must be declared as
145  int __cdecl FuncName(void) */
146 /* Return value is ignored for C++ initializers */
147 /* For C initializers: startup process is aborted if initializer return non-zero */
148 #define W32_FPTR_IN_SEG(S,F) \
149  __pragma (section (S,long,read)) \
150  __pragma (comment (linker, "/INCLUDE:" W32_INITHELPERVARNAMEDECORSTR (F))) \
151  W32_INITVARDECL __declspec(allocate (S))int (__cdecl * W32_INITHELPERVARNAME ( \
152  F))(void) = &F
153 
154 /* Section (segment) names for pointers to initializers */
155 #define W32_SEG_INIT_C_USER ".CRT$XCU"
156 #define W32_SEG_INIT_C_LIB ".CRT$XCL"
157 #define W32_SEG_INIT_CXX_USER ".CRT$XIU"
158 #define W32_SEG_INIT_CXX_LIB ".CRT$XIL"
159 
160 /* Declare macro for different initializers sections */
161 /* Macro can be used several times to register several initializers */
162 /* Once function is registered as initializer, it will be called automatically
163  during application startup */
164 /* "lib" initializers are called before "user" initializers */
165 /* "C" initializers are called before "C++" initializers */
166 #define W32_REG_INIT_C_USER(F) W32_FPTR_IN_SEG (W32_SEG_INIT_C_USER,F)
167 #define W32_REG_INIT_C_LIB(F) W32_FPTR_IN_SEG (W32_SEG_INIT_C_LIB,F)
168 #define W32_REG_INIT_CXX_USER(F) W32_FPTR_IN_SEG (W32_SEG_INIT_CXX_USER,F)
169 #define W32_REG_INIT_CXX_LIB(F) W32_FPTR_IN_SEG (W32_SEG_INIT_CXX_LIB,F)
170 
171 /* Choose main register macro based on language and program type */
172 /* Assuming that _LIB or _USRDLL is defined for static or DLL-library */
173 /* Macro can be used several times to register several initializers */
174 /* Once function is registered as initializer, it will be called automatically
175  during application startup */
176 /* Define AUTOINIT_FUNCS_FORCE_USER_LVL_INIT to register initializers
177  at user level even if building library */
178 #ifdef __cplusplus
179 #if ((defined(_LIB) && ! defined(_CONSOLE)) || defined(_USRDLL)) && \
180  ! defined(AUTOINIT_FUNCS_FORCE_USER_LVL_INIT)
181 #define W32_REGISTER_INIT(F) W32_REG_INIT_CXX_LIB (F)
182 #else /* ! _LIB && ! _DLL */
183 #define W32_REGISTER_INIT(F) W32_REG_INIT_CXX_USER (F)
184 #endif /* ! _LIB && ! _DLL */
185 #else /* !__cplusplus*/
186 #if ((defined(_LIB) && ! defined(_CONSOLE)) || defined(_USRDLL)) && \
187  ! defined(AUTOINIT_FUNCS_FORCE_USER_LVL_INIT)
188 #define W32_REGISTER_INIT(F) W32_REG_INIT_C_LIB (F)
189 #else /* ! _LIB && ! _DLL */
190 #define W32_REGISTER_INIT(F) W32_REG_INIT_C_USER (F)
191 #endif /* ! _LIB && ! _DLL */
192 #endif /* !__cplusplus*/
193 
194 #else /* _USRDLL */
195 
196 #ifndef WIN32_LEAN_AND_MEAN
197 #define WIN32_LEAN_AND_MEAN 1
198 #endif /* WIN32_LEAN_AND_MEAN */
199 /* Required for DllMain */
200 #include <Windows.h>
201 #endif /* _USRDLL */
202 
203 
204 #if ! defined(_USRDLL) || defined(AUTOINIT_FUNCS_FORCE_STATIC_REG)
205 #define W32_SET_INIT_AND_DEINIT(FI,FD) \
206  void __cdecl _W32_deinit_helper_ ## FD (void) \
207  { (void) (FD) (); } \
208  int __cdecl _W32_init_helper_ ## FI (void) \
209  { (void) (FI) (); atexit (_W32_deinit_helper_ ## FD); return 0; } \
210  W32_REGISTER_INIT (_W32_init_helper_ ## FI)
211 #else /* _USRDLL */
212 
213 /* If DllMain is already present in code, define AUTOINIT_FUNCS_CALL_USR_DLLMAIN
214  and rename DllMain to usr_DllMain */
215 #ifndef AUTOINIT_FUNCS_CALL_USR_DLLMAIN
216 #define W32_SET_INIT_AND_DEINIT(FI,FD) \
217  BOOL WINAPI DllMain (HINSTANCE hinst,DWORD reason,LPVOID unused) \
218  { if (DLL_PROCESS_ATTACH==reason) {(void) (FI) ();} \
219  else if (DLL_PROCESS_DETACH==reason) {(void) (FD) ();} \
220  return TRUE; \
221  } struct _W32_dummy_strc_ ## FI {int i;}
222 #else /* AUTOINIT_FUNCS_CALL_USR_DLLMAIN */
223 #define W32_SET_INIT_AND_DEINIT(FI,FD) \
224  BOOL WINAPI usr_DllMain (HINSTANCE hinst,DWORD reason,LPVOID unused); \
225  BOOL WINAPI DllMain (HINSTANCE hinst,DWORD reason,LPVOID unused) \
226  { if (DLL_PROCESS_ATTACH==reason) {(void) (FI) ();} \
227  else if (DLL_PROCESS_DETACH==reason) {(void) (FD) ();} \
228  return usr_DllMain (hinst,reason,unused); \
229  } struct _W32_dummy_strc_ ## FI {int i;}
230 #endif /* AUTOINIT_FUNCS_CALL_USR_DLLMAIN */
231 #endif /* _USRDLL */
232 
233 #define _SET_INIT_AND_DEINIT_FUNCS(FI,FD) W32_SET_INIT_AND_DEINIT (FI,FD)
234 /* Indicate that automatic initializers/deinitializers are supported */
235 #define _AUTOINIT_FUNCS_ARE_SUPPORTED 1
236 
237 #else /* !__GNUC__ && !_MSC_FULL_VER */
238 
239 /* Define EMIT_ERROR_IF_AUTOINIT_FUNCS_ARE_NOT_SUPPORTED before inclusion of header to
240  abort compilation if automatic initializers/deinitializers are not supported */
241 #ifdef EMIT_ERROR_IF_AUTOINIT_FUNCS_ARE_NOT_SUPPORTED
242 #error \
243  Compiler/platform don not support automatic calls of user-defined initializer and deinitializer
244 #endif /* EMIT_ERROR_IF_AUTOINIT_FUNCS_ARE_NOT_SUPPORTED */
245 
246 /* Do nothing */
247 #define _SET_INIT_AND_DEINIT_FUNCS(FI,FD)
248 /* Indicate that automatic initializers/deinitializers are not supported */
249 #define _AUTOINIT_FUNCS_ARE_NOT_SUPPORTED 1
250 
251 #endif /* !__GNUC__ && !_MSC_FULL_VER */
252 #endif /* !AUTOINIT_FUNCS_INCLUDED */