Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 1 | /* xmalloc.c -- safe versions of malloc and realloc */ |
| 2 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame^] | 3 | /* Copyright (C) 1991-2009 Free Software Foundation, Inc. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 4 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame^] | 5 | This file is part of GNU Bash, the GNU Bourne Again SHell. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 6 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame^] | 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. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 11 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame^] | 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. |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 16 | |
| 17 | You should have received a copy of the GNU General Public License |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame^] | 18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 20 | |
| 21 | #if defined (HAVE_CONFIG_H) |
| 22 | #include <config.h> |
| 23 | #endif |
| 24 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 25 | #include "bashtypes.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 40 | #include "bashintl.h" |
| 41 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 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 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 50 | #if defined (HAVE_SBRK) && !HAVE_DECL_SBRK |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 51 | extern char *sbrk(); |
| 52 | #endif |
| 53 | |
| 54 | static PTR_T lbreak; |
| 55 | static int brkfound; |
| 56 | static size_t allocated; |
| 57 | |
| 58 | /* **************************************************************** */ |
| 59 | /* */ |
| 60 | /* Memory Allocation and Deallocation. */ |
| 61 | /* */ |
| 62 | /* **************************************************************** */ |
| 63 | |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 64 | #if defined (HAVE_SBRK) |
| 65 | static size_t |
| 66 | findbrk () |
| 67 | { |
| 68 | if (brkfound == 0) |
| 69 | { |
| 70 | lbreak = (PTR_T)sbrk (0); |
| 71 | brkfound++; |
| 72 | } |
| 73 | return (char *)sbrk (0) - (char *)lbreak; |
| 74 | } |
| 75 | #endif |
| 76 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 77 | /* Return a pointer to free()able block of memory large enough |
| 78 | to hold BYTES number of bytes. If the memory cannot be allocated, |
| 79 | print an error message and abort. */ |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 80 | PTR_T |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 81 | xmalloc (bytes) |
| 82 | size_t bytes; |
| 83 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 84 | PTR_T temp; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 85 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 86 | temp = malloc (bytes); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 87 | |
| 88 | if (temp == 0) |
| 89 | { |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 90 | #if defined (HAVE_SBRK) |
| 91 | allocated = findbrk (); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 92 | fatal_error (_("xmalloc: cannot allocate %lu bytes (%lu bytes allocated)"), (unsigned long)bytes, (unsigned long)allocated); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 93 | #else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 94 | fatal_error (_("xmalloc: cannot allocate %lu bytes"), (unsigned long)bytes); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 95 | #endif /* !HAVE_SBRK */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | return (temp); |
| 99 | } |
| 100 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 101 | PTR_T |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 102 | xrealloc (pointer, bytes) |
| 103 | PTR_T pointer; |
| 104 | size_t bytes; |
| 105 | { |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 106 | PTR_T temp; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 107 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 108 | temp = pointer ? realloc (pointer, bytes) : malloc (bytes); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 109 | |
| 110 | if (temp == 0) |
| 111 | { |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 112 | #if defined (HAVE_SBRK) |
| 113 | allocated = findbrk (); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 114 | fatal_error (_("xrealloc: cannot reallocate %lu bytes (%lu bytes allocated)"), (unsigned long)bytes, (unsigned long)allocated); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 115 | #else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 116 | fatal_error (_("xrealloc: cannot allocate %lu bytes"), (unsigned long)bytes); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 117 | #endif /* !HAVE_SBRK */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | return (temp); |
| 121 | } |
| 122 | |
| 123 | /* Use this as the function to call when adding unwind protects so we |
| 124 | don't need to know what free() returns. */ |
| 125 | void |
| 126 | xfree (string) |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 127 | PTR_T string; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 128 | { |
| 129 | if (string) |
| 130 | free (string); |
| 131 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 132 | |
| 133 | #ifdef USING_BASH_MALLOC |
| 134 | #include <malloc/shmalloc.h> |
| 135 | |
| 136 | PTR_T |
| 137 | sh_xmalloc (bytes, file, line) |
| 138 | size_t bytes; |
| 139 | char *file; |
| 140 | int line; |
| 141 | { |
| 142 | PTR_T temp; |
| 143 | |
| 144 | temp = sh_malloc (bytes, file, line); |
| 145 | |
| 146 | if (temp == 0) |
| 147 | { |
| 148 | #if defined (HAVE_SBRK) |
| 149 | allocated = findbrk (); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 150 | fatal_error (_("xmalloc: %s:%d: cannot allocate %lu bytes (%lu bytes allocated)"), file, line, (unsigned long)bytes, (unsigned long)allocated); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 151 | #else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 152 | fatal_error (_("xmalloc: %s:%d: cannot allocate %lu bytes"), file, line, (unsigned long)bytes); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 153 | #endif /* !HAVE_SBRK */ |
| 154 | } |
| 155 | |
| 156 | return (temp); |
| 157 | } |
| 158 | |
| 159 | PTR_T |
| 160 | sh_xrealloc (pointer, bytes, file, line) |
| 161 | PTR_T pointer; |
| 162 | size_t bytes; |
| 163 | char *file; |
| 164 | int line; |
| 165 | { |
| 166 | PTR_T temp; |
| 167 | |
| 168 | temp = pointer ? sh_realloc (pointer, bytes, file, line) : sh_malloc (bytes, file, line); |
| 169 | |
| 170 | if (temp == 0) |
| 171 | { |
| 172 | #if defined (HAVE_SBRK) |
| 173 | allocated = findbrk (); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 174 | fatal_error (_("xrealloc: %s:%d: cannot reallocate %lu bytes (%lu bytes allocated)"), file, line, (unsigned long)bytes, (unsigned long)allocated); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 175 | #else |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 176 | fatal_error (_("xrealloc: %s:%d: cannot allocate %lu bytes"), file, line, (unsigned long)bytes); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 177 | #endif /* !HAVE_SBRK */ |
| 178 | } |
| 179 | |
| 180 | return (temp); |
| 181 | } |
| 182 | |
| 183 | void |
| 184 | sh_xfree (string, file, line) |
| 185 | PTR_T string; |
| 186 | char *file; |
| 187 | int line; |
| 188 | { |
| 189 | if (string) |
| 190 | sh_free (string, file, line); |
| 191 | } |
| 192 | #endif |