summaryrefslogtreecommitdiffstats
path: root/lib/xmalloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/xmalloc.c')
-rw-r--r--lib/xmalloc.c70
1 files changed, 28 insertions, 42 deletions
diff --git a/lib/xmalloc.c b/lib/xmalloc.c
index 181006b..687633c 100644
--- a/lib/xmalloc.c
+++ b/lib/xmalloc.c
@@ -1,7 +1,7 @@
1/* xmalloc.c -- malloc with out of memory checking 1/* xmalloc.c -- malloc with out of memory checking
2 2
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 2003, 3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
4 1999, 2000, 2002, 2003 Free Software Foundation, Inc. 4 1999, 2000, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
5 5
6 This program is free software; you can redistribute it and/or modify 6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by 7 it under the terms of the GNU General Public License as published by
@@ -15,9 +15,9 @@
15 15
16 You should have received a copy of the GNU General Public License 16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation, 17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 19
20#if HAVE_CONFIG_H 20#ifdef HAVE_CONFIG_H
21# include <config.h> 21# include <config.h>
22#endif 22#endif
23 23
@@ -26,44 +26,19 @@
26#include <stdlib.h> 26#include <stdlib.h>
27#include <string.h> 27#include <string.h>
28 28
29#include "gettext.h"
30#define _(msgid) gettext (msgid)
31#define N_(msgid) msgid
32
33#include "error.h"
34#include "exitfail.h"
35
36#ifndef SIZE_MAX 29#ifndef SIZE_MAX
37# define SIZE_MAX ((size_t) -1) 30# define SIZE_MAX ((size_t) -1)
38#endif 31#endif
39 32
40#ifndef HAVE_MALLOC 33/* 1 if calloc is known to be compatible with GNU calloc. This
41"you must run the autoconf test for a GNU libc compatible malloc" 34 matters if we are not also using the calloc module, which defines
42#endif 35 HAVE_CALLOC and supports the GNU API even on non-GNU platforms. */
43 36#if defined HAVE_CALLOC || defined __GLIBC__
44#ifndef HAVE_REALLOC 37enum { HAVE_GNU_CALLOC = 1 };
45"you must run the autoconf test for a GNU libc compatible realloc" 38#else
39enum { HAVE_GNU_CALLOC = 0 };
46#endif 40#endif
47 41
48/* If non NULL, call this function when memory is exhausted. */
49void (*xalloc_fail_func) (void) = 0;
50
51/* If XALLOC_FAIL_FUNC is NULL, or does return, display this message
52 before exiting when memory is exhausted. Goes through gettext. */
53char const xalloc_msg_memory_exhausted[] = N_("memory exhausted");
54
55void
56xalloc_die (void)
57{
58 if (xalloc_fail_func)
59 (*xalloc_fail_func) ();
60 error (exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted));
61 /* The `noreturn' cannot be given to error, since it may return if
62 its first argument is 0. To help compilers understand the
63 xalloc_die does terminate, call abort. */
64 abort ();
65}
66
67/* Allocate an array of N objects, each with S bytes of memory, 42/* Allocate an array of N objects, each with S bytes of memory,
68 dynamically, with error checking. S must be nonzero. */ 43 dynamically, with error checking. S must be nonzero. */
69 44
@@ -71,7 +46,7 @@ static inline void *
71xnmalloc_inline (size_t n, size_t s) 46xnmalloc_inline (size_t n, size_t s)
72{ 47{
73 void *p; 48 void *p;
74 if (xalloc_oversized (n, s) || ! (p = malloc (n * s))) 49 if (xalloc_oversized (n, s) || (! (p = malloc (n * s)) && n != 0))
75 xalloc_die (); 50 xalloc_die ();
76 return p; 51 return p;
77} 52}
@@ -96,7 +71,7 @@ xmalloc (size_t n)
96static inline void * 71static inline void *
97xnrealloc_inline (void *p, size_t n, size_t s) 72xnrealloc_inline (void *p, size_t n, size_t s)
98{ 73{
99 if (xalloc_oversized (n, s) || ! (p = realloc (p, n * s))) 74 if (xalloc_oversized (n, s) || (! (p = realloc (p, n * s)) && n != 0))
100 xalloc_die (); 75 xalloc_die ();
101 return p; 76 return p;
102} 77}
@@ -238,18 +213,29 @@ xcalloc (size_t n, size_t s)
238{ 213{
239 void *p; 214 void *p;
240 /* Test for overflow, since some calloc implementations don't have 215 /* Test for overflow, since some calloc implementations don't have
241 proper overflow checks. */ 216 proper overflow checks. But omit overflow and size-zero tests if
242 if (xalloc_oversized (n, s) || ! (p = calloc (n, s))) 217 HAVE_GNU_CALLOC, since GNU calloc catches overflow and never
218 returns NULL if successful. */
219 if ((! HAVE_GNU_CALLOC && xalloc_oversized (n, s))
220 || (! (p = calloc (n, s)) && (HAVE_GNU_CALLOC || n != 0)))
243 xalloc_die (); 221 xalloc_die ();
244 return p; 222 return p;
245} 223}
246 224
247/* Clone an object P of size S, with error checking. There's no need 225/* Clone an object P of size S, with error checking. There's no need
248 for xnclone (P, N, S), since xclone (P, N * S) works without any 226 for xnmemdup (P, N, S), since xmemdup (P, N * S) works without any
249 need for an arithmetic overflow check. */ 227 need for an arithmetic overflow check. */
250 228
251void * 229void *
252xclone (void const *p, size_t s) 230xmemdup (void const *p, size_t s)
253{ 231{
254 return memcpy (xmalloc (s), p, s); 232 return memcpy (xmalloc (s), p, s);
255} 233}
234
235/* Clone STRING. */
236
237char *
238xstrdup (char const *string)
239{
240 return xmemdup (string, strlen (string) + 1);
241}