changeset 10074:cf0a8e173c39

Help GCC to do better code generation.
author Bruno Haible <bruno@clisp.org>
date Thu, 15 May 2008 02:01:46 +0200
parents 0f65e1606f27
children cc7bfc9f7fc1
files ChangeLog lib/eealloc.h lib/pagealign_alloc.h lib/xalloc.h
diffstat 4 files changed, 41 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2008-05-14  Bruno Haible  <bruno@clisp.org>
+
+	Help GCC to do better code generation.
+	* lib/eealloc.h (eemalloc) [GCC >= 3]: Declare with attribute 'malloc'.
+	* lib/pagealign_alloc.h (pagealign_alloc, pagealign_xalloc): Likewise.
+	* lib/xalloc.h (ATTRIBUTE_MALLOC): New macro.
+	(xmalloc, xzalloc, xcalloc, xmemdup, xstrdup, xnmalloc, xcharalloc):
+	Declare with attribute 'malloc' if supported.
+
 2008-05-14  Lasse Collin  <lasse.collin@tukaani.org>
 
 	use "echo STR|wc -c" rather than unportable "expr length STR"
--- a/lib/eealloc.h
+++ b/lib/eealloc.h
@@ -1,5 +1,5 @@
 /* Memory allocation with expensive empty allocations.
-   Copyright (C) 2003 Free Software Foundation, Inc.
+   Copyright (C) 2003, 2008 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003,
    based on prior work by Jim Meyering.
 
@@ -34,6 +34,9 @@
 #if MALLOC_0_IS_NONNULL
 # define eemalloc malloc
 #else
+# if __GNUC__ >= 3
+static inline void *eemalloc (size_t n) __attribute__ ((__malloc__));
+# endif
 static inline void *
 eemalloc (size_t n)
 {
--- a/lib/pagealign_alloc.h
+++ b/lib/pagealign_alloc.h
@@ -1,6 +1,6 @@
 /* Memory allocation aligned to system page boundaries.
 
-   Copyright (C) 2005 Free Software Foundation, Inc.
+   Copyright (C) 2005, 2008 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -26,11 +26,19 @@
    to the next multiple.
    Return a pointer to the start of the memory block. Upon allocation failure,
    return NULL and set errno.  */
-extern void *pagealign_alloc (size_t size);
+extern void *pagealign_alloc (size_t size)
+# if __GNUC__ >= 3
+     __attribute__ ((__malloc__))
+# endif
+     ;
 
 /* Like pagealign_alloc, except it exits the program if the allocation
    fails.  */
-extern void *pagealign_xalloc (size_t size);
+extern void *pagealign_xalloc (size_t size)
+# if __GNUC__ >= 3
+     __attribute__ ((__malloc__))
+# endif
+     ;
 
 /* Free a memory block.
    PTR must be a non-NULL pointer returned by pagealign_alloc or
--- a/lib/xalloc.h
+++ b/lib/xalloc.h
@@ -37,6 +37,14 @@
 #  define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
 # endif
 
+# ifndef ATTRIBUTE_MALLOC
+#  if __GNUC__ >= 3
+#   define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
+#  else
+#   define ATTRIBUTE_MALLOC
+#  endif
+# endif
+
 /* This function is always triggered when memory is exhausted.
    It must be defined by the application, either explicitly
    or by using gnulib's xalloc-die module.  This is the
@@ -44,13 +52,13 @@
    memory allocation failure.  */
 extern void xalloc_die (void) ATTRIBUTE_NORETURN;
 
-void *xmalloc (size_t s);
-void *xzalloc (size_t s);
-void *xcalloc (size_t n, size_t s);
+void *xmalloc (size_t s) ATTRIBUTE_MALLOC;
+void *xzalloc (size_t s) ATTRIBUTE_MALLOC;
+void *xcalloc (size_t n, size_t s) ATTRIBUTE_MALLOC;
 void *xrealloc (void *p, size_t s);
 void *x2realloc (void *p, size_t *pn);
-void *xmemdup (void const *p, size_t s);
-char *xstrdup (char const *str);
+void *xmemdup (void const *p, size_t s) ATTRIBUTE_MALLOC;
+char *xstrdup (char const *str) ATTRIBUTE_MALLOC;
 
 /* Return 1 if an array of N objects, each of size S, cannot exist due
    to size arithmetic overflow.  S must be positive and N must be
@@ -97,10 +105,10 @@
 # if HAVE_INLINE
 #  define static_inline static inline
 # else
-   void *xnmalloc (size_t n, size_t s);
+   void *xnmalloc (size_t n, size_t s) ATTRIBUTE_MALLOC;
    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);
+   char *xcharalloc (size_t n) ATTRIBUTE_MALLOC;;
 # endif
 
 # ifdef static_inline
@@ -108,6 +116,7 @@
 /* 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) ATTRIBUTE_MALLOC;
 static_inline void *
 xnmalloc (size_t n, size_t s)
 {
@@ -219,6 +228,7 @@
 /* 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) ATTRIBUTE_MALLOC;
 static_inline char *
 xcharalloc (size_t n)
 {