From 2af49e60a13b8d2a7aba47cd6af70217d0e263e5 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 31 Mar 2008 04:17:22 +0000 Subject: [PATCH] Add macros wrapping the gcc alloc_size function attribute. (#523019, 2008-03-30 Matthias Clasen * glib/gmacros.h: Add macros wrapping the gcc alloc_size function attribute. (#523019, Rodrigo Moya) * glib/gmem.h: * glib/gslice.h: * glib/gstrfuncs.h: Use the new attribute where appropriate. svn path=/trunk/; revision=6781 --- ChangeLog | 9 ++++++++ docs/reference/ChangeLog | 6 +++++ docs/reference/glib/glib-sections.txt | 2 ++ docs/reference/glib/tmpl/macros_misc.sgml | 37 ++++++++++++++++++++++++++----- glib/gmacros.h | 8 +++++++ glib/gmem.h | 8 +++---- glib/gslice.h | 6 ++--- glib/gstrfuncs.h | 2 +- 8 files changed, 65 insertions(+), 13 deletions(-) diff --git a/ChangeLog b/ChangeLog index fc66709..92bfde8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,14 @@ 2008-03-30 Matthias Clasen + * glib/gmacros.h: Add macros wrapping the gcc alloc_size + function attribute. (#523019, Rodrigo Moya) + + * glib/gmem.h: + * glib/gslice.h: + * glib/gstrfuncs.h: Use the new attribute where appropriate. + +2008-03-30 Matthias Clasen + * glib/glibintl.h: * glib/gstrfuncs.c: * glib/gutils.c: Simple fixes to help building GLib on diff --git a/docs/reference/ChangeLog b/docs/reference/ChangeLog index 9ef8363..e0030fb 100644 --- a/docs/reference/ChangeLog +++ b/docs/reference/ChangeLog @@ -1,3 +1,9 @@ +2008-03-30 Matthias Clasen + + * glib/glib-sections.txt: + * glib/tmpl/macros-misc.sgml: Document G_GNUC_ALLOC_SIZE + macros + 2008-03-28 A. Walton * gio/overview.xml: diff --git a/docs/reference/glib/glib-sections.txt b/docs/reference/glib/glib-sections.txt index dcaceae..c94aff7 100644 --- a/docs/reference/glib/glib-sections.txt +++ b/docs/reference/glib/glib-sections.txt @@ -324,6 +324,8 @@ G_GNUC_EXTENSION G_GNUC_CONST G_GNUC_PURE G_GNUC_MALLOC +G_GNUC_ALLOC_SIZE +G_GNUC_ALLOC_SIZE2 G_GNUC_DEPRECATED G_GNUC_NORETURN G_GNUC_UNUSED diff --git a/docs/reference/glib/tmpl/macros_misc.sgml b/docs/reference/glib/tmpl/macros_misc.sgml index 3a78599..d354b4c 100644 --- a/docs/reference/glib/tmpl/macros_misc.sgml +++ b/docs/reference/glib/tmpl/macros_misc.sgml @@ -141,17 +141,44 @@ See the GNU C documentation for details. -Expands to the GNU C malloc function attribute if the compiler is -gcc. Declaring a function as malloc enables better optimization of the -function. A function can have the malloc attribute if it returns a pointer which is guaranteed -to not alias with any other pointer when the function returns (in practice, this means newly -allocated memory). +Expands to the GNU C malloc function attribute if the +compiler is gcc. Declaring a function as malloc enables +better optimization of the function. A function can have the malloc attribute +if it returns a pointer which is guaranteed to not alias with any other pointer +when the function returns (in practice, this means newly allocated memory). See the GNU C documentation for details. @Since: 2.6 + + +Expands to the GNU C alloc_size function attribute if the +compiler is a new enough gcc. This attribute tells the +compiler that the function returns a pointer to memory of a size that is +specified by the @xth function parameter. +See the GNU C documentation for details. + + +@x: the index of the argument specifying the allocation size +@Since: 2.18 + + + + +Expands to the GNU C alloc_size function attribute if the +compiler is a new enough gcc. This attribute tells the +compiler that the function returns a pointer to memory of a size that is +specified by the product of two function parameters. +See the GNU C documentation for details. + + +@x: the index of the argument specifying one factor of the allocation size +@y: the index of the argument specifying the second factor of the allocation size +@Since: 2.18 + + Expands to the GNU C deprecated attribute if the compiler diff --git a/glib/gmacros.h b/glib/gmacros.h index 997e7b9..ad52ebb 100644 --- a/glib/gmacros.h +++ b/glib/gmacros.h @@ -67,6 +67,14 @@ #define G_GNUC_NULL_TERMINATED #endif +#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) +#define G_GNUC_ALLOC_SIZE(x) __attribute__((__alloc_size__(x))) +#define G_GNUC_ALLOC_SIZE2(x,y) __attribute__((__alloc_size__(x,y))) +#else +#define G_GNUC_ALLOC_SIZE(x) +#define G_GNUC_ALLOC_SIZE2(x,y) +#endif + #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ > 4) #define G_GNUC_PRINTF( format_idx, arg_idx ) \ __attribute__((__format__ (__printf__, format_idx, arg_idx))) diff --git a/glib/gmem.h b/glib/gmem.h index 5c83d9f..2bea153 100644 --- a/glib/gmem.h +++ b/glib/gmem.h @@ -48,13 +48,13 @@ typedef struct _GMemVTable GMemVTable; /* Memory allocation functions */ -gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC; -gpointer g_malloc0 (gsize n_bytes) G_GNUC_MALLOC; +gpointer g_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1); +gpointer g_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1); gpointer g_realloc (gpointer mem, gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT; void g_free (gpointer mem); -gpointer g_try_malloc (gsize n_bytes) G_GNUC_MALLOC; -gpointer g_try_malloc0 (gsize n_bytes) G_GNUC_MALLOC; +gpointer g_try_malloc (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1); +gpointer g_try_malloc0 (gsize n_bytes) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1); gpointer g_try_realloc (gpointer mem, gsize n_bytes) G_GNUC_WARN_UNUSED_RESULT; diff --git a/glib/gslice.h b/glib/gslice.h index 56dc73f..835d124 100644 --- a/glib/gslice.h +++ b/glib/gslice.h @@ -34,10 +34,10 @@ G_BEGIN_DECLS /* slices - fast allocation/release of small memory blocks */ -gpointer g_slice_alloc (gsize block_size) G_GNUC_MALLOC; -gpointer g_slice_alloc0 (gsize block_size) G_GNUC_MALLOC; +gpointer g_slice_alloc (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1); +gpointer g_slice_alloc0 (gsize block_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1); gpointer g_slice_copy (gsize block_size, - gconstpointer mem_block) G_GNUC_MALLOC; + gconstpointer mem_block) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(1); void g_slice_free1 (gsize block_size, gpointer mem_block); void g_slice_free_chain_with_offset (gsize block_size, diff --git a/glib/gstrfuncs.h b/glib/gstrfuncs.h index f94ccb3..18b4949 100644 --- a/glib/gstrfuncs.h +++ b/glib/gstrfuncs.h @@ -218,7 +218,7 @@ gchar* g_strescape (const gchar *source, const gchar *exceptions) G_GNUC_MALLOC; gpointer g_memdup (gconstpointer mem, - guint byte_size) G_GNUC_MALLOC; + guint byte_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(2); /* NULL terminated string arrays. * g_strsplit(), g_strsplit_set() split up string into max_tokens tokens -- 2.7.4