[glib/resources: 1/6] Add macros to handle constructor functions on the compilers that support it



commit 1cbd4d82f244fbe93c81d377a70cc2b7bcbe54de
Author: Alexander Larsson <alexl redhat com>
Date:   Wed Dec 21 21:13:21 2011 +0100

    Add macros to handle constructor functions on the compilers that support it
    
    We define two possible macros, G_DEFINE_CONSTRUCTOR and
    G_DEFINE_CONSTRUCTOR_WITH_PRAGMA. If none are defined then constructors
    are not supported on this compiler.
    
    The canonical way to use these is:
      G_DEFINE_CONSTRUCTOR(my_func)
      G_DEFINE_CONSTRUCTOR_WITH_PRAGMA(my_func)
    
    static void my_func (void)
    {
      ...
    
    This is a bit ugly, but thats unavoidable as some compilers like
    suncc and pre VS 2008 only supports this via pragmas.

 glib/gmacros.h |   41 +++++++++++++++++++++++++++++++++++++++++
 1 files changed, 41 insertions(+), 0 deletions(-)
---
diff --git a/glib/gmacros.h b/glib/gmacros.h
index 2b340cb..42ee0fe 100644
--- a/glib/gmacros.h
+++ b/glib/gmacros.h
@@ -318,4 +318,45 @@
 #define GLIB_DEPRECATED_FOR(f) G_DEPRECATED_FOR(f)
 #endif
 
+#if  __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
+
+#define G_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
+
+#elif _MSC_VER >= 1500
+/* Visual studio 2008 and later has _Pragma */
+
+#define G_DEFINE_CONSTRUCTOR(_func) \
+  static void _func(void); \
+  static int _func ## _wrapper(void) { _func(); return 0; } \
+  __pragma(section(".CRT$XCU",read)) \
+  __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
+
+#elif defined (_MSC_VER)
+
+/* Pre Visual studio 2008 must use #pragma section */
+#define G_DEFINE_CONSTRUCTOR_WITH_PRAGMA_PRAGMA_ARGS(_func) \
+  section(".CRT$XCU",read)
+#define G_DEFINE_CONSTRUCTOR_WITH_PRAGMA(_func) \
+  static void _func(void); \
+  static int _func ## _wrapper(void) { _func(); return 0; } \
+  __declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
+
+#elif defined(__SUNPRO_C)
+
+/* This is not tested, but i believe it should work, based on:
+ * http://opensource.apple.com/source/OpenSSL098/OpenSSL098-35/src/fips/fips_premain.c
+ */
+#define G_DEFINE_CONSTRUCTOR_WITH_PRAGMA_PRAGMA_ARGS(_func) \
+  init(_func)
+#define G_DEFINE_CONSTRUCTOR_WITH_PRAGMA(_func) \
+  static void _func(void);
+
+#else
+
+/* constructors not supported for this compiler */
+
+#endif
+
+
+
 #endif /* __G_MACROS_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]