Patch: malloc function attribute

Kaveh R. Ghazi
Sun Nov 28 22:35:00 GMT 1999

 > From: Anthony Green <>
 > Jeff wrote:
 > > Otherwise it looks great.  Please install it.
 > Done (with your suggestions).  Thanks,
 > AG

Hey great!  I suggested this in September hoping someone would
consider it to be worthwhile, but the discussion trailed off when no
one could decide if __restrict__ would already satisfy the
functionality.  (Any idea about that?)

Here's the URL in case you're interested:

Anyway, now that the malloc attribute has been added, I'm submitting
the following patch to make use of it in the gcc sources.

QUESTION: should realloc and similar functions which could possibly
return the same pointer more than once have this attribute?  I don't
think so, so I didn't mark xrealloc or spaces.  If someone can confirm
either way, I'll either add them or remove realloc from the builtin
list.  (BTW, what the heck is spaces good for anyway?)

So if someone would please answer the above and let me know if the
following is okay to install, I would appreciate it.


1999-11-28  Kaveh R. Ghazi  <>

	* calls.c (special_function_p): Add `strdup' to the list of
	functions which have attribute malloc by default.

	* ansidecl.h (ATTRIBUTE_MALLOC): New macro.

	* libiberty.h (buildargv, dupargv, concat, choose_temp_base,
	make_temp_file, xmalloc, xcalloc, xstrdup, xmemdup): Add
	(xatexit): Remove __GNUC__ check, add ATTRIBUTE_NORETURN.

diff -rup orig/egcs-CVS19991128/gcc/calls.c egcs-CVS19991128/gcc/calls.c
--- orig/egcs-CVS19991128/gcc/calls.c	Tue Nov 16 15:35:21 1999
+++ egcs-CVS19991128/gcc/calls.c	Sun Nov 28 22:50:53 1999
@@ -607,6 +607,7 @@ special_function_p (name, fndecl, return
       else if (! strcmp (tname, "malloc")
 	       || ! strcmp (tname, "calloc")
 	       || ! strcmp (tname, "realloc")
+	       || ! strcmp (tname, "strdup")
 	       /* Note use of NAME rather than TNAME here.  These functions
 		  are only reserved when preceded with __.  */
 	       || ! strcmp (name, "__vn")	/* mangled __builtin_vec_new */
diff -rup orig/egcs-CVS19991128/include/ansidecl.h egcs-CVS19991128/include/ansidecl.h
--- orig/egcs-CVS19991128/include/ansidecl.h	Wed Nov 10 12:32:09 1999
+++ egcs-CVS19991128/include/ansidecl.h	Sun Nov 28 22:19:03 1999
@@ -187,6 +187,15 @@ So instead we use the macro below and te
 # define __attribute__(x)
+/* Attribute __malloc__ on functions was valid as of gcc 2.96. */
+# if (GCC_VERSION >= 2096)
+#  define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
+# else
+# endif /* GNUC >= 2.96 */
+#endif /* ATTRIBUTE_MALLOC */
 /* Attributes on labels were valid as of gcc 2.93. */
 # if (GCC_VERSION >= 2093)
diff -rup orig/egcs-CVS19991128/include/libiberty.h egcs-CVS19991128/include/libiberty.h
--- orig/egcs-CVS19991128/include/libiberty.h	Sun Nov 28 22:59:05 1999
+++ egcs-CVS19991128/include/libiberty.h	Sun Nov 28 22:29:19 1999
@@ -19,7 +19,7 @@ extern "C" {
 /* Build an argument vector from a string.  Allocates memory using
    malloc.  Use freeargv to free the vector.  */
-extern char **buildargv PARAMS ((char *));
+extern char **buildargv PARAMS ((char *)) ATTRIBUTE_MALLOC;
 /* Free a vector returned by buildargv.  */
@@ -28,7 +28,7 @@ extern void freeargv PARAMS ((char **));
 /* Duplicate an argument vector. Allocates memory using malloc.  Use
    freeargv to free the vector.  */
-extern char **dupargv PARAMS ((char **));
+extern char **dupargv PARAMS ((char **)) ATTRIBUTE_MALLOC;
 /* Return the last component of a path name.  Note that we can't use a
@@ -45,7 +45,7 @@ extern char *basename ();
 /* Concatenate an arbitrary number of strings, up to (char *) NULL.
    Allocates memory using xmalloc.  */
-extern char *concat PARAMS ((const char *, ...));
+extern char *concat PARAMS ((const char *, ...)) ATTRIBUTE_MALLOC;
 /* Check whether two file descriptors refer to the same file.  */
@@ -62,11 +62,11 @@ extern long get_run_time PARAMS ((void))
 /* Choose a temporary directory to use for scratch files.  */
-extern char *choose_temp_base PARAMS ((void));
+extern char *choose_temp_base PARAMS ((void)) ATTRIBUTE_MALLOC;
 /* Return a temporary file name or NULL if unable to create one.  */
-extern char *make_temp_file PARAMS ((const char *));
+extern char *make_temp_file PARAMS ((const char *)) ATTRIBUTE_MALLOC;
 /* Allocate memory filled with spaces.  Allocates using malloc.  */
@@ -117,11 +117,7 @@ extern int xatexit PARAMS ((void (*fn) (
 /* Exit, calling all the functions registered with xatexit.  */
-#ifndef __GNUC__
-extern void xexit PARAMS ((int status));
-void xexit PARAMS ((int status)) __attribute__ ((noreturn));
+extern void xexit PARAMS ((int status)) ATTRIBUTE_NORETURN;
 /* Set the program name used by xmalloc.  */
@@ -137,7 +133,7 @@ extern void xmalloc_set_program_name PAR
 /* Get a definition for va_list.  */
 #include <stdarg.h>
-extern PTR xmalloc PARAMS ((size_t));
+extern PTR xmalloc PARAMS ((size_t)) ATTRIBUTE_MALLOC;
 /* Reallocate memory without fail.  This works like xmalloc.  */
@@ -146,15 +142,15 @@ extern PTR xrealloc PARAMS ((PTR, size_t
 /* Allocate memory without fail and set it to zero.  This works like
    xmalloc.  */
-extern PTR xcalloc PARAMS ((size_t, size_t));
+extern PTR xcalloc PARAMS ((size_t, size_t)) ATTRIBUTE_MALLOC;
 /* Copy a string into a memory buffer without fail.  */
-extern char *xstrdup PARAMS ((const char *));
+extern char *xstrdup PARAMS ((const char *)) ATTRIBUTE_MALLOC;
 /* Copy an existing memory buffer to a new memory buffer without fail.  */
-extern PTR xmemdup PARAMS ((const PTR, size_t, size_t));
+extern PTR xmemdup PARAMS ((const PTR, size_t, size_t)) ATTRIBUTE_MALLOC;
 /* hex character manipulation routines */

More information about the Gcc-patches mailing list