[glib] Use G_STATIC_ASSERT_EXPR for size checks in atomic macros



commit 7cf112225399ffa262082727bf4f434d57568e30
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat May 28 00:04:10 2011 -0400

    Use G_STATIC_ASSERT_EXPR for size checks in atomic macros
    
    Also add the same size checks to the macros wrapping
    gcc builtins.

 glib/gatomic.h |   50 +++++++++++++++++++++++++++++---------------------
 1 files changed, 29 insertions(+), 21 deletions(-)
---
diff --git a/glib/gatomic.h b/glib/gatomic.h
index 969443a..9c85607 100644
--- a/glib/gatomic.h
+++ b/glib/gatomic.h
@@ -39,48 +39,56 @@
 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 */



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