Mercurial > hg > octave-shane > gnulib-hg
annotate lib/xmalloc.c @ 4554:5a1e7c84e9cd
Use three spaces, rather than tab, after '#'
in shell-script copyright notices.
Suggested by Bruno Haible.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Fri, 15 Aug 2003 18:04:34 +0000 (2003-08-15) |
parents | fd6696a97000 |
children | ce37d22a271f |
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 | |
24 #include <sys/types.h> | |
25 | |
26 #if STDC_HEADERS | |
653 | 27 # include <stdlib.h> |
9 | 28 #else |
1071 | 29 void *calloc (); |
30 void *malloc (); | |
31 void *realloc (); | |
9 | 32 void free (); |
33 #endif | |
34 | |
3966
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3130
diff
changeset
|
35 #include "gettext.h" |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3130
diff
changeset
|
36 #define _(msgid) gettext (msgid) |
22d3032f0239
Include gettext.h instead of <libintl.h> with #ifdefs.
Bruno Haible <bruno@clisp.org>
parents:
3130
diff
changeset
|
37 #define N_(msgid) msgid |
399 | 38 |
739 | 39 #include "error.h" |
4473
fd6696a97000
Sync with coreutils xalloc.h, xmalloc.c, xmemcoll.h, xmemcoll.c.
Paul Eggert <eggert@cs.ucla.edu>
parents:
4397
diff
changeset
|
40 #include "exitfail.h" |
1071 | 41 #include "xalloc.h" |
739 | 42 |
399 | 43 #ifndef EXIT_FAILURE |
653 | 44 # define EXIT_FAILURE 1 |
399 | 45 #endif |
46 | |
4000 | 47 /* The following tests require AC_PREREQ(2.54). */ |
48 | |
3985
f01511388a8f
Adjust to work with new autoconf macros, AC_FUNC_MALLOC
Jim Meyering <jim@meyering.net>
parents:
3966
diff
changeset
|
49 #ifndef HAVE_MALLOC |
4000 | 50 "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
|
51 #endif |
94c957144330
make sure autoconf tests have been run
Jim Meyering <jim@meyering.net>
parents:
1088
diff
changeset
|
52 |
3985
f01511388a8f
Adjust to work with new autoconf macros, AC_FUNC_MALLOC
Jim Meyering <jim@meyering.net>
parents:
3966
diff
changeset
|
53 #ifndef HAVE_REALLOC |
4000 | 54 "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
|
55 #endif |
739 | 56 |
1604 | 57 /* 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
|
58 void (*xalloc_fail_func) (void) = 0; |
399 | 59 |
1604 | 60 /* If XALLOC_FAIL_FUNC is NULL, or does return, display this message |
61 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
|
62 char const xalloc_msg_memory_exhausted[] = N_("memory exhausted"); |
399 | 63 |
1956
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
64 void |
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
65 xalloc_die (void) |
399 | 66 { |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
67 if (xalloc_fail_func) |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
68 (*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
|
69 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
|
70 /* 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
|
71 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
|
72 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
|
73 exit (EXIT_FAILURE); |
399 | 74 } |
9 | 75 |
76 /* Allocate N bytes of memory dynamically, with error checking. */ | |
77 | |
1071 | 78 void * |
1557 | 79 xmalloc (size_t n) |
9 | 80 { |
1071 | 81 void *p; |
9 | 82 |
83 p = malloc (n); | |
84 if (p == 0) | |
1956
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
85 xalloc_die (); |
9 | 86 return p; |
87 } | |
88 | |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
89 /* 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
|
90 with error checking. */ |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
91 |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
92 void * |
1557 | 93 xrealloc (void *p, size_t n) |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
94 { |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
95 p = realloc (p, n); |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
96 if (p == 0) |
1956
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
97 xalloc_die (); |
1088
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
98 return p; |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
99 } |
1ae95221563d
(xalloc_fail): Renamed from fixup_null_alloc.
Jim Meyering <jim@meyering.net>
parents:
1080
diff
changeset
|
100 |
739 | 101 /* Allocate memory for N elements of S bytes, with error checking. */ |
102 | |
1071 | 103 void * |
1557 | 104 xcalloc (size_t n, size_t s) |
739 | 105 { |
1071 | 106 void *p; |
739 | 107 |
108 p = calloc (n, s); | |
109 if (p == 0) | |
1956
b04912653d02
(xalloc_die): Rename from xalloc_fail and update callers.
Jim Meyering <jim@meyering.net>
parents:
1940
diff
changeset
|
110 xalloc_die (); |
739 | 111 return p; |
112 } |