1760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org/*
2760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * Copyright 2010 Luca Barbieri
3760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org *
4760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * Permission is hereby granted, free of charge, to any person obtaining
5760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * a copy of this software and associated documentation files (the
6760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * "Software"), to deal in the Software without restriction, including
7760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * without limitation the rights to use, copy, modify, merge, publish,
8760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * distribute, sublicense, and/or sell copies of the Software, and to
9760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * permit persons to whom the Software is furnished to do so, subject to
10760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * the following conditions:
11760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org *
12760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * The above copyright notice and this permission notice (including the
13760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * next paragraph) shall be included in all copies or substantial
14760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * portions of the Software.
15760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org *
16760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
20760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org *
24760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org **************************************************************************/
25760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org
26760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org#ifndef U_INIT_H
27760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org#define U_INIT_H
28760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org
29760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org/* Use UTIL_INIT(f) to have f called at program initialization.
30760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org   Note that it is only guaranteed to be called if any symbol in the
31760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org   .c file it is in sis referenced by the program.
32760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org
33760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org   UTIL_INIT functions are called in arbitrary order.
34760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org*/
35760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org
36760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org#ifdef __cplusplus
37760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org/* use a C++ global constructor */
38760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org#define UTIL_INIT(f) struct f##__gctor_t {f##__gctor_t() {x();}} f##__gctor;
39760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org#elif defined(_MSC_VER)
40760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org/* add a pointer to the section where MSVC stores global constructor pointers */
41760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org/* see http://blogs.msdn.com/vcblog/archive/2006/10/20/crt-initialization.aspx and
42760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org   http://stackoverflow.com/questions/1113409/attribute-constructor-equivalent-in-vc */
43760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org#pragma section(".CRT$XCU",read)
44760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org#define UTIL_INIT(f) static void __cdecl f##__init(void) {f();}; __declspec(allocate(".CRT$XCU")) void (__cdecl* f##__xcu)(void) = f##__init;
45760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org#elif defined(__GNUC__)
46760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org#define UTIL_INIT(f) static void f##__init(void) __attribute__((constructor)); static void f##__init(void) {f();}
47760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org#else
48760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org#error Unsupported compiler: please find out how to implement global initializers in C on it
49760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org#endif
50760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org
51760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org#endif
52760fd893ba809a7a5daa25c2749ff502f7186e83kbr@chromium.org
53