]>
git.ipfire.org Git - thirdparty/bash.git/blob - xmalloc.c
1 /* xmalloc.c -- safe versions of malloc and realloc */
3 /* Copyright (C) 1991-2016 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the GNU Bourne Again SHell.
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.
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.
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/>.
21 #if defined (HAVE_CONFIG_H)
25 #include "bashtypes.h"
28 #if defined (HAVE_UNISTD_H)
32 #if defined (HAVE_STDLIB_H)
35 # include "ansi_stdlib.h"
36 #endif /* HAVE_STDLIB_H */
43 # if defined (__STDC__)
47 # endif /* !__STDC__ */
50 #if defined (HAVE_SBRK) && !HAVE_DECL_SBRK
54 #if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC)
57 static size_t allocated
;
60 /* **************************************************************** */
62 /* Memory Allocation and Deallocation. */
64 /* **************************************************************** */
66 #if defined (HAVE_SBRK) && defined (USING_BASH_MALLOC)
71 lbreak = (PTR_T)sbrk (0); \
80 return (char *)sbrk (0) - (char *)lbreak
;
87 allocerr (func
, bytes
)
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
);
95 fatal_error (_("%s: cannot allocate %lu bytes"), func
, (unsigned long)bytes
);
96 #endif /* !HAVE_SBRK */
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. */
110 internal_warning("xmalloc: size argument is 0");
114 temp
= malloc (bytes
);
117 allocerr ("xmalloc", bytes
);
123 xrealloc (pointer
, bytes
)
131 internal_warning("xrealloc: size argument is 0");
135 temp
= pointer
? realloc (pointer
, bytes
) : malloc (bytes
);
138 allocerr ("xrealloc", bytes
);
143 /* Use this as the function to call when adding unwind protects so we
144 don't need to know what free() returns. */
153 #ifdef USING_BASH_MALLOC
154 #include <malloc/shmalloc.h>
157 sh_allocerr (func
, bytes
, file
, line
)
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
);
167 fatal_error (_("%s: %s:%d: cannot allocate %lu bytes"), func
, file
, line
, (unsigned long)bytes
);
168 #endif /* !HAVE_SBRK */
172 sh_xmalloc (bytes
, file
, line
)
181 internal_warning("xmalloc: %s:%d: size argument is 0", file
, line
);
185 temp
= sh_malloc (bytes
, file
, line
);
188 sh_allocerr ("xmalloc", bytes
, file
, line
);
194 sh_xrealloc (pointer
, bytes
, file
, line
)
204 internal_warning("xrealloc: %s:%d: size argument is 0", file
, line
);
208 temp
= pointer
? sh_realloc (pointer
, bytes
, file
, line
) : sh_malloc (bytes
, file
, line
);
211 sh_allocerr ("xrealloc", bytes
, file
, line
);
217 sh_xfree (string
, file
, line
)
223 sh_free (string
, file
, line
);