glib-init: make static assertions about platform assumptions
authorSimon McVittie <simon.mcvittie@collabora.co.uk>
Fri, 16 Nov 2012 12:53:39 +0000 (12:53 +0000)
committerMatthias Clasen <mclasen@redhat.com>
Mon, 20 Jan 2014 01:23:43 +0000 (20:23 -0500)
GLib has a pervasive assumption that function and data pointers are
basically interchangeable, which is true in all modern ABIs,
but not actually guaranteed by ISO C. If someone tries to use GLib on a
platform where function and data pointers are different sizes, fail early.

https://bugzilla.gnome.org/show_bug.cgi?id=688406

glib/glib-init.c

index ceb9395..02b66c0 100644 (file)
@@ -23,6 +23,8 @@
 
 #include "glib-init.h"
 
+#include "glib-private.h"
+#include "gtypes.h"
 #include "gutils.h"     /* for GDebugKey */
 #include "gconstructor.h"
 #include "gmem.h"       /* for g_mem_gc_friendly */
 #include <stdio.h>
 #include <ctype.h>
 
+/* This seems as good a place as any to make static assertions about platform
+ * assumptions we make throughout GLib. */
+
+/* We assume that data pointers are the same size as function pointers... */
+G_STATIC_ASSERT (sizeof (gpointer) == sizeof (GFunc));
+G_STATIC_ASSERT (_g_alignof (gpointer) == _g_alignof (GFunc));
+/* ... and that all function pointers are the same size. */
+G_STATIC_ASSERT (sizeof (GFunc) == sizeof (GCompareDataFunc));
+G_STATIC_ASSERT (_g_alignof (GFunc) == _g_alignof (GCompareDataFunc));
+
 /**
  * g_mem_gc_friendly:
  *