]> code.delx.au - gnu-emacs/commitdiff
Supply malloc and alloc_size attributes for extern allocators.
authorPaul Eggert <eggert@cs.ucla.edu>
Thu, 22 May 2014 16:40:35 +0000 (09:40 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Thu, 22 May 2014 16:40:35 +0000 (09:40 -0700)
This documents the C API, and helps GCC generate a bit better code.
* conf_post.h (ATTRIBUTE_MALLOC, ATTRIBUTE_ALLOC_SIZE)
(ATTRIBUTE_MALLOC_SIZE): New macros.
* gmalloc.c (malloc, realloc, calloc):
* gtkutil.h (malloc_widget_value):
* lisp.h (ralloc, r_re_alloc, xmalloc, xzalloc, xrealloc, xnmalloc)
(xnrealloc, xstrdup, xlispstrdup, record_xmalloc):
Use them.

src/ChangeLog
src/conf_post.h
src/gmalloc.c
src/gtkutil.h
src/lisp.h

index 2903939537408196156ca5ecbb17872da4cea3b1..5c3486d131a44e1922384da3a5fe64403ada3b07 100644 (file)
@@ -1,3 +1,15 @@
+2014-05-22  Paul Eggert  <eggert@cs.ucla.edu>
+
+       Supply malloc and alloc_size attributes for extern allocators.
+       This documents the C API, and helps GCC generate a bit better code.
+       * conf_post.h (ATTRIBUTE_MALLOC, ATTRIBUTE_ALLOC_SIZE)
+       (ATTRIBUTE_MALLOC_SIZE): New macros.
+       * gmalloc.c (malloc, realloc, calloc):
+       * gtkutil.h (malloc_widget_value):
+       * lisp.h (ralloc, r_re_alloc, xmalloc, xzalloc, xrealloc, xnmalloc)
+       (xnrealloc, xstrdup, xlispstrdup, record_xmalloc):
+       Use them.
+
 2014-05-21  Paul Eggert  <eggert@cs.ucla.edu>
 
        Don't assume that ImageMagick uses a 16-bit quantum (Bug#17519).
index 123f4803da5bece47141ad764ab2e0d73cd885e5..6f6af3d3e02d246d87e25f63eb78b8146a90903d 100644 (file)
@@ -225,6 +225,20 @@ extern void _DebPrint (const char *fmt, ...);
 
 #define ATTRIBUTE_CONST _GL_ATTRIBUTE_CONST
 
+#if 3 <= __GNUC__
+# define ATTRIBUTE_MALLOC __attribute__ ((__malloc__))
+#else
+# define ATTRIBUTE_MALLOC
+#endif
+
+#if 4 < __GNUC__ + (3 <= __GNUC_MINOR__)
+# define ATTRIBUTE_ALLOC_SIZE(args) __attribute__ ((__alloc_size__ args))
+#else
+# define ATTRIBUTE_ALLOC_SIZE(args)
+#endif
+
+#define ATTRIBUTE_MALLOC_SIZE(args) ATTRIBUTE_MALLOC ATTRIBUTE_ALLOC_SIZE (args)
+
 /* Work around GCC bug 59600: when a function is inlined, the inlined
    code may have its addresses sanitized even if the function has the
    no_sanitize_address attribute.  This bug is fixed in GCC 4.9.0 and
index 977abbdbbbd2b664310434f96b25007fbdc4227d..ab1dfd07db2ac25a12b7371be730902cb3775398 100644 (file)
@@ -51,12 +51,12 @@ extern "C"
 
 
 /* Allocate SIZE bytes of memory.  */
-extern void *malloc (size_t size);
+extern void *malloc (size_t size) ATTRIBUTE_MALLOC_SIZE ((1));
 /* Re-allocate the previously allocated block
    in ptr, making the new block SIZE bytes long.  */
-extern void *realloc (void *ptr, size_t size);
+extern void *realloc (void *ptr, size_t size) ATTRIBUTE_ALLOC_SIZE ((2));
 /* Allocate NMEMB elements of SIZE bytes each, all initialized to 0.  */
-extern void *calloc (size_t nmemb, size_t size);
+extern void *calloc (size_t nmemb, size_t size) ATTRIBUTE_MALLOC_SIZE ((1,2));
 /* Free a block allocated by `malloc', `realloc' or `calloc'.  */
 extern void free (void *ptr);
 
index 12bf461fd69f19e04e1a0faacce239e129c690cf..b576fc6d9fea6bb0ab4bcbc91b55df32be2312e2 100644 (file)
@@ -74,7 +74,7 @@ typedef struct xg_menu_item_cb_data_
 
 } xg_menu_item_cb_data;
 
-extern struct _widget_value *malloc_widget_value (void);
+extern struct _widget_value *malloc_widget_value (void) ATTRIBUTE_MALLOC;
 extern void free_widget_value (struct _widget_value *);
 
 extern bool xg_uses_old_file_dialog (void) ATTRIBUTE_CONST;
index 67b26ef91c7a13f46f45aeaba1dbf74a19e30716..40dd03c4fc4828b08e6dd763b5fe8f65719220a1 100644 (file)
@@ -3755,9 +3755,9 @@ INLINE void (check_cons_list) (void) { lisp_h_check_cons_list (); }
 
 #ifdef REL_ALLOC
 /* Defined in ralloc.c.  */
-extern void *r_alloc (void **, size_t);
+extern void *r_alloc (void **, size_t) ATTRIBUTE_ALLOC_SIZE ((2));
 extern void r_alloc_free (void **);
-extern void *r_re_alloc (void **, size_t);
+extern void *r_re_alloc (void **, size_t) ATTRIBUTE_ALLOC_SIZE ((2));
 extern void r_alloc_reset_variable (void **, void **);
 extern void r_alloc_inhibit_buffer_relocation (int);
 #endif
@@ -4391,16 +4391,17 @@ extern bool initialized;
 /* True means ^G can quit instantly.  */
 extern bool immediate_quit;
 
-extern void *xmalloc (size_t);
-extern void *xzalloc (size_t);
-extern void *xrealloc (void *, size_t);
+extern void *xmalloc (size_t) ATTRIBUTE_MALLOC_SIZE ((1));
+extern void *xzalloc (size_t) ATTRIBUTE_MALLOC_SIZE ((1));
+extern void *xrealloc (void *, size_t) ATTRIBUTE_ALLOC_SIZE ((2));
 extern void xfree (void *);
-extern void *xnmalloc (ptrdiff_t, ptrdiff_t);
-extern void *xnrealloc (void *, ptrdiff_t, ptrdiff_t);
+extern void *xnmalloc (ptrdiff_t, ptrdiff_t) ATTRIBUTE_MALLOC_SIZE ((1,2));
+extern void *xnrealloc (void *, ptrdiff_t, ptrdiff_t)
+  ATTRIBUTE_ALLOC_SIZE ((2,3));
 extern void *xpalloc (void *, ptrdiff_t *, ptrdiff_t, ptrdiff_t, ptrdiff_t);
 
-extern char *xstrdup (const char *);
-extern char *xlispstrdup (Lisp_Object);
+extern char *xstrdup (const char *) ATTRIBUTE_MALLOC;
+extern char *xlispstrdup (Lisp_Object) ATTRIBUTE_MALLOC;
 extern void dupstring (char **, char const *);
 extern void xputenv (const char *);
 
@@ -4432,7 +4433,7 @@ extern void init_system_name (void);
 
 enum MAX_ALLOCA { MAX_ALLOCA = 16 * 1024 };
 
-extern void *record_xmalloc (size_t);
+extern void *record_xmalloc (size_t) ATTRIBUTE_ALLOC_SIZE ((1));
 
 #define USE_SAFE_ALLOCA                        \
   ptrdiff_t sa_count = SPECPDL_INDEX (); bool sa_must_free = false