Home | History | Annotate | Download | only in wtf
      1 /*
      2  * Copyright (C) 2006 Apple Computer, Inc.
      3  *
      4  * This library is free software; you can redistribute it and/or
      5  * modify it under the terms of the GNU Library General Public
      6  * License as published by the Free Software Foundation; either
      7  * version 2 of the License, or (at your option) any later version.
      8  *
      9  * This library is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     12  * Library General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU Library General Public License
     15  * along with this library; see the file COPYING.LIB.  If not, write to
     16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     17  * Boston, MA 02110-1301, USA.
     18  *
     19  */
     20 
     21 #ifndef StaticConstructors_h
     22 #define StaticConstructors_h
     23 
     24 // We need to avoid having static constructors. We achieve this
     25 // with two separate methods for GCC and MSVC. Both methods prevent the static
     26 // initializers from being registered and called on program startup. On GCC, we
     27 // declare the global objects with a different type that can be POD default
     28 // initialized by the linker/loader. On MSVC we use a special compiler feature
     29 // to have the CRT ignore our static initializers. The constructors will never
     30 // be called and the objects will be left uninitialized.
     31 //
     32 // With both of these approaches, we must define and explicitly call an init
     33 // routine that uses placement new to create the objects and overwrite the
     34 // uninitialized placeholders.
     35 //
     36 // This is not completely portable, but is what we have for now without
     37 // changing how a lot of code accesses these global objects.
     38 
     39 #ifdef SKIP_STATIC_CONSTRUCTORS_ON_MSVC
     40 // - Assume that all includes of this header want ALL of their static
     41 //   initializers ignored. This is currently the case. This means that if
     42 //   a .cc includes this header (or it somehow gets included), all static
     43 //   initializers after the include will not be executed.
     44 // - We do this with a pragma, so that all of the static initializer pointers
     45 //   go into our own section, and the CRT won't call them. Eventually it would
     46 //   be nice if the section was discarded, because we don't want the pointers.
     47 //   See: http://msdn.microsoft.com/en-us/library/7977wcck(VS.80).aspx
     48 #pragma warning(disable:4075)
     49 #pragma init_seg(".unwantedstaticinits")
     50 #endif
     51 
     52 #ifndef SKIP_STATIC_CONSTRUCTORS_ON_GCC
     53     // Define an global in the normal way.
     54 #if COMPILER(MSVC7_OR_LOWER)
     55 #define DEFINE_GLOBAL(type, name) \
     56     const type name;
     57 #elif COMPILER(WINSCW)
     58 #define DEFINE_GLOBAL(type, name, arg...) \
     59     const type name;
     60 #else
     61 #define DEFINE_GLOBAL(type, name, ...) \
     62     const type name;
     63 #endif
     64 
     65 #else
     66 // Define an correctly-sized array of pointers to avoid static initialization.
     67 // Use an array of pointers instead of an array of char in case there is some alignment issue.
     68 #if COMPILER(MSVC7_OR_LOWER)
     69 #define DEFINE_GLOBAL(type, name) \
     70     void * name[(sizeof(type) + sizeof(void *) - 1) / sizeof(void *)];
     71 #elif COMPILER(WINSCW)
     72 #define DEFINE_GLOBAL(type, name, arg...) \
     73     void * name[(sizeof(type) + sizeof(void *) - 1) / sizeof(void *)];
     74 #else
     75 #define DEFINE_GLOBAL(type, name, ...) \
     76     void * name[(sizeof(type) + sizeof(void *) - 1) / sizeof(void *)];
     77 #endif
     78 #endif
     79 
     80 #endif // StaticConstructors_h
     81