Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright 2010 Luca Barbieri
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining
      5  * a copy of this software and associated documentation files (the
      6  * "Software"), to deal in the Software without restriction, including
      7  * without limitation the rights to use, copy, modify, merge, publish,
      8  * distribute, sublicense, and/or sell copies of the Software, and to
      9  * permit persons to whom the Software is furnished to do so, subject to
     10  * the following conditions:
     11  *
     12  * The above copyright notice and this permission notice (including the
     13  * next paragraph) shall be included in all copies or substantial
     14  * portions of the Software.
     15  *
     16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
     17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
     18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
     19  * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
     20  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
     21  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
     22  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     23  *
     24  **************************************************************************/
     25 
     26 #ifndef U_INIT_H
     27 #define U_INIT_H
     28 
     29 /* Use UTIL_INIT(f) to have f called at program initialization.
     30    Note that it is only guaranteed to be called if any symbol in the
     31    .c file it is in sis referenced by the program.
     32 
     33    UTIL_INIT functions are called in arbitrary order.
     34 */
     35 
     36 #ifdef __cplusplus
     37 /* use a C++ global constructor */
     38 #define UTIL_INIT(f) struct f##__gctor_t {f##__gctor_t() {x();}} f##__gctor;
     39 #elif defined(_MSC_VER)
     40 /* add a pointer to the section where MSVC stores global constructor pointers */
     41 /* see http://blogs.msdn.com/vcblog/archive/2006/10/20/crt-initialization.aspx and
     42    http://stackoverflow.com/questions/1113409/attribute-constructor-equivalent-in-vc */
     43 #pragma section(".CRT$XCU",read)
     44 #define UTIL_INIT(f) static void __cdecl f##__init(void) {f();}; __declspec(allocate(".CRT$XCU")) void (__cdecl* f##__xcu)(void) = f##__init;
     45 #elif defined(__GNUC__)
     46 #define UTIL_INIT(f) static void f##__init(void) __attribute__((constructor)); static void f##__init(void) {f();}
     47 #else
     48 #error Unsupported compiler: please find out how to implement global initializers in C on it
     49 #endif
     50 
     51 #endif
     52 
     53