Use GCC's `malloc' attribute for malloc-like routines.
authorLudovic Courtès <ludo@gnu.org>
Fri, 28 May 2010 13:29:12 +0000 (15:29 +0200)
committerLudovic Courtès <ludo@gnu.org>
Fri, 28 May 2010 15:02:13 +0000 (17:02 +0200)
* libguile/__scm.h (SCM_MALLOC): New macro.

* libguile/gc.h (scm_malloc, scm_calloc, scm_strdup, scm_strndup,
  scm_gc_malloc_pointerless, scm_gc_calloc, scm_gc_malloc,
  scm_gc_strdup, scm_gc_strndup): Mark as `SCM_MALLOC'.

libguile/__scm.h
libguile/gc.h

index 93f6662..f0373e8 100644 (file)
@@ -3,7 +3,7 @@
 #ifndef SCM___SCM_H
 #define SCM___SCM_H
 
-/* Copyright (C) 1995,1996,1998,1999,2000,2001,2002,2003, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
+/* Copyright (C) 1995,1996,1998,1999,2000,2001,2002,2003, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
 # undef SCM_ALIGNED
 #endif
 
+/* The SCM_MALLOC macro can be used in function declarations to tell the
+ * compiler that a function may be treated as if any non-NULL pointer it returns
+ * cannot alias any other pointer valid when the function returns.  */
+#if defined (__GNUC__) && (__GNUC__ >= 3)
+# define SCM_MALLOC  __attribute__ ((__malloc__))
+#else
+# define SCM_MALLOC
+#endif
+
 \f
 /* {Supported Options}
  *
index 8f05aab..3c10b8a 100644 (file)
@@ -182,23 +182,28 @@ SCM_API void scm_i_gc (const char *what);
 SCM_API void scm_gc_mark (SCM p);
 SCM_API void scm_gc_sweep (void);
 
-SCM_API void *scm_malloc (size_t size);
-SCM_API void *scm_calloc (size_t size);
+SCM_API void *scm_malloc (size_t size) SCM_MALLOC;
+SCM_API void *scm_calloc (size_t size) SCM_MALLOC;
 SCM_API void *scm_realloc (void *mem, size_t size);
-SCM_API char *scm_strdup (const char *str);
-SCM_API char *scm_strndup (const char *str, size_t n);
+SCM_API char *scm_strdup (const char *str) SCM_MALLOC;
+SCM_API char *scm_strndup (const char *str, size_t n) SCM_MALLOC;
 SCM_API void scm_gc_register_collectable_memory (void *mem, size_t size,
                                                 const char *what);
 SCM_API void scm_gc_unregister_collectable_memory (void *mem, size_t size,
                                                   const char *what);
-SCM_API void *scm_gc_malloc_pointerless (size_t size, const char *what);
-SCM_API void *scm_gc_calloc (size_t size, const char *what);
-SCM_API void *scm_gc_malloc (size_t size, const char *what);
+SCM_API void *scm_gc_malloc_pointerless (size_t size, const char *what)
+  SCM_MALLOC;
+SCM_API void *scm_gc_calloc (size_t size, const char *what)
+  SCM_MALLOC;
+SCM_API void *scm_gc_malloc (size_t size, const char *what)
+  SCM_MALLOC;
 SCM_API void *scm_gc_realloc (void *mem, size_t old_size, 
                              size_t new_size, const char *what);
 SCM_API void scm_gc_free (void *mem, size_t size, const char *what);
-SCM_API char *scm_gc_strdup (const char *str, const char *what);
-SCM_API char *scm_gc_strndup (const char *str, size_t n, const char *what);
+SCM_API char *scm_gc_strdup (const char *str, const char *what)
+  SCM_MALLOC;
+SCM_API char *scm_gc_strndup (const char *str, size_t n, const char *what)
+  SCM_MALLOC;
 
 SCM_API void scm_remember_upto_here_1 (SCM obj);
 SCM_API void scm_remember_upto_here_2 (SCM obj1, SCM obj2);