X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=lib%2Fxalloc.h;h=6122cc58b05d87da38d9f11f9dbcd849113c5b31;hb=edb5054087db6afdc3ab2cc645ce2d891ee88c6d;hp=314ed6d19e8387d4d92474473e6717583b29dba1;hpb=34ebad3df7a99eea326f9170f2517b5d23873d1b;p=gnulib.git diff --git a/lib/xalloc.h b/lib/xalloc.h index 314ed6d19..6122cc58b 100644 --- a/lib/xalloc.h +++ b/lib/xalloc.h @@ -1,7 +1,8 @@ /* xalloc.h -- malloc with out-of-memory checking - Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, - 1999, 2000, 2003, 2004, 2006, 2007, 2008 Free Software Foundation, Inc. + Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, + 2000, 2003, 2004, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, + Inc. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -37,6 +38,14 @@ extern "C" { # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) # endif +# ifndef ATTRIBUTE_MALLOC +# if __GNUC__ >= 3 +# define ATTRIBUTE_MALLOC __attribute__ ((__malloc__)) +# else +# define ATTRIBUTE_MALLOC +# endif +# endif + /* This function is always triggered when memory is exhausted. It must be defined by the application, either explicitly or by using gnulib's xalloc-die module. This is the @@ -44,14 +53,13 @@ extern "C" { memory allocation failure. */ extern void xalloc_die (void) ATTRIBUTE_NORETURN; -void *xmalloc (size_t s); -void *xzalloc (size_t s); -void *xcalloc (size_t n, size_t s); +void *xmalloc (size_t s) ATTRIBUTE_MALLOC; +void *xzalloc (size_t s) ATTRIBUTE_MALLOC; +void *xcalloc (size_t n, size_t s) ATTRIBUTE_MALLOC; void *xrealloc (void *p, size_t s); void *x2realloc (void *p, size_t *pn); -void *xmemdup (void const *p, size_t s); -void *xmemdup0 (void const *p, size_t s); -char *xstrdup (char const *str); +void *xmemdup (void const *p, size_t s) ATTRIBUTE_MALLOC; +char *xstrdup (char const *str) ATTRIBUTE_MALLOC; /* Return 1 if an array of N objects, each of size S, cannot exist due to size arithmetic overflow. S must be positive and N must be @@ -98,10 +106,10 @@ char *xstrdup (char const *str); # if HAVE_INLINE # define static_inline static inline # else - void *xnmalloc (size_t n, size_t s); - void *xnrealloc (void *p, size_t n, size_t s); - void *x2nrealloc (void *p, size_t *pn, size_t s); - char *xcharalloc (size_t n); +void *xnmalloc (size_t n, size_t s) ATTRIBUTE_MALLOC; +void *xnrealloc (void *p, size_t n, size_t s); +void *x2nrealloc (void *p, size_t *pn, size_t s); +char *xcharalloc (size_t n) ATTRIBUTE_MALLOC; # endif # ifdef static_inline @@ -109,6 +117,7 @@ char *xstrdup (char const *str); /* Allocate an array of N objects, each with S bytes of memory, dynamically, with error checking. S must be nonzero. */ +static_inline void *xnmalloc (size_t n, size_t s) ATTRIBUTE_MALLOC; static_inline void * xnmalloc (size_t n, size_t s) { @@ -153,9 +162,9 @@ xnrealloc (void *p, size_t n, size_t s) void append_int (int value) { - if (used == allocated) - p = x2nrealloc (p, &allocated, sizeof *p); - p[used++] = value; + if (used == allocated) + p = x2nrealloc (p, &allocated, sizeof *p); + p[used++] = value; } This causes x2nrealloc to allocate a block of some nonzero size the @@ -173,12 +182,12 @@ xnrealloc (void *p, size_t n, size_t s) void append_int (int value) { - if (used == allocated) - { - p = x2nrealloc (p, &allocated1, sizeof *p); - allocated = allocated1; - } - p[used++] = value; + if (used == allocated) + { + p = x2nrealloc (p, &allocated1, sizeof *p); + allocated = allocated1; + } + p[used++] = value; } */ @@ -191,25 +200,25 @@ x2nrealloc (void *p, size_t *pn, size_t s) if (! p) { if (! n) - { - /* The approximate size to use for initial small allocation - requests, when the invoking code specifies an old size of - zero. 64 bytes is the largest "small" request for the - GNU C library malloc. */ - enum { DEFAULT_MXFAST = 64 }; - - n = DEFAULT_MXFAST / s; - n += !n; - } + { + /* The approximate size to use for initial small allocation + requests, when the invoking code specifies an old size of + zero. 64 bytes is the largest "small" request for the + GNU C library malloc. */ + enum { DEFAULT_MXFAST = 64 }; + + n = DEFAULT_MXFAST / s; + n += !n; + } } else { /* Set N = ceil (1.5 * N) so that progress is made if N == 1. - Check for overflow, so that N * S stays in size_t range. - The check is slightly conservative, but an exact check isn't - worth the trouble. */ + Check for overflow, so that N * S stays in size_t range. + The check is slightly conservative, but an exact check isn't + worth the trouble. */ if ((size_t) -1 / 3 * 2 / s <= n) - xalloc_die (); + xalloc_die (); n += (n + 1) / 2; } @@ -220,6 +229,7 @@ x2nrealloc (void *p, size_t *pn, size_t s) /* Return a pointer to a new buffer of N bytes. This is like xmalloc, except it returns char *. */ +static_inline char *xcharalloc (size_t n) ATTRIBUTE_MALLOC; static_inline char * xcharalloc (size_t n) { @@ -265,12 +275,6 @@ xmemdup (T const *p, size_t s) return (T *) xmemdup ((void const *) p, s); } -template inline T * -xmemdup0 (T const *p, size_t s) -{ - return (T *) xmemdup0 ((void const *) p, s); -} - # endif