Mercurial > hg > octave-shane > gnulib-hg
annotate lib/xmalloc.c @ 3130:2cfeb7383c2e
[HAVE_DONE_WORKING_MALLOC_CHECK]: Enclose error-evoking
line in double quotes so that it evokes a better diagnostic.
[HAVE_DONE_WORKING_REALLOC_CHECK]: Likewise.
author | Jim Meyering <jim@meyering.net> |
---|---|
date | Fri, 26 Jan 2001 11:13:28 +0000 |
parents | bae17cdae026 |
children | 22d3032f0239 |
rev | line source |
---|---|
9 | 1 /* xmalloc.c -- malloc with out of memory checking |
2814
bae17cdae026
(xalloc_msg_memory_exhausted): Now char const[],
Jim Meyering <jim@meyering.net>
parents:
2793
diff
changeset
|
2 Copyright (C) 1990-1999, 2000 Free Software Foundation, Inc. |
9 | 3 |
4 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 | |
6 the Free Software Foundation; either version 2, or (at your option) | |
7 any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, | |
10 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 GNU General Public License for more details. | |
13 | |
14 You should have received a copy of the GNU General Public License | |
649
89f4c1937ac7
update FSF address in copyright and remove any trailing blanks
Jim Meyering <jim@meyering.net>
parents:
399
diff
changeset
|
15 along with this program; if not, write to the Free Software Foundation, |
89f4c1937ac7
update FSF address in copyright and remove any trailing blanks
Jim Meyering <jim@meyering.net>
parents:
399
diff
changeset
|
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
9 | 17 |
739 | 18 #if HAVE_CONFIG_H |
653 | 19 # include <config.h> |
399 | 20 #endif |
21 | |
22 #include <sys/types.h> | |
23 | |
24 #if STDC_HEADERS | |
653 | 25 # include <stdlib.h> |
9 | 26 #else |
1071 | 27 void *calloc (); |
28 void *malloc (); | |
29 void *realloc (); | |
9 | 30 void free (); |
31 #endif | |
32 | |
739 | 33 #if ENABLE_NLS |
399 | 34 # include <libintl.h> |
739 | 35 # define _(Text) gettext (Text) |
399 | 36 #else |
739 | 37 # define textdomain(Domain) |
38 # define _(Text) Text | |
399 | 39 #endif |
1071 | 40 #define N_(Text) Text |
399 | 41 |
739 | 42 #include "error.h" |
1071 | 43 #include "xalloc.h" |
739 | 44 |
399 | 45 #ifndef EXIT_FAILURE |
653 | 46 # define EXIT_FAILURE 1 |
399 | 47 #endif |
48 | |
1090
94c957144330
make sure autoconf tests have been run
Jim Meyering <jim@meyering.net>
parents:
1088
diff
changeset
|
49 #ifndef HAVE_DONE_WORKING_MALLOC_CHECK |
3130
2cfeb7383c2e
[HAVE_DONE_WORKING_MALLOC_CHECK]: Enclose error-evoking
Jim Meyering <jim@meyering.net>
parents:
2814
diff
changeset
|
50 "you must run the autoconf test for a properly working malloc -- see malloc.m4" |
1090
94c957144330
make sure autoconf tests have been run
Jim Meyering <jim@meyering.net>
parents:
1088
diff
changeset
|
51 #endif |
94c957144330
make sure autoconf tests have been run
Jim Meyering <jim@meyering.net>
parents:
1088
diff
changeset
|
52 |
94c957144330
make sure autoconf tests have been run
Jim Meyering <jim@meyering.net>
parents:
1088
diff
changeset
|
53 #ifndef HAVE_DONE_WORKING_REALLOC_CHECK |
3130
2cfeb7383c2e
[HAVE_DONE_WORKING_MALLOC_CHECK]: Enclose error-evoking
Jim Meyering <jim@meyering.net>
parents:
2814
diff
changeset
|
54 "you must run the autoconf test for a properly working realloc --see realloc.m4" |
1090
94c957144330
make sure autoconf tests have been run
Jim Meyering <jim@meyering.net>
parents:
1088
diff
changeset
|
55 #endif |
739 | 56 |
399 | 57 /* Exit value when the requested amount of memory is not available. |
58 The caller may set it to some other value. */ | |
1071 | 59 int xalloc_exit_failure = EXIT_FAILURE; |
60 | |
1604 | 61 /* If non NULL, call this function when memory is exhausted. */ |
1940
ee7e51d31515
(xalloc_fail_func): Use `PARAMS'.
Jim Meyering <jim@meyering.net>
parents:
1784
diff
changeset
|
62 void (*xalloc_fail_func) PARAMS ((void)) = 0; |
399 | 63 |
1604 | 64 /* If XALLOC_FAIL_FUNC is NULL, or does return, display this message |
65 before exiting when memory is exhausted. Goes through gettext. */ | |
2814
bae17cdae026
(xalloc_msg_memory_exhausted): Now char const[],
Jim Meyering <jim@meyering.net>
parents:
2793
diff
changeset
|
66 char const xalloc_msg_memory_exhausted[] = N_("memory exhausted"); |
399 | 67 |
1956
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
68 void |
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
69 xalloc_die (void) |
399 | 70 { |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
71 if (xalloc_fail_func) |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
72 (*xalloc_fail_func) (); |
1784
5afbb5279fd5
(xalloc_fail): Pass xalloc_msg_memory_exhausted through gettext.
Jim Meyering <jim@meyering.net>
parents:
1728
diff
changeset
|
73 error (xalloc_exit_failure, 0, "%s", _(xalloc_msg_memory_exhausted)); |
1956
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
74 /* The `noreturn' cannot be given to error, since it may return if |
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
75 its first argument is 0. To help compilers understand the |
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
76 xalloc_die does terminate, call exit. */ |
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
77 exit (EXIT_FAILURE); |
399 | 78 } |
9 | 79 |
80 /* Allocate N bytes of memory dynamically, with error checking. */ | |
81 | |
1071 | 82 void * |
1557 | 83 xmalloc (size_t n) |
9 | 84 { |
1071 | 85 void *p; |
9 | 86 |
87 p = malloc (n); | |
88 if (p == 0) | |
1956
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
89 xalloc_die (); |
9 | 90 return p; |
91 } | |
92 | |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
93 /* Change the size of an allocated block of memory P to N bytes, |
2814
bae17cdae026
(xalloc_msg_memory_exhausted): Now char const[],
Jim Meyering <jim@meyering.net>
parents:
2793
diff
changeset
|
94 with error checking. */ |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
95 |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
96 void * |
1557 | 97 xrealloc (void *p, size_t n) |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
98 { |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
99 p = realloc (p, n); |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
100 if (p == 0) |
1956
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
101 xalloc_die (); |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
102 return p; |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
103 } |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
104 |
739 | 105 /* Allocate memory for N elements of S bytes, with error checking. */ |
106 | |
1071 | 107 void * |
1557 | 108 xcalloc (size_t n, size_t s) |
739 | 109 { |
1071 | 110 void *p; |
739 | 111 |
112 p = calloc (n, s); | |
113 if (p == 0) | |
1956
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
114 xalloc_die (); |
739 | 115 return p; |
116 } |