gmacros: Add G_ALIGNOF superseding _g_alignof macro

This commit is contained in:
Tapasweni Pathak 2018-12-18 13:59:23 +05:30
parent c46565d56a
commit 58bbdcf6c0
6 changed files with 35 additions and 16 deletions

View File

@ -237,6 +237,9 @@ G_STRUCT_OFFSET
<SUBSECTION>
G_MEM_ALIGN
<SUBSECTION>
G_ALIGNOF
<SUBSECTION>
G_CONST_RETURN

View File

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

View File

@ -31,7 +31,7 @@
#include <string.h> /* 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

View File

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

View File

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

View File

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