Use G_STATIC_ASSERT_EXPR for size checks in atomic macros
authorMatthias Clasen <mclasen@redhat.com>
Sat, 28 May 2011 04:04:10 +0000 (00:04 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Sat, 28 May 2011 04:04:10 +0000 (00:04 -0400)
Also add the same size checks to the macros wrapping
gcc builtins.

glib/gatomic.h

index 969443a..9c85607 100644 (file)
 G_BEGIN_DECLS
 
 gint     g_atomic_int_exchange_and_add         (volatile gint G_GNUC_MAY_ALIAS *atomic,
-                                               gint               val);
+                                                gint          val);
 void     g_atomic_int_add                      (volatile gint G_GNUC_MAY_ALIAS *atomic,
-                                               gint               val);
+                                                gint          val);
 gboolean g_atomic_int_compare_and_exchange     (volatile gint G_GNUC_MAY_ALIAS *atomic,
-                                               gint               oldval,
-                                               gint               newval);
-gboolean g_atomic_pointer_compare_and_exchange (volatile gpointer G_GNUC_MAY_ALIAS *atomic, 
-                                               gpointer           oldval, 
-                                               gpointer           newval);
+                                                gint          oldval,
+                                                gint          newval);
+gboolean g_atomic_pointer_compare_and_exchange (volatile gpointer G_GNUC_MAY_ALIAS *atomic,
+                                                gpointer      oldval,
+                                                gpointer      newval);
 
 gint     g_atomic_int_get                      (volatile gint G_GNUC_MAY_ALIAS *atomic);
 void     g_atomic_int_set                      (volatile gint G_GNUC_MAY_ALIAS *atomic,
-                                               gint               newval);
+                                                gint          newval);
 gpointer g_atomic_pointer_get                  (volatile gpointer G_GNUC_MAY_ALIAS *atomic);
 void     g_atomic_pointer_set                  (volatile gpointer G_GNUC_MAY_ALIAS *atomic,
-                                               gpointer           newval);
+                                                gpointer      newval);
 
 #if defined(__GNUC__) && defined(G_ATOMIC_OP_USE_GCC_BUILTINS)
 
 #define g_atomic_int_exchange_and_add(atomic,val) \
-  __sync_fetch_and_add((atomic),(val))
+  __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)); \
+   __sync_fetch_and_add((atomic),(val)); })
 
 #define g_atomic_int_add(atomic,val) \
-  __sync_fetch_and_add((atomic),(val))
+  __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)); \
+   __sync_fetch_and_add((atomic),(val)); })
 
 #define g_atomic_int_compare_and_exchange(atomic,oldval,newval) \
-  __sync_bool_compare_and_swap((atomic),(oldval),(newval))
+  __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)); \
+   __sync_bool_compare_and_swap((atomic),(oldval),(newval)); })
 
 #define g_atomic_int_get(atomic) \
-  __extension__ ({ __sync_synchronize(); *(atomic); })
+  __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)); \
+   __sync_synchronize(); *(atomic); })
 
 #define g_atomic_int_set(atomic,newval) \
-  __extension__ ({ *(atomic) = (newval); __sync_synchronize(); })
+  __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)); \
+    *(atomic) = (newval); __sync_synchronize(); })
 
 #define g_atomic_pointer_compare_and_exchange(atomic,oldval,newval) \
-  __sync_bool_compare_and_swap((atomic),(oldval),(newval))
+  __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gpointer)); \
+    __sync_bool_compare_and_swap((atomic),(oldval),(newval)); })
 
 #define g_atomic_pointer_get(atomic) \
-  __extension__ ({ __sync_synchronize(); *(atomic); })
+  __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gpointer)); \
+    __sync_synchronize(); *(atomic); })
 
 #define g_atomic_pointer_set(atomic,newval) \
-  __extension__ ({ *(atomic) = (newval); __sync_synchronize(); })
+  __extension__ ({ G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gpointer)); \
+    *(atomic) = (newval); __sync_synchronize(); })
 
 #elif !defined(G_ATOMIC_OP_MEMORY_BARRIER_NEEDED)
 
@@ -92,16 +100,16 @@ void     g_atomic_pointer_set                  (volatile gpointer G_GNUC_MAY_ALI
 #else
 
 # define g_atomic_int_get(atomic) \
- ((void) sizeof (gchar [sizeof (*(atomic)) == sizeof (gint) ? 1 : -1]), \
+ (G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)), \
   (g_atomic_int_get) ((volatile gint G_GNUC_MAY_ALIAS *) (volatile void *) (atomic)))
 # define g_atomic_int_set(atomic, newval) \
- ((void) sizeof (gchar [sizeof (*(atomic)) == sizeof (gint) ? 1 : -1]), \
+ (G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gint)), \
   (g_atomic_int_set) ((volatile gint G_GNUC_MAY_ALIAS *) (volatile void *) (atomic), (newval)))
 # define g_atomic_pointer_get(atomic) \
- ((void) sizeof (gchar [sizeof (*(atomic)) == sizeof (gpointer) ? 1 : -1]), \
+ (G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gpointer)), \
   (g_atomic_pointer_get) ((volatile gpointer G_GNUC_MAY_ALIAS *) (volatile void *) (atomic)))
 # define g_atomic_pointer_set(atomic, newval) \
- ((void) sizeof (gchar [sizeof (*(atomic)) == sizeof (gpointer) ? 1 : -1]), \
+ (G_STATIC_ASSERT_EXPR(sizeof (*(atomic)) == sizeof (gpointer)), \
   (g_atomic_pointer_set) ((volatile gpointer G_GNUC_MAY_ALIAS *) (volatile void *) (atomic), (newval)))
 
 #endif /* G_ATOMIC_OP_MEMORY_BARRIER_NEEDED */