Mercurial > hg > octave-kai > gnulib-hg
annotate lib/xmalloc.c @ 4739:04758f7475fd
Merge changes from glibc.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Fri, 26 Sep 2003 07:35:01 +0000 |
parents | 4753d416bf20 |
children | d1dc5d9bf1ba |
rev | line source |
---|---|
9 | 1 /* xmalloc.c -- malloc with out of memory checking |
4397
c6450308f123
Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4000
diff
changeset
|
2 |
c6450308f123
Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4000
diff
changeset
|
3 Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, |
c6450308f123
Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4000
diff
changeset
|
4 1999, 2000, 2002, 2003 Free Software Foundation, Inc. |
9 | 5 |
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 | |
8 the Free Software Foundation; either version 2, or (at your option) | |
9 any later version. | |
10 | |
11 This program is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 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
|
17 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
|
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
9 | 19 |
739 | 20 #if HAVE_CONFIG_H |
653 | 21 # include <config.h> |
399 | 22 #endif |
23 | |
4691 | 24 #include "xalloc.h" |
399 | 25 |
4691 | 26 #include <stdlib.h> |
9 | 27 |
3966
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3130
diff
changeset
|
28 #include "gettext.h" |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3130
diff
changeset
|
29 #define _(msgid) gettext (msgid) |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3130
diff
changeset
|
30 #define N_(msgid) msgid |
399 | 31 |
739 | 32 #include "error.h" |
4696
4753d416bf20
Use "exit.h" rather than rolling EXIT_FAILURE ourselves in each module.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4691
diff
changeset
|
33 #include "exit.h" |
4473
fd6696a97000
Sync with coreutils xalloc.h, xmalloc.c, xmemcoll.h, xmemcoll.c.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4397
diff
changeset
|
34 #include "exitfail.h" |
739 | 35 |
4000 | 36 /* The following tests require AC_PREREQ(2.54). */ |
37 | |
3985
f01511388a8f
Adjust to work with new autoconf macros, AC_FUNC_MALLOC
Jim Meyering <jim@meyering.net>
parents:
3966
diff
changeset
|
38 #ifndef HAVE_MALLOC |
4000 | 39 "you must run the autoconf test for a GNU libc compatible malloc" |
1090
94c957144330
make sure autoconf tests have been run
Jim Meyering <jim@meyering.net>
parents:
1088
diff
changeset
|
40 #endif |
94c957144330
make sure autoconf tests have been run
Jim Meyering <jim@meyering.net>
parents:
1088
diff
changeset
|
41 |
3985
f01511388a8f
Adjust to work with new autoconf macros, AC_FUNC_MALLOC
Jim Meyering <jim@meyering.net>
parents:
3966
diff
changeset
|
42 #ifndef HAVE_REALLOC |
4000 | 43 "you must run the autoconf test for a GNU libc compatible realloc" |
1090
94c957144330
make sure autoconf tests have been run
Jim Meyering <jim@meyering.net>
parents:
1088
diff
changeset
|
44 #endif |
739 | 45 |
1604 | 46 /* If non NULL, call this function when memory is exhausted. */ |
4397
c6450308f123
Assume C89, so PARAMS isn't needed.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4000
diff
changeset
|
47 void (*xalloc_fail_func) (void) = 0; |
399 | 48 |
1604 | 49 /* If XALLOC_FAIL_FUNC is NULL, or does return, display this message |
50 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
|
51 char const xalloc_msg_memory_exhausted[] = N_("memory exhausted"); |
399 | 52 |
1956
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
53 void |
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
54 xalloc_die (void) |
399 | 55 { |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
56 if (xalloc_fail_func) |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
57 (*xalloc_fail_func) (); |
4473
fd6696a97000
Sync with coreutils xalloc.h, xmalloc.c, xmemcoll.h, xmemcoll.c.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4397
diff
changeset
|
58 error (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
|
59 /* 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
|
60 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
|
61 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
|
62 exit (EXIT_FAILURE); |
399 | 63 } |
9 | 64 |
65 /* Allocate N bytes of memory dynamically, with error checking. */ | |
66 | |
1071 | 67 void * |
1557 | 68 xmalloc (size_t n) |
9 | 69 { |
1071 | 70 void *p; |
9 | 71 |
72 p = malloc (n); | |
73 if (p == 0) | |
1956
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
74 xalloc_die (); |
9 | 75 return p; |
76 } | |
77 | |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
78 /* 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
|
79 with error checking. */ |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
80 |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
81 void * |
1557 | 82 xrealloc (void *p, size_t n) |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
83 { |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
84 p = realloc (p, n); |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
85 if (p == 0) |
1956
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
86 xalloc_die (); |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
87 return p; |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
88 } |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
89 |
739 | 90 /* Allocate memory for N elements of S bytes, with error checking. */ |
91 | |
1071 | 92 void * |
1557 | 93 xcalloc (size_t n, size_t s) |
739 | 94 { |
1071 | 95 void *p; |
739 | 96 |
97 p = calloc (n, s); | |
98 if (p == 0) | |
1956
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
99 xalloc_die (); |
739 | 100 return p; |
101 } |