]> git.ipfire.org Git - thirdparty/bash.git/blob - xmalloc.c
Bash-5.2 patch 26: fix typo when specifying readline's custom color prefix
[thirdparty/bash.git] / xmalloc.c
1 /* xmalloc.c -- safe versions of malloc and realloc */
2
3 /* Copyright (C) 1991-2016 Free Software Foundation, Inc.
4
5 This file is part of GNU Bash, the GNU Bourne Again SHell.
6
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #if defined (HAVE_CONFIG_H)
22 #include <config.h>
23 #endif
24
25 #include "bashtypes.h"
26 #include <stdio.h>
27
28 #if defined (HAVE_UNISTD_H)
29 # include <unistd.h>
30 #endif
31
32 #if defined (HAVE_STDLIB_H)
33 # include <stdlib.h>
34 #else
35 # include "ansi_stdlib.h"
36 #endif /* HAVE_STDLIB_H */
37
38 #include "error.h"
39
40 #include "bashintl.h"
41
42 #if !defined (PTR_T)
43 # if defined (__STDC__)
44 # define PTR_T void *
45 # else
46 # define PTR_T char *
47 # endif /* !__STDC__ */
48 #endif /* !PTR_T */
49
50 #if defined (HAVE_SBRK) && !HAVE_DECL_SBRK
51 extern char *sbrk();
52 #endif
53
54 #if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC)
55 static PTR_T lbreak;
56 static int brkfound;
57 static size_t allocated;
58 #endif
59
60 /* **************************************************************** */
61 /* */
62 /* Memory Allocation and Deallocation. */
63 /* */
64 /* **************************************************************** */
65
66 #if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC)
67 #define FINDBRK() \
68 do { \
69 if (brkfound == 0) \
70 { \
71 lbreak = (PTR_T)sbrk (0); \
72 brkfound++; \
73 } \
74 } while (0)
75
76 static size_t
77 findbrk ()
78 {
79 FINDBRK();
80 return (char *)sbrk (0) - (char *)lbreak;
81 }
82 #else
83 #define FINDBRK()
84 #endif
85
86 static void
87 allocerr (func, bytes)
88 const char *func;
89 size_t bytes;
90 {
91 #if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC)
92 allocated = findbrk ();
93 fatal_error (_("%s: cannot allocate %lu bytes (%lu bytes allocated)"), func, (unsigned long)bytes, (unsigned long)allocated);
94 #else
95 fatal_error (_("%s: cannot allocate %lu bytes"), func, (unsigned long)bytes);
96 #endif /* !HAVE_SBRK */
97 }
98
99 /* Return a pointer to free()able block of memory large enough
100 to hold BYTES number of bytes. If the memory cannot be allocated,
101 print an error message and abort. */
102 PTR_T
103 xmalloc (bytes)
104 size_t bytes;
105 {
106 PTR_T temp;
107
108 #if defined (DEBUG)
109 if (bytes == 0)
110 internal_warning("xmalloc: size argument is 0");
111 #endif
112
113 FINDBRK();
114 temp = malloc (bytes);
115
116 if (temp == 0)
117 allocerr ("xmalloc", bytes);
118
119 return (temp);
120 }
121
122 PTR_T
123 xrealloc (pointer, bytes)
124 PTR_T pointer;
125 size_t bytes;
126 {
127 PTR_T temp;
128
129 #if defined (DEBUG)
130 if (bytes == 0)
131 internal_warning("xrealloc: size argument is 0");
132 #endif
133
134 FINDBRK();
135 temp = pointer ? realloc (pointer, bytes) : malloc (bytes);
136
137 if (temp == 0)
138 allocerr ("xrealloc", bytes);
139
140 return (temp);
141 }
142
143 /* Use this as the function to call when adding unwind protects so we
144 don't need to know what free() returns. */
145 void
146 xfree (string)
147 PTR_T string;
148 {
149 if (string)
150 free (string);
151 }
152
153 #ifdef USING_BASH_MALLOC
154 #include <malloc/shmalloc.h>
155
156 static void
157 sh_allocerr (func, bytes, file, line)
158 const char *func;
159 size_t bytes;
160 char *file;
161 int line;
162 {
163 #if defined (HAVE_SBRK)
164 allocated = findbrk ();
165 fatal_error (_("%s: %s:%d: cannot allocate %lu bytes (%lu bytes allocated)"), func, file, line, (unsigned long)bytes, (unsigned long)allocated);
166 #else
167 fatal_error (_("%s: %s:%d: cannot allocate %lu bytes"), func, file, line, (unsigned long)bytes);
168 #endif /* !HAVE_SBRK */
169 }
170
171 PTR_T
172 sh_xmalloc (bytes, file, line)
173 size_t bytes;
174 char *file;
175 int line;
176 {
177 PTR_T temp;
178
179 #if defined (DEBUG)
180 if (bytes == 0)
181 internal_warning("xmalloc: %s:%d: size argument is 0", file, line);
182 #endif
183
184 FINDBRK();
185 temp = sh_malloc (bytes, file, line);
186
187 if (temp == 0)
188 sh_allocerr ("xmalloc", bytes, file, line);
189
190 return (temp);
191 }
192
193 PTR_T
194 sh_xrealloc (pointer, bytes, file, line)
195 PTR_T pointer;
196 size_t bytes;
197 char *file;
198 int line;
199 {
200 PTR_T temp;
201
202 #if defined (DEBUG)
203 if (bytes == 0)
204 internal_warning("xrealloc: %s:%d: size argument is 0", file, line);
205 #endif
206
207 FINDBRK();
208 temp = pointer ? sh_realloc (pointer, bytes, file, line) : sh_malloc (bytes, file, line);
209
210 if (temp == 0)
211 sh_allocerr ("xrealloc", bytes, file, line);
212
213 return (temp);
214 }
215
216 void
217 sh_xfree (string, file, line)
218 PTR_T string;
219 char *file;
220 int line;
221 {
222 if (string)
223 sh_free (string, file, line);
224 }
225 #endif