From: Paul Eggert Date: Wed, 21 Nov 2012 06:25:07 +0000 (-0800) Subject: xalloc: better 'inline' X-Git-Tag: v0.1~352 X-Git-Url: http://erislabs.net/gitweb/?a=commitdiff_plain;h=e76d7f2cbc2cd785d0c5dc5085ba9e41ab4720b9;p=gnulib.git xalloc: better 'inline' * lib/xmalloc.c, lib/xalloc.h (XALLOC_INLINE): New macro. Replace all uses of 'static inline' with it. (static_inline): Remove. * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc, xcharalloc): Let 'extern inline' do the work automatically, instead of doing it by hand. * m4/xalloc.m4 (gl_PREREQ_XALLOC, gl_PREREQ_XMALLOC): Remove. All uses removed. * modules/xalloc (Depends-on): Remove 'inline'. Add 'extern-inline'. --- diff --git a/ChangeLog b/ChangeLog index 62000b664..fa482da60 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,16 @@ 2012-11-29 Paul Eggert + xalloc: better 'inline' + * lib/xmalloc.c, lib/xalloc.h (XALLOC_INLINE): + New macro. Replace all uses of 'static inline' with it. + (static_inline): Remove. + * lib/xalloc.h (xnmalloc, xnrealloc, x2nrealloc, xcharalloc): + Let 'extern inline' do the work automatically, instead of doing + it by hand. + * m4/xalloc.m4 (gl_PREREQ_XALLOC, gl_PREREQ_XMALLOC): + Remove. All uses removed. + * modules/xalloc (Depends-on): Remove 'inline'. Add 'extern-inline'. + gethrxtime: better 'inline' * lib/xtime.c: New file. * lib/gethrxtime.c, lib/gethrxtime.h (GETHRXTIME_INLINE): diff --git a/lib/xalloc.h b/lib/xalloc.h index 6f5b87ee2..acf30a140 100644 --- a/lib/xalloc.h +++ b/lib/xalloc.h @@ -16,28 +16,33 @@ along with this program. If not, see . */ #ifndef XALLOC_H_ -# define XALLOC_H_ +#define XALLOC_H_ -# include +#include -# include "xalloc-oversized.h" +#include "xalloc-oversized.h" -# ifdef __cplusplus +_GL_INLINE_HEADER_BEGIN +#ifndef XALLOC_INLINE +# define XALLOC_INLINE _GL_INLINE +#endif + +#ifdef __cplusplus extern "C" { -# endif +#endif -# if __GNUC__ >= 3 -# define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__)) -# else -# define _GL_ATTRIBUTE_MALLOC -# endif +#if __GNUC__ >= 3 +# define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__)) +#else +# define _GL_ATTRIBUTE_MALLOC +#endif -# if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) -# define _GL_ATTRIBUTE_ALLOC_SIZE(args) __attribute__ ((__alloc_size__ args)) -# else -# define _GL_ATTRIBUTE_ALLOC_SIZE(args) -# endif +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) +# define _GL_ATTRIBUTE_ALLOC_SIZE(args) __attribute__ ((__alloc_size__ args)) +#else +# define _GL_ATTRIBUTE_ALLOC_SIZE(args) +#endif /* This function is always triggered when memory is exhausted. It must be defined by the application, either explicitly @@ -67,45 +72,31 @@ char *xstrdup (char const *str) /* Allocate an object of type T dynamically, with error checking. */ /* extern t *XMALLOC (typename t); */ -# define XMALLOC(t) ((t *) xmalloc (sizeof (t))) +#define XMALLOC(t) ((t *) xmalloc (sizeof (t))) /* Allocate memory for N elements of type T, with error checking. */ /* extern t *XNMALLOC (size_t n, typename t); */ -# define XNMALLOC(n, t) \ - ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t)))) +#define XNMALLOC(n, t) \ + ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t)))) /* Allocate an object of type T dynamically, with error checking, and zero it. */ /* extern t *XZALLOC (typename t); */ -# define XZALLOC(t) ((t *) xzalloc (sizeof (t))) +#define XZALLOC(t) ((t *) xzalloc (sizeof (t))) /* Allocate memory for N elements of type T, with error checking, and zero it. */ /* extern t *XCALLOC (size_t n, typename t); */ -# define XCALLOC(n, t) \ - ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t)))) - - -# if HAVE_INLINE -# define static_inline static inline -# else -void *xnmalloc (size_t n, size_t s) - _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)); -void *xnrealloc (void *p, size_t n, size_t s) - _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)); -void *x2nrealloc (void *p, size_t *pn, size_t s); -char *xcharalloc (size_t n) - _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); -# endif +#define XCALLOC(n, t) \ + ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t)))) -# ifdef static_inline /* Allocate an array of N objects, each with S bytes of memory, dynamically, with error checking. S must be nonzero. */ -static_inline void *xnmalloc (size_t n, size_t s) +XALLOC_INLINE void *xnmalloc (size_t n, size_t s) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)); -static_inline void * +XALLOC_INLINE void * xnmalloc (size_t n, size_t s) { if (xalloc_oversized (n, s)) @@ -116,9 +107,9 @@ xnmalloc (size_t n, size_t s) /* Change the size of an allocated block of memory P to an array of N objects each of S bytes, with error checking. S must be nonzero. */ -static_inline void *xnrealloc (void *p, size_t n, size_t s) +XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s) _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)); -static_inline void * +XALLOC_INLINE void * xnrealloc (void *p, size_t n, size_t s) { if (xalloc_oversized (n, s)) @@ -181,7 +172,7 @@ xnrealloc (void *p, size_t n, size_t s) */ -static_inline void * +XALLOC_INLINE void * x2nrealloc (void *p, size_t *pn, size_t s) { size_t n = *pn; @@ -218,17 +209,15 @@ x2nrealloc (void *p, size_t *pn, size_t s) /* Return a pointer to a new buffer of N bytes. This is like xmalloc, except it returns char *. */ -static_inline char *xcharalloc (size_t n) +XALLOC_INLINE char *xcharalloc (size_t n) _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); -static_inline char * +XALLOC_INLINE char * xcharalloc (size_t n) { return XNMALLOC (n, char); } -# endif - -# ifdef __cplusplus +#ifdef __cplusplus } /* C++ does not allow conversions from void * to other pointer types @@ -265,7 +254,7 @@ xmemdup (T const *p, size_t s) return (T *) xmemdup ((void const *) p, s); } -# endif +#endif #endif /* !XALLOC_H_ */ diff --git a/lib/xmalloc.c b/lib/xmalloc.c index 3b5f86cdc..975598560 100644 --- a/lib/xmalloc.c +++ b/lib/xmalloc.c @@ -17,11 +17,9 @@ #include -#if ! HAVE_INLINE -# define static_inline -#endif +#define XALLOC_INLINE _GL_EXTERN_INLINE + #include "xalloc.h" -#undef static_inline #include #include diff --git a/m4/xalloc.m4 b/m4/xalloc.m4 index f47b649b5..6c2d5d032 100644 --- a/m4/xalloc.m4 +++ b/m4/xalloc.m4 @@ -1,22 +1,7 @@ -# xalloc.m4 serial 17 +# xalloc.m4 serial 18 dnl Copyright (C) 2002-2006, 2009-2012 Free Software Foundation, Inc. dnl This file is free software; the Free Software Foundation dnl gives unlimited permission to copy and/or distribute it, dnl with or without modifications, as long as this notice is preserved. -AC_DEFUN([gl_XALLOC], -[ - gl_PREREQ_XALLOC - gl_PREREQ_XMALLOC -]) - -# Prerequisites of lib/xalloc.h. -AC_DEFUN([gl_PREREQ_XALLOC], [ - AC_REQUIRE([gl_INLINE]) - : -]) - -# Prerequisites of lib/xmalloc.c. -AC_DEFUN([gl_PREREQ_XMALLOC], [ - : -]) +AC_DEFUN([gl_XALLOC], [:]) diff --git a/modules/xalloc b/modules/xalloc index 19e7a0b93..c6c20836b 100644 --- a/modules/xalloc +++ b/modules/xalloc @@ -7,7 +7,7 @@ lib/xmalloc.c m4/xalloc.m4 Depends-on: -inline +extern-inline xalloc-die xalloc-oversized