]> git.ipfire.org Git - thirdparty/bash.git/blame - xmalloc.c
bash-5.0 distribution sources and documentation
[thirdparty/bash.git] / xmalloc.c
CommitLineData
ccc6cda3
JA
1/* xmalloc.c -- safe versions of malloc and realloc */
2
a0c0a00f 3/* Copyright (C) 1991-2016 Free Software Foundation, Inc.
ccc6cda3 4
3185942a 5 This file is part of GNU Bash, the GNU Bourne Again SHell.
ccc6cda3 6
3185942a
JA
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.
ccc6cda3 11
3185942a
JA
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.
ccc6cda3
JA
16
17 You should have received a copy of the GNU General Public License
3185942a
JA
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
19*/
ccc6cda3
JA
20
21#if defined (HAVE_CONFIG_H)
22#include <config.h>
23#endif
24
d166f048 25#include "bashtypes.h"
ccc6cda3
JA
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
b80f6443
JA
40#include "bashintl.h"
41
ccc6cda3
JA
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
f73dda09 50#if defined (HAVE_SBRK) && !HAVE_DECL_SBRK
ccc6cda3
JA
51extern char *sbrk();
52#endif
53
d233b485 54#if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC)
ccc6cda3
JA
55static PTR_T lbreak;
56static int brkfound;
57static size_t allocated;
d233b485 58#endif
ccc6cda3
JA
59
60/* **************************************************************** */
61/* */
62/* Memory Allocation and Deallocation. */
63/* */
64/* **************************************************************** */
65
a0c0a00f 66#if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC)
0001803f
CR
67#define FINDBRK() \
68do { \
69 if (brkfound == 0) \
70 { \
71 lbreak = (PTR_T)sbrk (0); \
72 brkfound++; \
73 } \
74} while (0)
75
b72432fd
JA
76static size_t
77findbrk ()
78{
0001803f 79 FINDBRK();
b72432fd
JA
80 return (char *)sbrk (0) - (char *)lbreak;
81}
0001803f
CR
82#else
83#define FINDBRK()
b72432fd
JA
84#endif
85
0001803f
CR
86static void
87allocerr (func, bytes)
88 const char *func;
89 size_t bytes;
90{
a0c0a00f 91#if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC)
0001803f
CR
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
ccc6cda3
JA
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. */
f73dda09 102PTR_T
ccc6cda3
JA
103xmalloc (bytes)
104 size_t bytes;
105{
f73dda09 106 PTR_T temp;
ccc6cda3 107
0001803f
CR
108#if defined (DEBUG)
109 if (bytes == 0)
110 internal_warning("xmalloc: size argument is 0");
111#endif
112
113 FINDBRK();
f73dda09 114 temp = malloc (bytes);
ccc6cda3
JA
115
116 if (temp == 0)
0001803f 117 allocerr ("xmalloc", bytes);
ccc6cda3
JA
118
119 return (temp);
120}
121
f73dda09 122PTR_T
ccc6cda3
JA
123xrealloc (pointer, bytes)
124 PTR_T pointer;
125 size_t bytes;
126{
f73dda09 127 PTR_T temp;
ccc6cda3 128
0001803f
CR
129#if defined (DEBUG)
130 if (bytes == 0)
131 internal_warning("xrealloc: size argument is 0");
132#endif
133
134 FINDBRK();
f73dda09 135 temp = pointer ? realloc (pointer, bytes) : malloc (bytes);
ccc6cda3
JA
136
137 if (temp == 0)
0001803f 138 allocerr ("xrealloc", bytes);
ccc6cda3
JA
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. */
145void
146xfree (string)
bb70624e 147 PTR_T string;
ccc6cda3
JA
148{
149 if (string)
150 free (string);
151}
f73dda09
JA
152
153#ifdef USING_BASH_MALLOC
154#include <malloc/shmalloc.h>
155
0001803f
CR
156static void
157sh_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
f73dda09
JA
171PTR_T
172sh_xmalloc (bytes, file, line)
173 size_t bytes;
174 char *file;
175 int line;
176{
177 PTR_T temp;
178
0001803f
CR
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();
f73dda09
JA
185 temp = sh_malloc (bytes, file, line);
186
187 if (temp == 0)
0001803f 188 sh_allocerr ("xmalloc", bytes, file, line);
f73dda09
JA
189
190 return (temp);
191}
192
193PTR_T
194sh_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
0001803f
CR
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();
f73dda09
JA
208 temp = pointer ? sh_realloc (pointer, bytes, file, line) : sh_malloc (bytes, file, line);
209
210 if (temp == 0)
0001803f 211 sh_allocerr ("xrealloc", bytes, file, line);
f73dda09
JA
212
213 return (temp);
214}
215
216void
217sh_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