Commit 986b0a59 authored by Simon McVittie's avatar Simon McVittie

Merge branch '1055-will-the-alignment-never-end' into 'master'

gmacros: Don’t use __alignof__ in G_ALIGNOF implementation

Closes #1055

See merge request !559
parents cfd2e837 ab56f045
Pipeline #48862 failed with stages
in 11 minutes and 3 seconds
...@@ -1870,11 +1870,16 @@ ...@@ -1870,11 +1870,16 @@
* G_ALIGNOF * G_ALIGNOF
* @a: a type-name * @a: a type-name
* *
* Return the minimum alignment required by the platform ABI for values of the given * Return the minimal 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 * 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 * 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. * to be aligned at a 4-byte boundary, so `G_ALIGNOF (int)` is 4 on most platforms.
* *
* Note this is not necessarily the same as the value returned by GCC’s
* `__alignof__` operator, which returns the preferred alignment for a type.
* The preferred alignment may be a stricter alignment than the minimal
* alignment.
*
* Since: 2.60 * Since: 2.60
*/ */
......
...@@ -406,12 +406,14 @@ ...@@ -406,12 +406,14 @@
#endif #endif
/* Provide G_ALIGNOF alignment macro. /* Provide G_ALIGNOF alignment macro.
*
* Note we cannot use the gcc __alignof__ operator here, as that returns the
* preferred alignment rather than the minimal alignment. See
* https://gitlab.gnome.org/GNOME/glib/merge_requests/538/diffs#note_390790.
*/ */
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__cplusplus) #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__cplusplus)
#define G_ALIGNOF(type) _Alignof (type) #define G_ALIGNOF(type) _Alignof (type)
#elif defined(__GNUC__)
#define G_ALIGNOF(type) (__alignof__ (type))
#else #else
#define G_ALIGNOF(type) (G_STRUCT_OFFSET (struct { char a; type b; }, b)) #define G_ALIGNOF(type) (G_STRUCT_OFFSET (struct { char a; type b; }, b))
#endif #endif
......
...@@ -70,6 +70,7 @@ test_programs = \ ...@@ -70,6 +70,7 @@ test_programs = \
keyfile \ keyfile \
list \ list \
logging \ logging \
macros \
mainloop \ mainloop \
mappedfile \ mappedfile \
markup \ markup \
......
/* GLib testing framework examples and tests
*
* Copyright © 2018 Endless Mobile, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Author: Philip Withnall <withnall@endlessm.com>
*/
#include <glib.h>
/* Test G_ALIGNOF() gives the same results as the G_STRUCT_OFFSET fallback. This
* should be the minimal alignment for the given type.
*
* This is necessary because the implementation of G_ALIGNOF() varies depending
* on the compiler in use. We want all implementations to be consistent.
*
* In the case that the compiler uses the G_STRUCT_OFFSET fallback, this test
* is a no-op. */
static void
test_alignof_fallback (void)
{
#define check_alignof(type) \
g_assert_cmpint (G_ALIGNOF (type), ==, G_STRUCT_OFFSET (struct { char a; type b; }, b))
check_alignof (char);
check_alignof (int);
check_alignof (float);
check_alignof (double);
check_alignof (struct { char a; int b; });
}
int
main (int argc,
char *argv[])
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/alignof/fallback", test_alignof_fallback);
return g_test_run ();
}
...@@ -38,6 +38,7 @@ glib_tests = { ...@@ -38,6 +38,7 @@ glib_tests = {
'keyfile' : {}, 'keyfile' : {},
'list' : {}, 'list' : {},
'logging' : {}, 'logging' : {},
'macros' : {},
'mainloop' : {}, 'mainloop' : {},
'mappedfile' : {}, 'mappedfile' : {},
'markup' : {}, 'markup' : {},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment