diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index 1369012ce..fb7bfd5f1 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -237,6 +237,9 @@ G_STRUCT_OFFSET G_MEM_ALIGN + +G_ALIGNOF + G_CONST_RETURN diff --git a/glib/docs.c b/glib/docs.c index f45a1fee8..30f6e6a56 100644 --- a/glib/docs.c +++ b/glib/docs.c @@ -1865,7 +1865,19 @@ * macro will not work on an array allocated on the heap, only static * arrays or arrays on the stack. */ - + +/** + * G_ALIGNOF + * @a: a type-name + * + * Return the minimum alignment required by the platform ABI for values of the given + * type. The address of a variable or struct member of the given type must always be + * a multiple of this alignment. For example, most platforms require int variables + * to be aligned at a 4-byte boundary, so `G_ALIGNOF (int)` is 4 on most platforms. + * + * Since: 2.60 + */ + /* Miscellaneous Macros {{{1 */ /** diff --git a/glib/ghash.c b/glib/ghash.c index af79f5b67..dac5552bd 100644 --- a/glib/ghash.c +++ b/glib/ghash.c @@ -31,7 +31,7 @@ #include /* memset */ #include "ghash.h" - +#include "gmacros.h" #include "glib-private.h" #include "gstrfuncs.h" #include "gatomic.h" @@ -285,7 +285,7 @@ typedef struct } RealIter; G_STATIC_ASSERT (sizeof (GHashTableIter) == sizeof (RealIter)); -G_STATIC_ASSERT (_g_alignof (GHashTableIter) >= _g_alignof (RealIter)); +G_STATIC_ASSERT (G_ALIGNOF (GHashTableIter) >= G_ALIGNOF (RealIter)); /* Each table size has an associated prime modulo (the first prime * lower than the table size) used to find the initial bucket. Probing diff --git a/glib/glib-init.c b/glib/glib-init.c index 6cb4e4a0d..6ffe7933b 100644 --- a/glib/glib-init.c +++ b/glib/glib-init.c @@ -20,8 +20,7 @@ #include "config.h" #include "glib-init.h" - -#include "glib-private.h" +#include "gmacros.h" #include "gtypes.h" #include "gutils.h" /* for GDebugKey */ #include "gconstructor.h" @@ -40,10 +39,10 @@ G_STATIC_ASSERT (CHAR_BIT == 8); /* We assume that data pointers are the same size as function pointers... */ G_STATIC_ASSERT (sizeof (gpointer) == sizeof (GFunc)); -G_STATIC_ASSERT (_g_alignof (gpointer) == _g_alignof (GFunc)); +G_STATIC_ASSERT (G_ALIGNOF (gpointer) == G_ALIGNOF (GFunc)); /* ... and that all function pointers are the same size. */ G_STATIC_ASSERT (sizeof (GFunc) == sizeof (GCompareDataFunc)); -G_STATIC_ASSERT (_g_alignof (GFunc) == _g_alignof (GCompareDataFunc)); +G_STATIC_ASSERT (G_ALIGNOF (GFunc) == G_ALIGNOF (GCompareDataFunc)); /* We assume that "small" enums (those where all values fit in INT32_MIN * to INT32_MAX) are exactly int-sized. In particular, we assume that if @@ -64,9 +63,9 @@ typedef enum { G_STATIC_ASSERT (sizeof (TestChar) == sizeof (int)); G_STATIC_ASSERT (sizeof (TestShort) == sizeof (int)); G_STATIC_ASSERT (sizeof (TestInt) == sizeof (int)); -G_STATIC_ASSERT (_g_alignof (TestChar) == _g_alignof (int)); -G_STATIC_ASSERT (_g_alignof (TestShort) == _g_alignof (int)); -G_STATIC_ASSERT (_g_alignof (TestInt) == _g_alignof (int)); +G_STATIC_ASSERT (G_ALIGNOF (TestChar) == G_ALIGNOF (int)); +G_STATIC_ASSERT (G_ALIGNOF (TestShort) == G_ALIGNOF (int)); +G_STATIC_ASSERT (G_ALIGNOF (TestInt) == G_ALIGNOF (int)); /** * g_mem_gc_friendly: diff --git a/glib/glib-private.h b/glib/glib-private.h index 31acd9386..c002b4a6d 100644 --- a/glib/glib-private.h +++ b/glib/glib-private.h @@ -22,12 +22,6 @@ #include "gwakeup.h" #include "gstdioprivate.h" -#if defined(__GNUC__) -# define _g_alignof(type) (__alignof__ (type)) -#else -# define _g_alignof(type) (G_STRUCT_OFFSET (struct { char a; type b; }, b)) -#endif - GMainContext * g_get_worker_context (void); gboolean g_check_setuid (void); GMainContext * g_main_context_new_with_next_id (guint next_id); diff --git a/glib/gmacros.h b/glib/gmacros.h index 437330f76..f8db7e83b 100644 --- a/glib/gmacros.h +++ b/glib/gmacros.h @@ -405,6 +405,17 @@ #endif #endif +/* Provide G_ALIGNOF alignment macro. + */ + +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__cplusplus) +#define G_ALIGNOF(type) _Alignof (type) +#elif defined(__GNUC__) +#define G_ALIGNOF(type) (__alignof__ (type)) +#else +#define G_ALIGNOF(type) (G_STRUCT_OFFSET (struct { char a; type b; }, b)) +#endif + /* Deprecated -- do not use. */ #ifndef G_DISABLE_DEPRECATED #ifdef G_DISABLE_CONST_RETURNS