2008-11-04 Anders Carlsson <andersca@apple.com>
[webkit/qt.git] / WebCore / platform / StaticConstructors.h
blob26b44de32ed2e633f55d0cd4d1dca7dba303248e
1 /*
2 * This file is part of the DOM implementation for KDE.
4 * Copyright (C) 2006 Apple Computer, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 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 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
23 #ifndef StaticConstructors_h
24 #define StaticConstructors_h
26 // For WebCore we need to avoid having static constructors. We achieve this
27 // with two separate methods for GCC and MSVC. Both methods prevent the static
28 // initializers from being registered and called on program startup. On GCC, we
29 // declare the global objects with a different type that can be POD default
30 // initialized by the linker/loader. On MSVC we use a special compiler feature
31 // to have the CRT ignore our static initializers. The constructors will never
32 // be called and the objects will be left uninitialized.
34 // With both of these approaches, we must define and explicitly call an init
35 // routine that uses placement new to create the objects and overwrite the
36 // uninitialized placeholders.
38 // This is not completely portable, but is what we have for now without
39 // changing how a lot of code accesses these global objects.
41 #ifdef SKIP_STATIC_CONSTRUCTORS_ON_MSVC
42 // - Assume that all includes of this header want ALL of their static
43 // initializers ignored. This is currently the case. This means that if
44 // a .cc includes this header (or it somehow gets included), all static
45 // initializers after the include will not be executed.
46 // - We do this with a pragma, so that all of the static initializer pointers
47 // go into our own section, and the CRT won't call them. Eventually it would
48 // be nice if the section was discarded, because we don't want the pointers.
49 // See: http://msdn.microsoft.com/en-us/library/7977wcck(VS.80).aspx
50 #pragma warning(disable:4075)
51 #pragma init_seg(".unwantedstaticinits")
52 #endif
54 #ifndef SKIP_STATIC_CONSTRUCTORS_ON_GCC
55 // Define an global in the normal way.
56 #if COMPILER(MSVC7)
57 #define DEFINE_GLOBAL(type, name) \
58 const type name;
59 #else
60 #define DEFINE_GLOBAL(type, name, ...) \
61 const type name;
62 #endif
64 #else
65 // Define an correctly-sized array of pointers to avoid static initialization.
66 // Use an array of pointers instead of an array of char in case there is some alignment issue.
67 #if COMPILER(MSVC7)
68 #define DEFINE_GLOBAL(type, name) \
69 void * name[(sizeof(type) + sizeof(void *) - 1) / sizeof(void *)];
70 #else
71 #define DEFINE_GLOBAL(type, name, ...) \
72 void * name[(sizeof(type) + sizeof(void *) - 1) / sizeof(void *)];
73 #endif
74 #endif
76 #endif // StaticConstructors_h