From d91a04a3dfc05b42031e8fd00af2cd29b6fa585d Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 29 Oct 2014 16:15:41 -0700 Subject: [PATCH] obstack: prefer __alignof__ to alignof This is for portability to pre-4.7 GCC when compiling glibc. See Joseph S. Myers in: http://sourceware.org/ml/libc-alpha/2014-10/msg00703.html * lib/obstack.c (__alignof__) [!_LIBC && !__GNUC__]: New macro, defined by including and using . (MAX): New macro. (DEFAULT_ALIGNMENT, DEFAULT_ROUNDING): Redefine in terms of these. Do not use enums as they are not portable to some broken compilers. * modules/obstack (Depends-on): Depend on alignof, not stdalign. --- ChangeLog | 11 +++++++++++ lib/obstack.c | 32 ++++++++++++++++++-------------- modules/obstack | 2 +- 3 files changed, 30 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index ffe7285..cea54fc 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2014-10-29 Paul Eggert + obstack: prefer __alignof__ to alignof + This is for portability to pre-4.7 GCC when compiling glibc. + See Joseph S. Myers in: + http://sourceware.org/ml/libc-alpha/2014-10/msg00703.html + * lib/obstack.c (__alignof__) [!_LIBC && !__GNUC__]: + New macro, defined by including and using . + (MAX): New macro. + (DEFAULT_ALIGNMENT, DEFAULT_ROUNDING): Redefine in terms of these. + Do not use enums as they are not portable to some broken compilers. + * modules/obstack (Depends-on): Depend on alignof, not stdalign. + obstack: prefer alignof to calculating alignments by hand * lib/obstack.c: Include . (struct fooalign): Remove. diff --git a/lib/obstack.c b/lib/obstack.c index 03281ae..ba7dff3 100644 --- a/lib/obstack.c +++ b/lib/obstack.c @@ -48,26 +48,30 @@ #endif #ifndef _OBSTACK_ELIDE_CODE -# include +# if !defined _LIBC && !defined __GNUC__ +# include +# define __alignof__(type) alignof_type (type) +# endif # include # include +# ifndef MAX +# define MAX(a,b) ((a) > (b) ? (a) : (b)) +# endif + /* Determine default alignment. */ -union fooround -{ - uintmax_t i; - long double d; - void *p; -}; + /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT. But in fact it might be less smart and round addresses to as much as - DEFAULT_ROUNDING. So we prepare for it to do that. */ -enum -{ - DEFAULT_ALIGNMENT = alignof (union fooround), - DEFAULT_ROUNDING = sizeof (union fooround) -}; - + DEFAULT_ROUNDING. So we prepare for it to do that. + + DEFAULT_ALIGNMENT cannot be an enum constant; see gnulib's alignof.h. */ +#define DEFAULT_ALIGNMENT MAX (__alignof__ (long double), \ + MAX (__alignof__ (uintmax_t), \ + __alignof__ (void *))) +#define DEFAULT_ROUNDING MAX (sizeof (long double), \ + MAX (sizeof (uintmax_t), \ + sizeof (void *))) /* Define a macro that either calls functions with the traditional malloc/free calling interface, or calls functions with the mmalloc/mfree interface diff --git a/modules/obstack b/modules/obstack index c2c6390..36dc444 100644 --- a/modules/obstack +++ b/modules/obstack @@ -6,9 +6,9 @@ lib/obstack.h lib/obstack.c Depends-on: +alignof gettext-h exitfail -stdalign stdint stdlib -- 1.9.3