[glib] gatomic: statically assert that our assumptions hold



commit 1c47f62de78086e55295d3d4e1f31b0839f89bbf
Author: Simon McVittie <simon mcvittie collabora co uk>
Date:   Mon May 4 10:32:21 2015 +0100

    gatomic: statically assert that our assumptions hold
    
    This code assumes that int is exactly 32 bits, and that pointers
    are either 4 or 8 bits, on platforms with __ATOMIC_SEQ_CST.
    In practice this is going to be true.
    
    A previous attempt at this assertion placed the G_STATIC_ASSERT
    at the top level in gatomic.h, but that broke anjuta, which
    redefined __unused__ at the time. These assertions are about the
    platform/compiler ABI, so it's sufficient to check them once,
    while compiling GLib itself; accordingly, move them to the
    implementation.
    
    Bug: https://bugzilla.gnome.org/show_bug.cgi?id=730932

 glib/gatomic.c |   10 ++++++++++
 glib/gatomic.h |    8 ++++++++
 2 files changed, 18 insertions(+), 0 deletions(-)
---
diff --git a/glib/gatomic.c b/glib/gatomic.c
index e70e809..e3e49fa 100644
--- a/glib/gatomic.c
+++ b/glib/gatomic.c
@@ -95,6 +95,16 @@
  */
 
 #if defined (__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
+
+#if defined(__ATOMIC_SEQ_CST) && !defined(__clang__)
+/* The implementation used in this code path in gatomic.h assumes
+ * 4-byte int */
+G_STATIC_ASSERT (sizeof (gint) == 4);
+
+/* The implementations in gatomic.h assume 4- or 8-byte pointers */
+G_STATIC_ASSERT (sizeof (void *) == 4 || sizeof (void *) == 8);
+#endif
+
 /**
  * g_atomic_int_get:
  * @atomic: a pointer to a #gint or #guint
diff --git a/glib/gatomic.h b/glib/gatomic.h
index 9d04b1d..32d0d1e 100644
--- a/glib/gatomic.h
+++ b/glib/gatomic.h
@@ -87,6 +87,10 @@ G_END_DECLS
 /* We prefer the new C11-style atomic extension of GCC if available */
 #if defined(__ATOMIC_SEQ_CST) && !defined(__clang__)
 
+/* This assumes sizeof(int) is 4: gatomic.c statically
+ * asserts that (using G_STATIC_ASSERT at top-level in a header was
+ * problematic, see #730932) */
+
 #define g_atomic_int_get(atomic) \
   (G_GNUC_EXTENSION ({                                                       \
     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gint));                     \
@@ -116,6 +120,10 @@ G_END_DECLS
 
 #else /* GLIB_SIZEOF_VOID_P == 8 */
 
+/* This assumes that if sizeof(void *) is not 8, then it is 4:
+ * gatomic.c statically asserts that (using G_STATIC_ASSERT
+ * at top-level in a header was problematic, see #730932) */
+
 #define g_atomic_pointer_get(atomic) \
   (G_GNUC_EXTENSION ({                                                       \
     G_STATIC_ASSERT (sizeof *(atomic) == sizeof (gpointer));                 \


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