StaticConstructors.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. #ifndef StaticConstructors_h
  21. #define StaticConstructors_h
  22. // We need to avoid having static constructors. We achieve this
  23. // with two separate methods for GCC and MSVC. Both methods prevent the static
  24. // initializers from being registered and called on program startup. On GCC, we
  25. // declare the global objects with a different type that can be POD default
  26. // initialized by the linker/loader. On MSVC we use a special compiler feature
  27. // to have the CRT ignore our static initializers. The constructors will never
  28. // be called and the objects will be left uninitialized.
  29. //
  30. // With both of these approaches, we must define and explicitly call an init
  31. // routine that uses placement new to create the objects and overwrite the
  32. // uninitialized placeholders.
  33. //
  34. // This is not completely portable, but is what we have for now without
  35. // changing how a lot of code accesses these global objects.
  36. #ifdef SKIP_STATIC_CONSTRUCTORS_ON_MSVC
  37. // - Assume that all includes of this header want ALL of their static
  38. // initializers ignored. This is currently the case. This means that if
  39. // a .cc includes this header (or it somehow gets included), all static
  40. // initializers after the include will not be executed.
  41. // - We do this with a pragma, so that all of the static initializer pointers
  42. // go into our own section, and the CRT won't call them. Eventually it would
  43. // be nice if the section was discarded, because we don't want the pointers.
  44. // See: http://msdn.microsoft.com/en-us/library/7977wcck(VS.80).aspx
  45. #pragma warning(disable:4075)
  46. #pragma init_seg(".unwantedstaticinits")
  47. #endif
  48. #ifndef SKIP_STATIC_CONSTRUCTORS_ON_GCC
  49. // Define an global in the normal way.
  50. #define DEFINE_GLOBAL(type, name, ...) \
  51. const type name;
  52. #else
  53. // Define an correctly-sized array of pointers to avoid static initialization.
  54. // Use an array of pointers instead of an array of char in case there is some alignment issue.
  55. #define DEFINE_GLOBAL(type, name, ...) \
  56. void * name[(sizeof(type) + sizeof(void *) - 1) / sizeof(void *)];
  57. #endif
  58. #endif // StaticConstructors_h