diff options
Diffstat (limited to 'gl/xalloc.h')
| -rw-r--r-- | gl/xalloc.h | 133 |
1 files changed, 56 insertions, 77 deletions
diff --git a/gl/xalloc.h b/gl/xalloc.h index 6122cc58..da7c4b6b 100644 --- a/gl/xalloc.h +++ b/gl/xalloc.h | |||
| @@ -1,8 +1,6 @@ | |||
| 1 | /* xalloc.h -- malloc with out-of-memory checking | 1 | /* xalloc.h -- malloc with out-of-memory checking |
| 2 | 2 | ||
| 3 | Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, | 3 | Copyright (C) 1990-2000, 2003-2004, 2006-2013 Free Software Foundation, Inc. |
| 4 | 2000, 2003, 2004, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, | ||
| 5 | Inc. | ||
| 6 | 4 | ||
| 7 | This program is free software: you can redistribute it and/or modify | 5 | This program is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by | 6 | it under the terms of the GNU General Public License as published by |
| @@ -18,64 +16,54 @@ | |||
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | 17 | ||
| 20 | #ifndef XALLOC_H_ | 18 | #ifndef XALLOC_H_ |
| 21 | # define XALLOC_H_ | 19 | #define XALLOC_H_ |
| 22 | 20 | ||
| 23 | # include <stddef.h> | 21 | #include <stddef.h> |
| 24 | 22 | ||
| 23 | #include "xalloc-oversized.h" | ||
| 25 | 24 | ||
| 26 | # ifdef __cplusplus | 25 | _GL_INLINE_HEADER_BEGIN |
| 27 | extern "C" { | 26 | #ifndef XALLOC_INLINE |
| 28 | # endif | 27 | # define XALLOC_INLINE _GL_INLINE |
| 28 | #endif | ||
| 29 | 29 | ||
| 30 | #ifdef __cplusplus | ||
| 31 | extern "C" { | ||
| 32 | #endif | ||
| 30 | 33 | ||
| 31 | # ifndef __attribute__ | ||
| 32 | # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) | ||
| 33 | # define __attribute__(x) | ||
| 34 | # endif | ||
| 35 | # endif | ||
| 36 | 34 | ||
| 37 | # ifndef ATTRIBUTE_NORETURN | 35 | #if __GNUC__ >= 3 |
| 38 | # define ATTRIBUTE_NORETURN __attribute__ ((__noreturn__)) | 36 | # define _GL_ATTRIBUTE_MALLOC __attribute__ ((__malloc__)) |
| 39 | # endif | 37 | #else |
| 38 | # define _GL_ATTRIBUTE_MALLOC | ||
| 39 | #endif | ||
| 40 | 40 | ||
| 41 | # ifndef ATTRIBUTE_MALLOC | 41 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) |
| 42 | # if __GNUC__ >= 3 | 42 | # define _GL_ATTRIBUTE_ALLOC_SIZE(args) __attribute__ ((__alloc_size__ args)) |
| 43 | # define ATTRIBUTE_MALLOC __attribute__ ((__malloc__)) | 43 | #else |
| 44 | # else | 44 | # define _GL_ATTRIBUTE_ALLOC_SIZE(args) |
| 45 | # define ATTRIBUTE_MALLOC | 45 | #endif |
| 46 | # endif | ||
| 47 | # endif | ||
| 48 | 46 | ||
| 49 | /* This function is always triggered when memory is exhausted. | 47 | /* This function is always triggered when memory is exhausted. |
| 50 | It must be defined by the application, either explicitly | 48 | It must be defined by the application, either explicitly |
| 51 | or by using gnulib's xalloc-die module. This is the | 49 | or by using gnulib's xalloc-die module. This is the |
| 52 | function to call when one wants the program to die because of a | 50 | function to call when one wants the program to die because of a |
| 53 | memory allocation failure. */ | 51 | memory allocation failure. */ |
| 54 | extern void xalloc_die (void) ATTRIBUTE_NORETURN; | 52 | extern _Noreturn void xalloc_die (void); |
| 55 | 53 | ||
| 56 | void *xmalloc (size_t s) ATTRIBUTE_MALLOC; | 54 | void *xmalloc (size_t s) |
| 57 | void *xzalloc (size_t s) ATTRIBUTE_MALLOC; | 55 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); |
| 58 | void *xcalloc (size_t n, size_t s) ATTRIBUTE_MALLOC; | 56 | void *xzalloc (size_t s) |
| 59 | void *xrealloc (void *p, size_t s); | 57 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); |
| 58 | void *xcalloc (size_t n, size_t s) | ||
| 59 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)); | ||
| 60 | void *xrealloc (void *p, size_t s) | ||
| 61 | _GL_ATTRIBUTE_ALLOC_SIZE ((2)); | ||
| 60 | void *x2realloc (void *p, size_t *pn); | 62 | void *x2realloc (void *p, size_t *pn); |
| 61 | void *xmemdup (void const *p, size_t s) ATTRIBUTE_MALLOC; | 63 | void *xmemdup (void const *p, size_t s) |
| 62 | char *xstrdup (char const *str) ATTRIBUTE_MALLOC; | 64 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((2)); |
| 63 | 65 | char *xstrdup (char const *str) | |
| 64 | /* Return 1 if an array of N objects, each of size S, cannot exist due | 66 | _GL_ATTRIBUTE_MALLOC; |
| 65 | to size arithmetic overflow. S must be positive and N must be | ||
| 66 | nonnegative. This is a macro, not an inline function, so that it | ||
| 67 | works correctly even when SIZE_MAX < N. | ||
| 68 | |||
| 69 | By gnulib convention, SIZE_MAX represents overflow in size | ||
| 70 | calculations, so the conservative dividend to use here is | ||
| 71 | SIZE_MAX - 1, since SIZE_MAX might represent an overflowed value. | ||
| 72 | However, malloc (SIZE_MAX) fails on all known hosts where | ||
| 73 | sizeof (ptrdiff_t) <= sizeof (size_t), so do not bother to test for | ||
| 74 | exactly-SIZE_MAX allocations on such hosts; this avoids a test and | ||
| 75 | branch when S is known to be 1. */ | ||
| 76 | # define xalloc_oversized(n, s) \ | ||
| 77 | ((size_t) (sizeof (ptrdiff_t) <= sizeof (size_t) ? -1 : -2) / (s) < (n)) | ||
| 78 | |||
| 79 | 67 | ||
| 80 | /* In the following macros, T must be an elementary or structure/union or | 68 | /* In the following macros, T must be an elementary or structure/union or |
| 81 | typedef'ed type, or a pointer to such a type. To apply one of the | 69 | typedef'ed type, or a pointer to such a type. To apply one of the |
| @@ -84,41 +72,31 @@ char *xstrdup (char const *str) ATTRIBUTE_MALLOC; | |||
| 84 | 72 | ||
| 85 | /* Allocate an object of type T dynamically, with error checking. */ | 73 | /* Allocate an object of type T dynamically, with error checking. */ |
| 86 | /* extern t *XMALLOC (typename t); */ | 74 | /* extern t *XMALLOC (typename t); */ |
| 87 | # define XMALLOC(t) ((t *) xmalloc (sizeof (t))) | 75 | #define XMALLOC(t) ((t *) xmalloc (sizeof (t))) |
| 88 | 76 | ||
| 89 | /* Allocate memory for N elements of type T, with error checking. */ | 77 | /* Allocate memory for N elements of type T, with error checking. */ |
| 90 | /* extern t *XNMALLOC (size_t n, typename t); */ | 78 | /* extern t *XNMALLOC (size_t n, typename t); */ |
| 91 | # define XNMALLOC(n, t) \ | 79 | #define XNMALLOC(n, t) \ |
| 92 | ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t)))) | 80 | ((t *) (sizeof (t) == 1 ? xmalloc (n) : xnmalloc (n, sizeof (t)))) |
| 93 | 81 | ||
| 94 | /* Allocate an object of type T dynamically, with error checking, | 82 | /* Allocate an object of type T dynamically, with error checking, |
| 95 | and zero it. */ | 83 | and zero it. */ |
| 96 | /* extern t *XZALLOC (typename t); */ | 84 | /* extern t *XZALLOC (typename t); */ |
| 97 | # define XZALLOC(t) ((t *) xzalloc (sizeof (t))) | 85 | #define XZALLOC(t) ((t *) xzalloc (sizeof (t))) |
| 98 | 86 | ||
| 99 | /* Allocate memory for N elements of type T, with error checking, | 87 | /* Allocate memory for N elements of type T, with error checking, |
| 100 | and zero it. */ | 88 | and zero it. */ |
| 101 | /* extern t *XCALLOC (size_t n, typename t); */ | 89 | /* extern t *XCALLOC (size_t n, typename t); */ |
| 102 | # define XCALLOC(n, t) \ | 90 | #define XCALLOC(n, t) \ |
| 103 | ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t)))) | 91 | ((t *) (sizeof (t) == 1 ? xzalloc (n) : xcalloc (n, sizeof (t)))) |
| 104 | |||
| 105 | 92 | ||
| 106 | # if HAVE_INLINE | ||
| 107 | # define static_inline static inline | ||
| 108 | # else | ||
| 109 | void *xnmalloc (size_t n, size_t s) ATTRIBUTE_MALLOC; | ||
| 110 | void *xnrealloc (void *p, size_t n, size_t s); | ||
| 111 | void *x2nrealloc (void *p, size_t *pn, size_t s); | ||
| 112 | char *xcharalloc (size_t n) ATTRIBUTE_MALLOC; | ||
| 113 | # endif | ||
| 114 | |||
| 115 | # ifdef static_inline | ||
| 116 | 93 | ||
| 117 | /* Allocate an array of N objects, each with S bytes of memory, | 94 | /* Allocate an array of N objects, each with S bytes of memory, |
| 118 | dynamically, with error checking. S must be nonzero. */ | 95 | dynamically, with error checking. S must be nonzero. */ |
| 119 | 96 | ||
| 120 | static_inline void *xnmalloc (size_t n, size_t s) ATTRIBUTE_MALLOC; | 97 | XALLOC_INLINE void *xnmalloc (size_t n, size_t s) |
| 121 | static_inline void * | 98 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1, 2)); |
| 99 | XALLOC_INLINE void * | ||
| 122 | xnmalloc (size_t n, size_t s) | 100 | xnmalloc (size_t n, size_t s) |
| 123 | { | 101 | { |
| 124 | if (xalloc_oversized (n, s)) | 102 | if (xalloc_oversized (n, s)) |
| @@ -129,7 +107,9 @@ xnmalloc (size_t n, size_t s) | |||
| 129 | /* Change the size of an allocated block of memory P to an array of N | 107 | /* Change the size of an allocated block of memory P to an array of N |
| 130 | objects each of S bytes, with error checking. S must be nonzero. */ | 108 | objects each of S bytes, with error checking. S must be nonzero. */ |
| 131 | 109 | ||
| 132 | static_inline void * | 110 | XALLOC_INLINE void *xnrealloc (void *p, size_t n, size_t s) |
| 111 | _GL_ATTRIBUTE_ALLOC_SIZE ((2, 3)); | ||
| 112 | XALLOC_INLINE void * | ||
| 133 | xnrealloc (void *p, size_t n, size_t s) | 113 | xnrealloc (void *p, size_t n, size_t s) |
| 134 | { | 114 | { |
| 135 | if (xalloc_oversized (n, s)) | 115 | if (xalloc_oversized (n, s)) |
| @@ -192,7 +172,7 @@ xnrealloc (void *p, size_t n, size_t s) | |||
| 192 | 172 | ||
| 193 | */ | 173 | */ |
| 194 | 174 | ||
| 195 | static_inline void * | 175 | XALLOC_INLINE void * |
| 196 | x2nrealloc (void *p, size_t *pn, size_t s) | 176 | x2nrealloc (void *p, size_t *pn, size_t s) |
| 197 | { | 177 | { |
| 198 | size_t n = *pn; | 178 | size_t n = *pn; |
| @@ -203,9 +183,9 @@ x2nrealloc (void *p, size_t *pn, size_t s) | |||
| 203 | { | 183 | { |
| 204 | /* The approximate size to use for initial small allocation | 184 | /* The approximate size to use for initial small allocation |
| 205 | requests, when the invoking code specifies an old size of | 185 | requests, when the invoking code specifies an old size of |
| 206 | zero. 64 bytes is the largest "small" request for the | 186 | zero. This is the largest "small" request for the GNU C |
| 207 | GNU C library malloc. */ | 187 | library malloc. */ |
| 208 | enum { DEFAULT_MXFAST = 64 }; | 188 | enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 }; |
| 209 | 189 | ||
| 210 | n = DEFAULT_MXFAST / s; | 190 | n = DEFAULT_MXFAST / s; |
| 211 | n += !n; | 191 | n += !n; |
| @@ -229,16 +209,15 @@ x2nrealloc (void *p, size_t *pn, size_t s) | |||
| 229 | /* Return a pointer to a new buffer of N bytes. This is like xmalloc, | 209 | /* Return a pointer to a new buffer of N bytes. This is like xmalloc, |
| 230 | except it returns char *. */ | 210 | except it returns char *. */ |
| 231 | 211 | ||
| 232 | static_inline char *xcharalloc (size_t n) ATTRIBUTE_MALLOC; | 212 | XALLOC_INLINE char *xcharalloc (size_t n) |
| 233 | static_inline char * | 213 | _GL_ATTRIBUTE_MALLOC _GL_ATTRIBUTE_ALLOC_SIZE ((1)); |
| 214 | XALLOC_INLINE char * | ||
| 234 | xcharalloc (size_t n) | 215 | xcharalloc (size_t n) |
| 235 | { | 216 | { |
| 236 | return XNMALLOC (n, char); | 217 | return XNMALLOC (n, char); |
| 237 | } | 218 | } |
| 238 | 219 | ||
| 239 | # endif | 220 | #ifdef __cplusplus |
| 240 | |||
| 241 | # ifdef __cplusplus | ||
| 242 | } | 221 | } |
| 243 | 222 | ||
| 244 | /* C++ does not allow conversions from void * to other pointer types | 223 | /* C++ does not allow conversions from void * to other pointer types |
| @@ -275,7 +254,7 @@ xmemdup (T const *p, size_t s) | |||
| 275 | return (T *) xmemdup ((void const *) p, s); | 254 | return (T *) xmemdup ((void const *) p, s); |
| 276 | } | 255 | } |
| 277 | 256 | ||
| 278 | # endif | 257 | #endif |
| 279 | 258 | ||
| 280 | 259 | ||
| 281 | #endif /* !XALLOC_H_ */ | 260 | #endif /* !XALLOC_H_ */ |
