GNU libmicrohttpd  0.9.59
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(W32_INITHELPERVARNAME(f))
140 
141 /* Declare section (segment), put variable pointing to init function to chosen segment,
142  force linker to include variable to avoid omitting by optimizer */
143 /* Initialization function must be declared as
144  int __cdecl FuncName(void) */
145 /* Return value is ignored for C++ initializers */
146 /* For C initializers: startup process is aborted if initializer return non-zero */
147 #define W32_FPTR_IN_SEG(S,F) \
148  __pragma(section(S,long,read)) \
149  __pragma(comment(linker, "/INCLUDE:" W32_INITHELPERVARNAMEDECORSTR(F))) \
150  W32_INITVARDECL __declspec(allocate(S)) int(__cdecl *W32_INITHELPERVARNAME(F))(void) = &F
151 
152 /* Section (segment) names for pointers to initializers */
153 #define W32_SEG_INIT_C_USER ".CRT$XCU"
154 #define W32_SEG_INIT_C_LIB ".CRT$XCL"
155 #define W32_SEG_INIT_CXX_USER ".CRT$XIU"
156 #define W32_SEG_INIT_CXX_LIB ".CRT$XIL"
157 
158 /* Declare macro for different initializers sections */
159 /* Macro can be used several times to register several initializers */
160 /* Once function is registered as initializer, it will be called automatically
161  during application startup */
162 /* "lib" initializers are called before "user" initializers */
163 /* "C" initializers are called before "C++" initializers */
164 #define W32_REG_INIT_C_USER(F) W32_FPTR_IN_SEG(W32_SEG_INIT_C_USER,F)
165 #define W32_REG_INIT_C_LIB(F) W32_FPTR_IN_SEG(W32_SEG_INIT_C_LIB,F)
166 #define W32_REG_INIT_CXX_USER(F) W32_FPTR_IN_SEG(W32_SEG_INIT_CXX_USER,F)
167 #define W32_REG_INIT_CXX_LIB(F) W32_FPTR_IN_SEG(W32_SEG_INIT_CXX_LIB,F)
168 
169 /* Choose main register macro based on language and program type */
170 /* Assuming that _LIB or _USRDLL is defined for static or DLL-library */
171 /* Macro can be used several times to register several initializers */
172 /* Once function is registered as initializer, it will be called automatically
173  during application startup */
174 /* Define AUTOINIT_FUNCS_FORCE_USER_LVL_INIT to register initializers
175  at user level even if building library */
176 #ifdef __cplusplus
177 #if ((defined(_LIB) && !defined(_CONSOLE)) || defined(_USRDLL)) && !defined(AUTOINIT_FUNCS_FORCE_USER_LVL_INIT)
178 #define W32_REGISTER_INIT(F) W32_REG_INIT_CXX_LIB(F)
179 #else /* ! _LIB && ! _DLL */
180 #define W32_REGISTER_INIT(F) W32_REG_INIT_CXX_USER(F)
181 #endif /* ! _LIB && ! _DLL */
182 #else /* !__cplusplus*/
183 #if ((defined(_LIB) && !defined(_CONSOLE)) || defined(_USRDLL)) && !defined(AUTOINIT_FUNCS_FORCE_USER_LVL_INIT)
184 #define W32_REGISTER_INIT(F) W32_REG_INIT_C_LIB(F)
185 #else /* ! _LIB && ! _DLL */
186 #define W32_REGISTER_INIT(F) W32_REG_INIT_C_USER(F)
187 #endif /* ! _LIB && ! _DLL */
188 #endif /* !__cplusplus*/
189 
190 #else /* _USRDLL */
191 
192 #ifndef WIN32_LEAN_AND_MEAN
193 #define WIN32_LEAN_AND_MEAN 1
194 #endif /* WIN32_LEAN_AND_MEAN */
195 /* Required for DllMain */
196 #include <Windows.h>
197 #endif /* _USRDLL */
198 
199 
200 #if !defined(_USRDLL) || defined(AUTOINIT_FUNCS_FORCE_STATIC_REG)
201 #define W32_SET_INIT_AND_DEINIT(FI,FD) \
202  void __cdecl _W32_deinit_helper_##FD(void) \
203  { (void)(FD)(); } \
204  int __cdecl _W32_init_helper_##FI(void) \
205  { (void)(FI)(); atexit(_W32_deinit_helper_##FD); return 0; } \
206  W32_REGISTER_INIT(_W32_init_helper_##FI)
207 #else /* _USRDLL */
208 
209 /* If DllMain is already present in code, define AUTOINIT_FUNCS_CALL_USR_DLLMAIN
210  and rename DllMain to usr_DllMain */
211 #ifndef AUTOINIT_FUNCS_CALL_USR_DLLMAIN
212 #define W32_SET_INIT_AND_DEINIT(FI,FD) \
213  BOOL WINAPI DllMain(HINSTANCE hinst,DWORD reason,LPVOID unused) \
214  { if(DLL_PROCESS_ATTACH==reason) {(void)(FI)();} \
215  else if(DLL_PROCESS_DETACH==reason) {(void)(FD)();} \
216  return TRUE; \
217  } struct _W32_dummy_strc_##FI{int i;}
218 #else /* AUTOINIT_FUNCS_CALL_USR_DLLMAIN */
219 #define W32_SET_INIT_AND_DEINIT(FI,FD) \
220  BOOL WINAPI usr_DllMain(HINSTANCE hinst,DWORD reason,LPVOID unused); \
221  BOOL WINAPI DllMain(HINSTANCE hinst,DWORD reason,LPVOID unused) \
222  { if(DLL_PROCESS_ATTACH==reason) {(void)(FI)();} \
223  else if(DLL_PROCESS_DETACH==reason) {(void)(FD)();} \
224  return usr_DllMain(hinst,reason,unused); \
225  } struct _W32_dummy_strc_##FI{int i;}
226 #endif /* AUTOINIT_FUNCS_CALL_USR_DLLMAIN */
227 #endif /* _USRDLL */
228 
229 #define _SET_INIT_AND_DEINIT_FUNCS(FI,FD) W32_SET_INIT_AND_DEINIT(FI,FD)
230 /* Indicate that automatic initializers/deinitializers are supported */
231 #define _AUTOINIT_FUNCS_ARE_SUPPORTED 1
232 
233 #else /* !__GNUC__ && !_MSC_FULL_VER */
234 
235 /* Define EMIT_ERROR_IF_AUTOINIT_FUNCS_ARE_NOT_SUPPORTED before inclusion of header to
236  abort compilation if automatic initializers/deinitializers are not supported */
237 #ifdef EMIT_ERROR_IF_AUTOINIT_FUNCS_ARE_NOT_SUPPORTED
238 #error Compiler/platform don not support automatic calls of user-defined initializer and deinitializer
239 #endif /* EMIT_ERROR_IF_AUTOINIT_FUNCS_ARE_NOT_SUPPORTED */
240 
241 /* Do nothing */
242 #define _SET_INIT_AND_DEINIT_FUNCS(FI,FD)
243 /* Indicate that automatic initializers/deinitializers are not supported */
244 #define _AUTOINIT_FUNCS_ARE_NOT_SUPPORTED 1
245 
246 #endif /* !__GNUC__ && !_MSC_FULL_VER */
247 #endif /* !AUTOINIT_FUNCS_INCLUDED */