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
This commit is contained in:
Simon McVittie 2015-05-04 10:32:21 +01:00 committed by Colin Walters
parent ecdd3c29fc
commit 1c47f62de7
2 changed files with 18 additions and 0 deletions

View File

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

View File

@ -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)); \