From dd1d33cf7c97da68f541ba497524a998bc228058 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sat, 23 Nov 2013 10:03:42 +0100 Subject: [PATCH] marshal: use __alignof__ operator if available --- configure.ac | 1 + m4/alignof.m4 | 14 ++++++++++++++ src/marshal.c | 6 +++++- 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 m4/alignof.m4 diff --git a/configure.ac b/configure.ac index 1e409e14..9e1ac0ef 100644 --- a/configure.ac +++ b/configure.ac @@ -93,6 +93,7 @@ AC_CACHE_SAVE # Checks for typedefs, structures, and compiler characteristics. lldp_CHECK___PROGNAME +lldp_CHECK_ALIGNOF # Checks for library functions. AC_CONFIG_LIBOBJ_DIR([src/compat]) diff --git a/m4/alignof.m4 b/m4/alignof.m4 new file mode 100644 index 00000000..f45caae7 --- /dev/null +++ b/m4/alignof.m4 @@ -0,0 +1,14 @@ +# +# lldp_CHECK_ALIGNOF +# +AC_DEFUN([lldp_CHECK_ALIGNOF],[ + AC_CACHE_CHECK([whether compiler understands __alignof__], lldp_cv_check_alignof, [ + AC_TRY_COMPILE([], + [ return __alignof__(long); ], + [ lldp_cv_check_alignof="yes" ], + [ lldp_cv_check_alignof="no" ]) + ]) + if test x"$lldp_cv_check_alignof" = x"yes"; then + AC_DEFINE([HAVE_ALIGNOF], [1], [Define if __alignof__ operator is available]) + fi +]) diff --git a/src/marshal.c b/src/marshal.c index 45c46cbc..8a57d305 100644 --- a/src/marshal.c +++ b/src/marshal.c @@ -31,7 +31,11 @@ #include "lldpd-structs.h" /* Stolen from CCAN */ -#define ALIGNOF(t) ((sizeof(t) > 1)?((char *)(&((struct { char c; t _h; } *)0)->_h) - (char *)0):1) +#if HAVE_ALIGNOF +# define ALIGNOF(t) (__alignof__(t)) +#else +# define ALIGNOF(t) ((sizeof(t) > 1)?((char *)(&((struct { char c; t _h; } *)0)->_h) - (char *)0):1) +#endif /* A serialized object */ struct marshal_serialized { -- 2.39.5