(root)/
glib-2.79.0/
glib/
gconstructor.h
       1  /* GLIB - Library of useful routines for C programming
       2   * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
       3   *
       4   * SPDX-License-Identifier: LGPL-2.1-or-later
       5   *
       6   * This library is free software; you can redistribute it and/or
       7   * modify it under the terms of the GNU Lesser General Public
       8   * License as published by the Free Software Foundation; either
       9   * version 2.1 of the License, or (at your option) any later version.
      10   *
      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   * Lesser General Public License for more details.
      15   *
      16   * You should have received a copy of the GNU Lesser General Public
      17   * License along with this library; if not, see <http://www.gnu.org/licenses/>.
      18   */
      19  
      20  /*
      21   * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
      22   * file for a list of people on the GLib Team.  See the ChangeLog
      23   * files for a list of changes.  These files are distributed with
      24   * GLib at ftp://ftp.gtk.org/pub/gtk/.
      25   */
      26  
      27  #ifndef __G_CONSTRUCTOR_H__
      28  #define __G_CONSTRUCTOR_H__
      29  
      30  /*
      31    If G_HAS_CONSTRUCTORS is true then the compiler support *both* constructors and
      32    destructors, in a usable way, including e.g. on library unload. If not you're on
      33    your own.
      34  
      35    Some compilers need #pragma to handle this, which does not work with macros,
      36    so the way you need to use this is (for constructors):
      37  
      38    #ifdef G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
      39    #pragma G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
      40    #endif
      41    G_DEFINE_CONSTRUCTOR(my_constructor)
      42    static void my_constructor(void) {
      43     ...
      44    }
      45  
      46  */
      47  
      48  #ifndef __GTK_DOC_IGNORE__
      49  
      50  #if  __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
      51  
      52  #define G_HAS_CONSTRUCTORS 1
      53  
      54  #define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
      55  #define G_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
      56  
      57  #elif defined (_MSC_VER)
      58  
      59  /*
      60   * Only try to include gslist.h if not already included via glib.h,
      61   * so that items using gconstructor.h outside of GLib (such as
      62   * GResources) continue to build properly.
      63   */
      64  #ifndef __G_LIB_H__
      65  #include "gslist.h"
      66  #endif
      67  
      68  #include <stdlib.h>
      69  
      70  #define G_HAS_CONSTRUCTORS 1
      71  
      72  /* We do some weird things to avoid the constructors being optimized
      73   * away on VS2015 if WholeProgramOptimization is enabled. First we
      74   * make a reference to the array from the wrapper to make sure its
      75   * references. Then we use a pragma to make sure the wrapper function
      76   * symbol is always included at the link stage. Also, the symbols
      77   * need to be extern (but not dllexport), even though they are not
      78   * really used from another object file.
      79   */
      80  
      81  /* We need to account for differences between the mangling of symbols
      82   * for x86 and x64/ARM/ARM64 programs, as symbols on x86 are prefixed
      83   * with an underscore but symbols on x64/ARM/ARM64 are not.
      84   */
      85  #ifdef _M_IX86
      86  #define G_MSVC_SYMBOL_PREFIX "_"
      87  #else
      88  #define G_MSVC_SYMBOL_PREFIX ""
      89  #endif
      90  
      91  #define G_DEFINE_CONSTRUCTOR(_func) G_MSVC_CTOR (_func, G_MSVC_SYMBOL_PREFIX)
      92  #define G_DEFINE_DESTRUCTOR(_func) G_MSVC_DTOR (_func, G_MSVC_SYMBOL_PREFIX)
      93  
      94  #define G_MSVC_CTOR(_func,_sym_prefix) \
      95    static void _func(void); \
      96    extern int (* _array ## _func)(void);              \
      97    int _func ## _wrapper(void);              \
      98    int _func ## _wrapper(void) { _func(); g_slist_find (NULL,  _array ## _func); return 0; } \
      99    __pragma(comment(linker,"/include:" _sym_prefix # _func "_wrapper")) \
     100    __pragma(section(".CRT$XCU",read)) \
     101    __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _wrapper;
     102  
     103  #define G_MSVC_DTOR(_func,_sym_prefix) \
     104    static void _func(void); \
     105    extern int (* _array ## _func)(void);              \
     106    int _func ## _constructor(void);              \
     107    int _func ## _constructor(void) { atexit (_func); g_slist_find (NULL,  _array ## _func); return 0; } \
     108     __pragma(comment(linker,"/include:" _sym_prefix # _func "_constructor")) \
     109    __pragma(section(".CRT$XCU",read)) \
     110    __declspec(allocate(".CRT$XCU")) int (* _array ## _func)(void) = _func ## _constructor;
     111  
     112  #elif defined(__SUNPRO_C)
     113  
     114  /* This is not tested, but i believe it should work, based on:
     115   * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
     116   */
     117  
     118  #define G_HAS_CONSTRUCTORS 1
     119  
     120  #define G_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
     121  #define G_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
     122  
     123  #define G_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
     124    init(_func)
     125  #define G_DEFINE_CONSTRUCTOR(_func) \
     126    static void _func(void);
     127  
     128  #define G_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
     129    fini(_func)
     130  #define G_DEFINE_DESTRUCTOR(_func) \
     131    static void _func(void);
     132  
     133  #else
     134  
     135  /* constructors not supported for this compiler */
     136  
     137  #endif
     138  
     139  #endif /* __GTK_DOC_IGNORE__ */
     140  #endif /* __G_CONSTRUCTOR_H__ */