bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gnulib] xmalloc, xnmalloc


From: Paul Eggert
Subject: Re: [bug-gnulib] xmalloc, xnmalloc
Date: Tue, 07 Nov 2006 16:37:10 -0800
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/21.4 (gnu/linux)

Bruno Haible <address@hidden> writes:

> The purpose of HAVE_INLINE is to avoid bloating the binary when compiling
> with a compiler that doesn't support 'inline'. With such a compiler,
> AC_C_INLINE defines 'inline' to empty, and such older compilers don't
> optimize away unused static functions.

Hmm, what if GCC's __NO_INLINE__ macro is defined?  Shouldn't
m4/inline.m4 define HAVE_INLINE only if __NO_INLINE__ is not defined?

Also, for what it's worth, I think GCC 3.3 and earlier don't optimize
away unused static functions in some cases; this stuff wasn't fixed
until -funit-at-a-time option was made the default with -O2 or higher
(GCC 3.4, I think).

> What I suggest to get around this, is to define these functions with
> external linkage when such a compiler is in use. To avoid code duplication,
> one needs to move the function definitions to a separate file.

Urk.  There's gotta be a better way.

I installed the following, with some misgivings as I worry we're
over-optimizing here (for obsolete compilers!).  As you can see, from
a minor style issue, I prefer macros like "static_inline" and
"long_double" when defining macros that stand for their standard
counterparts like "static inline" and "long double".

But anyway, does this solve the problems you mentioned?

2006-11-07  Paul Eggert  <address@hidden>

        * lib/xalloc.h (XMALLOC, XNMALLOC, XZALLOC, XCALLOC): Move
        definitions up, to avoid colliding with change below.
        (static_inline) [HAVE_INLINE]: New macro.
        (xnmalloc, xnrealloc, x2nrealloc, xcharalloc):
        Provide extern decls when !HAVE_INLINE.  Do not define unless
        static_inline is defined, either by us or by xmalloc.c.  Use
        static_inline rather than static inline.
        (XCALLOC): Optimize sizeof(T) = 1 case.
        * lib/xmalloc.c (static_inline) [!HAVE_INLINE]: New macro.

2006-11-07  Bruno Haible  <address@hidden>

        * lib/xalloc.h (XNMALLOC): Restore optimization of sizeof(T) = 1 case.
        * m4/xalloc.m4 (gl_PREREQ_XALLOC): Require gl_INLINE instead of
        AC_C_INLINE.
        * modules/xalloc (Files): Add m4/inline.m4.

Index: lib/xalloc.h
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/xalloc.h,v
retrieving revision 1.31
diff -p -u -r1.31 xalloc.h
--- lib/xalloc.h        6 Nov 2006 21:24:35 -0000       1.31
+++ lib/xalloc.h        8 Nov 2006 00:21:58 -0000
@@ -68,10 +68,48 @@ char *xstrdup (char const *str);
 # define xalloc_oversized(n, s) \
     ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n))
 
+
+/* In the following macros, T must be an elementary or structure/union or
+   typedef'ed type, or a pointer to such a type.  To apply one of the
+   following macros to a function pointer or array type, you need to typedef
+   it first and use the typedef name.  */
+
+/* Allocate an object of type T dynamically, with error checking.  */
+/* extern t *XMALLOC (typename 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))))
+
+/* 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)))
+
+/* 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);
+   void *xnrealloc (void *p, size_t n, size_t s);
+   void *x2nrealloc (void *p, size_t *pn, size_t s);
+   char *xcharalloc (size_t n);
+# endif
+
+# 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 *
+static_inline void *
 xnmalloc (size_t n, size_t s)
 {
   if (xalloc_oversized (n, s))
@@ -82,7 +120,7 @@ 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 *
+static_inline void *
 xnrealloc (void *p, size_t n, size_t s)
 {
   if (xalloc_oversized (n, s))
@@ -145,7 +183,7 @@ xnrealloc (void *p, size_t n, size_t s)
 
    */
 
-static inline void *
+static_inline void *
 x2nrealloc (void *p, size_t *pn, size_t s)
 {
   size_t n = *pn;
@@ -175,37 +213,17 @@ x2nrealloc (void *p, size_t *pn, size_t 
   return xrealloc (p, n * s);
 }
 
-/* In the following macros, T must be an elementary or structure/union or
-   typedef'ed type, or a pointer to such a type.  To apply one of the
-   following macros to a function pointer or array type, you need to typedef
-   it first and use the typedef name.  */
-
-/* Allocate an object of type T dynamically, with error checking.  */
-/* extern t *XMALLOC (typename 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 *) 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)))
-
-/* 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 *) xcalloc (n, sizeof (t)))
-
 /* Return a pointer to a new buffer of N bytes.  This is like xmalloc,
    except it returns char *.  */
-static inline char *
+
+static_inline char *
 xcharalloc (size_t n)
 {
   return XNMALLOC (n, char);
 }
 
+# endif
+
 # ifdef __cplusplus
 }
 
Index: lib/xmalloc.c
===================================================================
RCS file: /cvsroot/gnulib/gnulib/lib/xmalloc.c,v
retrieving revision 1.40
diff -p -u -r1.40 xmalloc.c
--- lib/xmalloc.c       6 Nov 2006 21:24:35 -0000       1.40
+++ lib/xmalloc.c       8 Nov 2006 00:21:58 -0000
@@ -20,7 +20,11 @@
 
 #include <config.h>
 
+#if ! HAVE_INLINE
+# define static_inline
+#endif
 #include "xalloc.h"
+#undef static_inline
 
 #include <stdlib.h>
 #include <string.h>
Index: m4/xalloc.m4
===================================================================
RCS file: /cvsroot/gnulib/gnulib/m4/xalloc.m4,v
retrieving revision 1.17
diff -p -u -r1.17 xalloc.m4
--- m4/xalloc.m4        6 Nov 2006 21:38:27 -0000       1.17
+++ m4/xalloc.m4        8 Nov 2006 00:21:58 -0000
@@ -1,4 +1,4 @@
-# xalloc.m4 serial 15
+# xalloc.m4 serial 16
 dnl Copyright (C) 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -14,7 +14,7 @@ AC_DEFUN([gl_XALLOC],
 
 # Prerequisites of lib/xalloc.h.
 AC_DEFUN([gl_PREREQ_XALLOC], [
-  AC_REQUIRE([AC_C_INLINE])
+  AC_REQUIRE([gl_INLINE])
   :
 ])
 
Index: modules/xalloc
===================================================================
RCS file: /cvsroot/gnulib/gnulib/modules/xalloc,v
retrieving revision 1.19
diff -p -u -r1.19 xalloc
--- modules/xalloc      13 Oct 2006 12:40:23 -0000      1.19
+++ modules/xalloc      8 Nov 2006 00:21:58 -0000
@@ -5,6 +5,7 @@ Files:
 lib/xalloc.h
 lib/xmalloc.c
 m4/xalloc.m4
+m4/inline.m4
 
 Depends-on:
 xalloc-die




reply via email to

[Prev in Thread] Current Thread [Next in Thread]