Use compiler support for destructors instead of g_atexit()

If the compiler supports a destructor feature we can use that instead of
the deprecated g_atexit() function to print out the profile report.

The constructor/destructor detection has been taken from the equivalent
macro dance in GLib.
This commit is contained in:
Emmanuele Bassi 2012-02-15 15:56:46 +00:00
parent 8361418f9d
commit f2773d46e2
2 changed files with 110 additions and 4 deletions

View File

@ -267,4 +267,96 @@ typedef enum _ClutterCullResult
G_END_DECLS
/* taken from glib/gconstructor.h
If CLUTTER_HAS_CONSTRUCTORS is true then the compiler support *both*
constructors and destructors, in a sane way, including e.g. on library
unload. If not you're on your own.
Some compilers need #pragma to handle this, which does not work with
macros, so the way you need to use this is (for constructors):
#ifdef CLUTTER_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA
#pragma CLUTTER_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(my_constructor)
#endif
CLUTTER_DEFINE_CONSTRUCTOR(my_constructor)
static void my_constructor(void) {
...
}
*/
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)
#define CLUTTER_HAS_CONSTRUCTORS 1
#define CLUTTER_DEFINE_CONSTRUCTOR(_func) static void __attribute__((constructor)) _func (void);
#define CLUTTER_DEFINE_DESTRUCTOR(_func) static void __attribute__((destructor)) _func (void);
#elif defined (_MSC_VER) && (_MSC_VER >= 1500)
/* Visual studio 2008 and later has _Pragma */
#define CLUTTER_HAS_CONSTRUCTORS 1
#define CLUTTER_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 (* _array ## _func)(void) = _func ## _wrapper;
#define CLUTTER_DEFINE_DESTRUCTOR(_func) \
static void _func(void); \
static int _func ## _constructor(void) { atexit (_func); return 0; } \
__pragma(section(".CRT$XCU",read)) \
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
#elif defined (_MSC_VER)
#define CLUTTER_HAS_CONSTRUCTORS 1
/* Pre Visual studio 2008 must use #pragma section */
#define CLUTTER_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
#define CLUTTER_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
#define CLUTTER_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
section(".CRT$XCU",read)
#define CLUTTER_DEFINE_CONSTRUCTOR(_func) \
static void _func(void); \
static int _func ## _wrapper(void) { _func(); return 0; } \
__declspec(allocate(".CRT$XCU")) static int (*p)(void) = _func ## _wrapper;
#define CLUTTER_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
section(".CRT$XCU",read)
#define CLUTTER_DEFINE_DESTRUCTOR(_func) \
static void _func(void); \
static int _func ## _constructor(void) { atexit (_func); return 0; } \
__declspec(allocate(".CRT$XCU")) static int (* _array ## _func)(void) = _func ## _constructor;
#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 CLUTTER_HAS_CONSTRUCTORS 1
#define CLUTTER_DEFINE_CONSTRUCTOR_NEEDS_PRAGMA 1
#define CLUTTER_DEFINE_DESTRUCTOR_NEEDS_PRAGMA 1
#define CLUTTER_DEFINE_CONSTRUCTOR_PRAGMA_ARGS(_func) \
init(_func)
#define CLUTTER_DEFINE_CONSTRUCTOR(_func) \
static void _func(void);
#define CLUTTER_DEFINE_DESTRUCTOR_PRAGMA_ARGS(_func) \
fini(_func)
#define CLUTTER_DEFINE_DESTRUCTOR(_func) \
static void _func(void);
#else
/* constructors not supported for this compiler */
#endif
#endif /* __CLUTTER_PRIVATE_H__ */

View File

@ -1,10 +1,13 @@
#ifdef CLUTTER_ENABLE_PROFILE
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
/* XXX - we need this for g_atexit() */
#define G_DISABLE_DEPRECATION_WARNINGS
#include "clutter-profile.h"
#include "clutter-private.h"
UProfContext *_clutter_uprof_context;
@ -221,8 +224,15 @@ _clutter_uprof_report_done (UProfReport *report, void *closure, void *user_data)
g_free (closure);
}
#ifdef CLUTTER_HAS_CONSTRUCTORS
#ifdef CLUTTER_DEFINE_DESTRUCTOR_NEEDS_PRAGMA
#pragma CLUTTER_DEFINE_DESTRUCTOR_PRAGMA_ARGS(print_profile_report_atexit)
#endif
CLUTTER_DEFINE_DESTRUCTOR(print_profile_report_atexit)
#endif /* CLUTTER_HAS_CONSTRUCTORS */
static void
print_exit_report (void)
print_profile_report_atexit (void)
{
if (!(clutter_profile_flags & CLUTTER_PROFILE_DISABLE_REPORT))
uprof_report_print (clutter_uprof_report);
@ -239,7 +249,11 @@ _clutter_uprof_init (void)
_clutter_uprof_context = uprof_context_new ("Clutter");
uprof_context_link (_clutter_uprof_context, uprof_get_mainloop_context ());
g_atexit (print_exit_report);
#ifndef CLUTTER_HAS_CONSTRUCTORS
/* fall back to the old atexit() trick if we don't have destructors */
g_atexit (print_profile_report_atexit);
#endif
cogl_context = uprof_find_context ("Cogl");
if (cogl_context)