summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSiarhei Siamashka <siarhei.siamashka@nokia.com>2010-09-24 16:36:16 +0300
committerSiarhei Siamashka <siarhei.siamashka@nokia.com>2010-11-05 16:02:28 +0200
commitfed4a2fde540916fc182917762b85b38052c04de (patch)
tree8d4a03e3a86526e9b78a2e3127f9448898a0aa2b
parent99699771cd82e108fbace655bf44013bdccde3bf (diff)
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
-rw-r--r--configure.ac27
-rw-r--r--pixman/pixman.c15
2 files changed, 39 insertions, 3 deletions
diff --git a/configure.ac b/configure.ac
index 81938988..050e4b03 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 e35c5b37..045c556d 100644
--- a/pixman/pixman.c
+++ b/pixman/pixman.c
@@ -30,14 +30,23 @@
#include <stdlib.h>
+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;
}