Add macros wrapping the gcc alloc_size function attribute. (#523019,
authorMatthias Clasen <mclasen@redhat.com>
Mon, 31 Mar 2008 04:17:22 +0000 (04:17 +0000)
committerMatthias Clasen <matthiasc@src.gnome.org>
Mon, 31 Mar 2008 04:17:22 +0000 (04:17 +0000)
2008-03-30  Matthias Clasen  <mclasen@redhat.com>

        * 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
docs/reference/ChangeLog
docs/reference/glib/glib-sections.txt
docs/reference/glib/tmpl/macros_misc.sgml
glib/gmacros.h
glib/gmem.h
glib/gslice.h
glib/gstrfuncs.h

index fc66709723b9ddd83ca75b875645c7185b8407ed..92bfde8fe82ab583e086e954735f169ac2c3d7b3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2008-03-30  Matthias Clasen  <mclasen@redhat.com>
+
+       * 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  <mclasen@redhat.com>
 
        * glib/glibintl.h:
index 9ef836398779d86fea6c85b136a99963bc8b9196..e0030fbec0bb5496e6091fdf0a12fac89077d3f0 100644 (file)
@@ -1,3 +1,9 @@
+2008-03-30  Matthias Clasen  <mclasen@redhat.com>
+
+       * glib/glib-sections.txt:
+       * glib/tmpl/macros-misc.sgml: Document G_GNUC_ALLOC_SIZE
+       macros
+
 2008-03-28  A. Walton  <awalton@svn.gnome.org>
 
        * gio/overview.xml:
index dcaceae81f8d79ea3ddd6b83d1daa9c82a63f3c1..c94aff72d6090dc452d6e6b86f46723d10680119 100644 (file)
@@ -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
index 3a78599686fbe5e80d910331d3d5d6dbadb56c80..d354b4c68d6c5834c514eb8107bd109d8f69ffeb 100644 (file)
@@ -141,17 +141,44 @@ See the GNU C documentation for details.
 
 <!-- ##### MACRO G_GNUC_MALLOC ##### -->
 <para>
-Expands to the GNU C <literal>malloc</literal> function attribute if the compiler is 
-<command>gcc</command>. 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 <literal>malloc</literal> function attribute if the 
+compiler is <command>gcc</command>. 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. 
 </para>
 
 @Since: 2.6
 
 
+<!-- ##### MACRO G_GNUC_ALLOC_SIZE ##### -->
+<para>
+Expands to the GNU C <literal>alloc_size</literal> function attribute if the 
+compiler is a new enough <command>gcc</command>. This attribute tells the
+compiler that the function returns a pointer to memory of a size that is
+specified by the @x<!-- -->th function parameter.
+See the GNU C documentation for details. 
+</para>
+
+@x: the index of the argument specifying the allocation size 
+@Since: 2.18
+
+
+<!-- ##### MACRO G_GNUC_ALLOC_SIZE2 ##### -->
+<para>
+Expands to the GNU C <literal>alloc_size</literal> function attribute if the 
+compiler is a new enough <command>gcc</command>. 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. 
+</para>
+
+@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
+
+
 <!-- ##### MACRO G_GNUC_DEPRECATED ##### -->
 <para>
 Expands to the GNU C <literal>deprecated</literal> attribute if the compiler 
index 997e7b9781398fcb1854f4deae894e2b85e317bb..ad52ebb43b885ab64ffe0a1c6f9a733eca71455e 100644 (file)
 #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)))
index 5c83d9f78e9337ce416fdb38a5142eb32b6e9f13..2bea153131471975b26e1269cb83f6716a9c1d89 100644 (file)
@@ -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;
 
index 56dc73fb800ac976f59cdd2ba91436245b7ee61d..835d12476c4ea2a06997b55634e58b4bb59534ae 100644 (file)
@@ -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,
index f94ccb3fda3550c60b3d56753d929b436dd4cf0d..18b4949515b9f9e8c0577498a1a426892e68cde3 100644 (file)
@@ -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