Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* memory allocation routines with error checking. |
c1687039 | 2 | Copyright 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc. |
252b5132 RH |
3 | |
4 | This file is part of the libiberty library. | |
5 | Libiberty is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Library General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2 of the License, or (at your option) any later version. | |
9 | ||
10 | Libiberty is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Library General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Library General Public | |
16 | License along with libiberty; see the file COPYING.LIB. If | |
17 | not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
18 | Boston, MA 02111-1307, USA. */ | |
19 | ||
39423523 DD |
20 | /* |
21 | ||
22 | @deftypefn Replacement void* xmalloc (size_t) | |
23 | ||
24 | Allocate memory without fail. If @code{malloc} fails, this will print | |
25 | a message to stderr (using the name set by @code{xmalloc_set_program_name}, | |
26 | if any) and then call @code{xexit}. Note that it is therefore safe for | |
27 | a program to contain @code{#define malloc xmalloc} in its source. | |
28 | ||
29 | @end deftypefn | |
30 | ||
31 | @deftypefn Replacement void* xrealloc (void*, size_t) | |
32 | Reallocate memory without fail. This routine functions like @code{realloc}, | |
33 | but will behave the same as @code{xmalloc} if memory cannot be found. | |
34 | ||
35 | @end deftypefn | |
36 | ||
37 | @deftypefn Replacement void* xcalloc (size_t, size_t) | |
38 | ||
39 | Allocate memory without fail, and set it to zero. This routine functions | |
40 | like @code{calloc}, but will behave the same as @code{xmalloc} if memory | |
41 | cannot be found. | |
42 | ||
43 | @end deftypefn | |
44 | ||
45 | @deftypefn Replacement void xmalloc_set_program_name (const char *@var{name}) | |
46 | ||
47 | You can use this to set the name of the program used by | |
48 | @code{xmalloc_failed} when printing a failure message. | |
49 | ||
50 | @end deftypefn | |
51 | ||
52 | @deftypefn Replacement void xmalloc_failed (size_t) | |
53 | ||
54 | This function is not meant to be called by client code, and is listed | |
55 | here for completeness only. If any of the allocation routines fail, this | |
56 | function will be called to print an error message and terminate execution. | |
57 | ||
58 | @end deftypefn | |
59 | ||
60 | */ | |
61 | ||
eb383413 L |
62 | #ifdef HAVE_CONFIG_H |
63 | #include "config.h" | |
64 | #endif | |
252b5132 RH |
65 | #include "ansidecl.h" |
66 | #include "libiberty.h" | |
67 | ||
68 | #include <stdio.h> | |
69 | ||
70 | #ifdef __STDC__ | |
71 | #include <stddef.h> | |
72 | #else | |
73 | #define size_t unsigned long | |
74 | #define ptrdiff_t long | |
75 | #endif | |
76 | ||
77 | #if VMS | |
78 | #include <stdlib.h> | |
79 | #include <unixlib.h> | |
80 | #else | |
81 | /* For systems with larger pointers than ints, these must be declared. */ | |
82 | PTR malloc PARAMS ((size_t)); | |
83 | PTR realloc PARAMS ((PTR, size_t)); | |
84 | PTR calloc PARAMS ((size_t, size_t)); | |
85 | PTR sbrk PARAMS ((ptrdiff_t)); | |
86 | #endif | |
87 | ||
88 | /* The program name if set. */ | |
89 | static const char *name = ""; | |
90 | ||
c1687039 | 91 | #ifdef HAVE_SBRK |
252b5132 RH |
92 | /* The initial sbrk, set when the program name is set. Not used for win32 |
93 | ports other than cygwin32. */ | |
94 | static char *first_break = NULL; | |
c1687039 | 95 | #endif /* HAVE_SBRK */ |
252b5132 RH |
96 | |
97 | void | |
98 | xmalloc_set_program_name (s) | |
99 | const char *s; | |
100 | { | |
101 | name = s; | |
c1687039 ILT |
102 | #ifdef HAVE_SBRK |
103 | /* Win32 ports other than cygwin32 don't have brk() */ | |
252b5132 RH |
104 | if (first_break == NULL) |
105 | first_break = (char *) sbrk (0); | |
c1687039 | 106 | #endif /* HAVE_SBRK */ |
252b5132 RH |
107 | } |
108 | ||
2ea7befd DD |
109 | void |
110 | xmalloc_failed (size) | |
111 | size_t size; | |
112 | { | |
113 | #ifdef HAVE_SBRK | |
114 | extern char **environ; | |
115 | size_t allocated; | |
116 | ||
117 | if (first_break != NULL) | |
118 | allocated = (char *) sbrk (0) - first_break; | |
119 | else | |
120 | allocated = (char *) sbrk (0) - (char *) &environ; | |
121 | fprintf (stderr, | |
122 | "\n%s%sCannot allocate %lu bytes after allocating %lu bytes\n", | |
123 | name, *name ? ": " : "", | |
124 | (unsigned long) size, (unsigned long) allocated); | |
125 | #else /* HAVE_SBRK */ | |
126 | fprintf (stderr, | |
127 | "\n%s%sCannot allocate %lu bytes\n", | |
128 | name, *name ? ": " : "", | |
129 | (unsigned long) size); | |
130 | #endif /* HAVE_SBRK */ | |
131 | xexit (1); | |
132 | } | |
133 | ||
252b5132 RH |
134 | PTR |
135 | xmalloc (size) | |
136 | size_t size; | |
137 | { | |
138 | PTR newmem; | |
139 | ||
140 | if (size == 0) | |
141 | size = 1; | |
142 | newmem = malloc (size); | |
143 | if (!newmem) | |
2ea7befd DD |
144 | xmalloc_failed (size); |
145 | ||
252b5132 RH |
146 | return (newmem); |
147 | } | |
148 | ||
149 | PTR | |
150 | xcalloc (nelem, elsize) | |
151 | size_t nelem, elsize; | |
152 | { | |
153 | PTR newmem; | |
154 | ||
155 | if (nelem == 0 || elsize == 0) | |
156 | nelem = elsize = 1; | |
157 | ||
158 | newmem = calloc (nelem, elsize); | |
159 | if (!newmem) | |
2ea7befd DD |
160 | xmalloc_failed (nelem * elsize); |
161 | ||
252b5132 RH |
162 | return (newmem); |
163 | } | |
164 | ||
165 | PTR | |
166 | xrealloc (oldmem, size) | |
167 | PTR oldmem; | |
168 | size_t size; | |
169 | { | |
170 | PTR newmem; | |
171 | ||
172 | if (size == 0) | |
173 | size = 1; | |
174 | if (!oldmem) | |
175 | newmem = malloc (size); | |
176 | else | |
177 | newmem = realloc (oldmem, size); | |
178 | if (!newmem) | |
2ea7befd DD |
179 | xmalloc_failed (size); |
180 | ||
252b5132 RH |
181 | return (newmem); |
182 | } |