]> git.ipfire.org Git - thirdparty/bash.git/blob - xmalloc.c
fix for subshells resetting job stats; new hook for rewriting part of completion...
[thirdparty/bash.git] / xmalloc.c
1 /* xmalloc.c -- safe versions of malloc and realloc */
2
3 /* Copyright (C) 1991-2016,2023 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 # define PTR_T void *
44 #endif /* !PTR_T */
45
46 #if HAVE_SBRK && !HAVE_DECL_SBRK
47 extern char *sbrk();
48 #endif
49
50 #if HAVE_SBRK && defined (USING_BASH_MALLOC)
51 static PTR_T lbreak;
52 static int brkfound;
53 static size_t allocated;
54 #endif
55
56 /* **************************************************************** */
57 /* */
58 /* Memory Allocation and Deallocation. */
59 /* */
60 /* **************************************************************** */
61
62 #if HAVE_SBRK && defined (USING_BASH_MALLOC)
63 #define FINDBRK() \
64 do { \
65 if (brkfound == 0) \
66 { \
67 lbreak = (PTR_T)sbrk (0); \
68 brkfound++; \
69 } \
70 } while (0)
71
72 static size_t
73 findbrk (void)
74 {
75 FINDBRK();
76 return (char *)sbrk (0) - (char *)lbreak;
77 }
78 #else
79 #define FINDBRK()
80 #endif
81
82 static void
83 allocerr (const char *func, size_t bytes)
84 {
85 #if HAVE_SBRK && defined (USING_BASH_MALLOC)
86 allocated = findbrk ();
87 fatal_error (_("%s: cannot allocate %lu bytes (%lu bytes allocated)"), func, (unsigned long)bytes, (unsigned long)allocated);
88 #else
89 fatal_error (_("%s: cannot allocate %lu bytes"), func, (unsigned long)bytes);
90 #endif /* !HAVE_SBRK */
91 }
92
93 /* Return a pointer to free()able block of memory large enough
94 to hold BYTES number of bytes. If the memory cannot be allocated,
95 print an error message and abort. */
96 PTR_T
97 xmalloc (size_t bytes)
98 {
99 PTR_T temp;
100
101 #if defined (DEBUG)
102 if (bytes == 0)
103 internal_warning("xmalloc: size argument is 0");
104 #endif
105
106 FINDBRK();
107 temp = malloc (bytes);
108
109 if (temp == 0)
110 allocerr ("xmalloc", bytes);
111
112 return (temp);
113 }
114
115 PTR_T
116 xrealloc (PTR_T pointer, size_t bytes)
117 {
118 PTR_T temp;
119
120 #if defined (DEBUG)
121 if (bytes == 0)
122 internal_warning("xrealloc: size argument is 0");
123 #endif
124
125 FINDBRK();
126 temp = pointer ? realloc (pointer, bytes) : malloc (bytes);
127
128 if (temp == 0)
129 allocerr ("xrealloc", bytes);
130
131 return (temp);
132 }
133
134 /* Use this as the function to call when adding unwind protects so we
135 don't need to know what free() returns. */
136 void
137 xfree (PTR_T string)
138 {
139 if (string)
140 free (string);
141 }
142
143 #ifdef USING_BASH_MALLOC
144 #include <malloc/shmalloc.h>
145
146 static void
147 sh_allocerr (const char *func, size_t bytes, char *file, int line)
148 {
149 #if HAVE_SBRK
150 allocated = findbrk ();
151 fatal_error (_("%s: %s:%d: cannot allocate %lu bytes (%lu bytes allocated)"), func, file, line, (unsigned long)bytes, (unsigned long)allocated);
152 #else
153 fatal_error (_("%s: %s:%d: cannot allocate %lu bytes"), func, file, line, (unsigned long)bytes);
154 #endif /* !HAVE_SBRK */
155 }
156
157 PTR_T
158 sh_xmalloc (size_t bytes, char *file, int line)
159 {
160 PTR_T temp;
161
162 #if defined (DEBUG)
163 if (bytes == 0)
164 internal_warning("xmalloc: %s:%d: size argument is 0", file, line);
165 #endif
166
167 FINDBRK();
168 temp = sh_malloc (bytes, file, line);
169
170 if (temp == 0)
171 sh_allocerr ("xmalloc", bytes, file, line);
172
173 return (temp);
174 }
175
176 PTR_T
177 sh_xrealloc (PTR_T pointer, size_t bytes, char *file, int line)
178 {
179 PTR_T temp;
180
181 #if defined (DEBUG)
182 if (bytes == 0)
183 internal_warning("xrealloc: %s:%d: size argument is 0", file, line);
184 #endif
185
186 FINDBRK();
187 temp = pointer ? sh_realloc (pointer, bytes, file, line) : sh_malloc (bytes, file, line);
188
189 if (temp == 0)
190 sh_allocerr ("xrealloc", bytes, file, line);
191
192 return (temp);
193 }
194
195 void
196 sh_xfree (PTR_T string, char *file, int line)
197 {
198 if (string)
199 sh_free (string, file, line);
200 }
201 #endif