Use G_STATIC_ASSERT_EXPR for size checks in atomic macros

Also add the same size checks to the macros wrapping
gcc builtins.
This commit is contained in:
Matthias Clasen 2011-05-28 00:04:10 -04:00
parent edd65baa6d
commit 7cf1122253

View File

@ -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 */