On Mon, Jan 31, 2011 at 03:45:52PM -0300, Davidlohr Bueso wrote: > From: Davidlohr Bueso <dave@xxxxxxx> > Date: Mon, 31 Jan 2011 15:42:07 -0300 > > When using an older version of GCC (3.0), the alloc_size attribute, used in xalloc.h does not exist, producing multiple warnings: > > warning: `__alloc_size__' attribute directive ignored Good catch. > +#if ((__GNUC__ * 100 + __GNUC__MINOR__) > 300) /* gcc version > 3.0 */ > +# define ALLOCSZ_ATTR(x,...) __attribute__ ((alloc_size(x, ##__VA_ARGS__))) > +#else > +# define ALLOCSZ_ATTR(x,...) > +#endif Hmm.. frankly, I don't like if function attributes are defined by uppercase macros and without the "__" prefix. I think that solution used in kernel and glibc is better. Applied the patch below. Thanks. Karel >From 40084d0d5c5031580907d61e2df81b5b21af6c68 Mon Sep 17 00:00:00 2001 From: Karel Zak <kzak@xxxxxxxxxx> Date: Mon, 7 Feb 2011 17:29:47 +0100 Subject: [PATCH] include: [c.h] add fallback for alloc_size attributes Reported-by: Davidlohr Bueso <dave@xxxxxxx> Signed-off-by: Karel Zak <kzak@xxxxxxxxxx> --- include/c.h | 27 +++++++++++++++++++++++++++ include/xalloc.h | 8 +++++--- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/include/c.h b/include/c.h index 02c29e9..f50a338 100644 --- a/include/c.h +++ b/include/c.h @@ -10,6 +10,15 @@ /* * Compiler specific stuff */ +#ifndef __GNUC_PREREQ +# if defined __GNUC__ && defined __GNUC_MINOR__ +# define __GNUC_PREREQ(maj, min) \ + ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min)) +# else +# define __GNUC_PREREQ(maj, min) 0 +# endif +#endif + #ifdef __GNUC__ /* &a[0] degrades to a pointer: a different type from an array */ @@ -24,6 +33,24 @@ # define ignore_result(x) ((void) (x)) #endif /* !__GNUC__ */ +/* + * Function attributes + */ +#ifndef __ul_alloc_size +# if __GNUC_PREREQ (3, 0) +# define __ul_alloc_size(s) __attribute__((alloc_size(s))) +# else +# define __ul_alloc_size(s) +# endif +#endif + +#ifndef __ul_calloc_size +# if __GNUC_PREREQ (3, 0) +# define __ul_calloc_size(n, s) __attribute__((alloc_size(n, s))) +# else +# define __ul_calloc_size(n, s) +# endif +#endif /* Force a compilation error if condition is true, but also produce a * result (of value 0 and type size_t), so the expression can be used diff --git a/include/xalloc.h b/include/xalloc.h index fc2f886..27efa30 100644 --- a/include/xalloc.h +++ b/include/xalloc.h @@ -13,11 +13,13 @@ #include <stdlib.h> #include <err.h> +#include "c.h" + #ifndef XALLOC_EXIT_CODE # define XALLOC_EXIT_CODE EXIT_FAILURE #endif -static inline __attribute__((alloc_size(1))) +static inline __ul_alloc_size(1) void *xmalloc(const size_t size) { void *ret = malloc(size); @@ -27,7 +29,7 @@ void *xmalloc(const size_t size) return ret; } -static inline __attribute__((alloc_size(2))) +static inline __ul_alloc_size(2) void *xrealloc(void *ptr, const size_t size) { void *ret = realloc(ptr, size); @@ -37,7 +39,7 @@ void *xrealloc(void *ptr, const size_t size) return ret; } -static inline __attribute__((alloc_size(1,2))) +static inline __ul_calloc_size(1, 2) void *xcalloc(const size_t nelems, const size_t size) { void *ret = calloc(nelems, size); -- 1.7.3.4 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html