From fed4a2fde540916fc182917762b85b38052c04de Mon Sep 17 00:00:00 2001 From: Siarhei Siamashka Date: Fri, 24 Sep 2010 16:36:16 +0300 Subject: [PATCH] Do CPU features detection from 'constructor' function when compiled with gcc There is attribute 'constructor' supported since gcc 2.7 which allows to have a constructor function for library initialization. This eliminates an extra branch for each composite operation and also helps to avoid complains from race condition detection tools like helgrind. The other compilers may or may not support this attribute properly. Ideally, the compilers should fail to compile the code with unknown attribute, so the configure check should do the right job. But in reality the problems are surely possible. Fortunately such problems should be quite easy to find because NULL pointer dereference should happen almost immediately if the constructor fails to run. clang 2.7: supports __attribute__((constructor)) properly and pretends to be gcc tcc 0.9.25: ignores __attribute__((constructor)), but does not pretend to be gcc --- configure.ac | 27 +++++++++++++++++++++++++++ pixman/pixman.c | 15 ++++++++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/configure.ac b/configure.ac index 8193898..050e4b0 100644 --- a/configure.ac +++ b/configure.ac @@ -740,6 +740,33 @@ AC_SUBST(HAVE_PTHREAD_SETSPECIFIC) AC_SUBST(PTHREAD_LDFLAGS) AC_SUBST(PTHREAD_LIBS) +dnl ===================================== +dnl __attribute__((constructor)) + +support_for_attribute_constructor=no + +AC_MSG_CHECKING(for __attribute__((constructor))) +AC_LINK_IFELSE([ +#if defined(__GNUC__) && (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 7)) +/* attribute 'constructor' is supported since gcc 2.7, but some compilers + * may only pretend to be gcc, so let's try to actually use it + */ +static int x = 1; +static void __attribute__((constructor)) constructor_function () { x = 0; } +int main (void) { return x; } +#else +#error not gcc or gcc version is older than 2.7 +#endif +], support_for_attribute_constructor=yes) + +if test x$support_for_attribute_constructor = xyes; then + AC_DEFINE([TOOLCHAIN_SUPPORTS_ATTRIBUTE_CONSTRUCTOR], + [],[Whether the tool chain supports __attribute__((constructor))]) +fi + +AC_MSG_RESULT($support_for_attribute_constructor) +AC_SUBST(TOOLCHAIN_SUPPORTS_ATTRIBUTE_CONSTRUCTOR) + AC_OUTPUT([pixman-1.pc pixman-1-uninstalled.pc Makefile diff --git a/pixman/pixman.c b/pixman/pixman.c index e35c5b3..045c556 100644 --- a/pixman/pixman.c +++ b/pixman/pixman.c @@ -30,14 +30,23 @@ #include +static pixman_implementation_t *global_implementation; + +#ifdef TOOLCHAIN_SUPPORTS_ATTRIBUTE_CONSTRUCTOR +static void __attribute__((constructor)) +pixman_constructor (void) +{ + global_implementation = _pixman_choose_implementation (); +} +#endif + static force_inline pixman_implementation_t * get_implementation (void) { - static pixman_implementation_t *global_implementation; - +#ifndef TOOLCHAIN_SUPPORTS_ATTRIBUTE_CONSTRUCTOR if (!global_implementation) global_implementation = _pixman_choose_implementation (); - +#endif return global_implementation; } -- 2.7.4