1 |
/* xmalloc.c -- malloc with out of memory checking |
/* xmalloc.c -- malloc with out of memory checking |
2 |
Copyright (C) 1990, 91, 92, 93, 94, 95, 96 Free Software Foundation, Inc. |
Copyright (C) 1990-1999, 2000, 2002 Free Software Foundation, Inc. |
3 |
|
|
4 |
This program is free software; you can redistribute it and/or modify |
This program is free software; you can redistribute it and/or modify |
5 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
19 |
# include <config.h> |
# include <config.h> |
20 |
#endif |
#endif |
21 |
|
|
|
#if __STDC__ |
|
|
# define VOID void |
|
|
#else |
|
|
# define VOID char |
|
|
#endif |
|
|
|
|
22 |
#include <sys/types.h> |
#include <sys/types.h> |
23 |
|
|
24 |
#if STDC_HEADERS |
#if STDC_HEADERS |
25 |
# include <stdlib.h> |
# include <stdlib.h> |
26 |
#else |
#else |
27 |
VOID *calloc (); |
void *calloc (); |
28 |
VOID *malloc (); |
void *malloc (); |
29 |
VOID *realloc (); |
void *realloc (); |
30 |
void free (); |
void free (); |
31 |
#endif |
#endif |
32 |
|
|
33 |
#if ENABLE_NLS |
#include "gettext.h" |
34 |
# include <libintl.h> |
#define _(msgid) gettext (msgid) |
35 |
# define _(Text) gettext (Text) |
#define N_(msgid) msgid |
|
#else |
|
|
# define textdomain(Domain) |
|
|
# define _(Text) Text |
|
|
#endif |
|
36 |
|
|
37 |
#include "err.h" |
#include "error.h" |
38 |
|
#include "xalloc.h" |
39 |
|
|
40 |
#ifndef EXIT_FAILURE |
#ifndef EXIT_FAILURE |
41 |
# define EXIT_FAILURE 1 |
# define EXIT_FAILURE 1 |
42 |
#endif |
#endif |
43 |
|
|
44 |
/* Prototypes for functions defined here. */ |
/* The following tests require AC_PREREQ(2.54). */ |
45 |
#if defined (__STDC__) && __STDC__ |
|
46 |
static VOID *fixup_null_alloc (size_t n); |
#ifndef HAVE_MALLOC |
47 |
VOID *xmalloc (size_t n); |
"you must run the autoconf test for a GNU libc compatible malloc" |
|
VOID *xcalloc (size_t n, size_t s); |
|
|
VOID *xrealloc (VOID *p, size_t n); |
|
48 |
#endif |
#endif |
49 |
|
|
50 |
|
#ifndef HAVE_REALLOC |
51 |
|
"you must run the autoconf test for a GNU libc compatible realloc" |
52 |
|
#endif |
53 |
|
|
54 |
/* Exit value when the requested amount of memory is not available. |
/* Exit value when the requested amount of memory is not available. |
55 |
The caller may set it to some other value. */ |
The caller may set it to some other value. */ |
56 |
int xmalloc_exit_failure = EXIT_FAILURE; |
int xalloc_exit_failure = EXIT_FAILURE; |
57 |
|
|
58 |
static VOID * |
/* If non NULL, call this function when memory is exhausted. */ |
59 |
fixup_null_alloc (n) |
void (*xalloc_fail_func) PARAMS ((void)) = 0; |
|
size_t n; |
|
|
{ |
|
|
VOID *p; |
|
60 |
|
|
61 |
p = 0; |
/* If XALLOC_FAIL_FUNC is NULL, or does return, display this message |
62 |
if (n == 0) |
before exiting when memory is exhausted. Goes through gettext. */ |
63 |
p = malloc ((size_t) 1); |
char const xalloc_msg_memory_exhausted[] = N_("memory exhausted"); |
64 |
if (p == 0) |
|
65 |
err (xmalloc_exit_failure, _("Memory exhausted")); |
void |
66 |
return p; |
xalloc_die (void) |
67 |
|
{ |
68 |
|
if (xalloc_fail_func) |
69 |
|
(*xalloc_fail_func) (); |
70 |
|
error (xalloc_exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted)); |
71 |
|
/* The `noreturn' cannot be given to error, since it may return if |
72 |
|
its first argument is 0. To help compilers understand the |
73 |
|
xalloc_die does terminate, call exit. */ |
74 |
|
exit (EXIT_FAILURE); |
75 |
} |
} |
76 |
|
|
77 |
/* Allocate N bytes of memory dynamically, with error checking. */ |
/* Allocate N bytes of memory dynamically, with error checking. */ |
78 |
|
|
79 |
VOID * |
void * |
80 |
xmalloc (n) |
xmalloc (size_t n) |
|
size_t n; |
|
81 |
{ |
{ |
82 |
VOID *p; |
void *p; |
83 |
|
|
84 |
p = malloc (n); |
p = malloc (n); |
85 |
if (p == 0) |
if (p == 0) |
86 |
p = fixup_null_alloc (n); |
xalloc_die (); |
87 |
return p; |
return p; |
88 |
} |
} |
89 |
|
|
90 |
/* Allocate memory for N elements of S bytes, with error checking. */ |
/* Change the size of an allocated block of memory P to N bytes, |
91 |
|
with error checking. */ |
92 |
|
|
93 |
VOID * |
void * |
94 |
xcalloc (n, s) |
xrealloc (void *p, size_t n) |
|
size_t n, s; |
|
95 |
{ |
{ |
96 |
VOID *p; |
p = realloc (p, n); |
|
|
|
|
p = calloc (n, s); |
|
97 |
if (p == 0) |
if (p == 0) |
98 |
p = fixup_null_alloc (n); |
xalloc_die (); |
99 |
return p; |
return p; |
100 |
} |
} |
101 |
|
|
102 |
/* Change the size of an allocated block of memory P to N bytes, |
/* Allocate memory for N elements of S bytes, with error checking. */ |
|
with error checking. |
|
|
If P is NULL, run xmalloc. */ |
|
103 |
|
|
104 |
VOID * |
void * |
105 |
xrealloc (p, n) |
xcalloc (size_t n, size_t s) |
|
VOID *p; |
|
|
size_t n; |
|
106 |
{ |
{ |
107 |
|
void *p; |
108 |
|
|
109 |
|
p = calloc (n, s); |
110 |
if (p == 0) |
if (p == 0) |
111 |
return xmalloc (n); |
xalloc_die (); |
|
p = realloc (p, n); |
|
|
if (p == 0) |
|
|
p = fixup_null_alloc (n); |
|
112 |
return p; |
return p; |
113 |
} |
} |