1
/*********************************************************
2
* Copyright (C) 2009 VMware, Inc. All rights reserved.
4
* This program is free software; you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License as published
6
* by the Free Software Foundation version 2.1 and no later version.
8
* This program is distributed in the hope that it will be useful, but
9
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10
* or FITNESS FOR A PARTICULAR PURPOSE. See the Lesser GNU General Public
11
* License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program; if not, write to the Free Software Foundation, Inc.,
15
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
17
*********************************************************/
29
#include "vm_assert.h"
32
*-----------------------------------------------------------------------------
34
* Util_SafeInternalMalloc --
35
* Helper function for malloc
38
* Pointer to the dynamically allocated memory.
41
* May Panic if ran out of memory.
43
*-----------------------------------------------------------------------------
47
Util_SafeInternalMalloc(int bugNumber, // IN:
49
const char *file, // IN:
52
void *result = malloc(size);
54
if (result == NULL && size != 0) {
55
if (bugNumber == -1) {
56
Panic("Unrecoverable memory allocation failure at %s:%d\n",
59
Panic("Unrecoverable memory allocation failure at %s:%d. Bug "
60
"number: %d\n", file, lineno, bugNumber);
68
*-----------------------------------------------------------------------------
70
* Util_SafeInternalRealloc --
71
* Helper function for realloc
74
* Pointer to the dynamically allocated memory.
77
* May Panic if ran out of memory.
79
*-----------------------------------------------------------------------------
83
Util_SafeInternalRealloc(int bugNumber, // IN:
86
const char *file, // IN:
89
void *result = realloc(ptr, size);
91
if (result == NULL && size != 0) {
92
if (bugNumber == -1) {
93
Panic("Unrecoverable memory allocation failure at %s:%d\n",
96
Panic("Unrecoverable memory allocation failure at %s:%d. Bug "
97
"number: %d\n", file, lineno, bugNumber);
105
*-----------------------------------------------------------------------------
107
* Util_SafeInternalCalloc --
108
* Helper function for calloc
111
* Pointer to the dynamically allocated memory.
114
* May Panic if ran out of memory.
116
*-----------------------------------------------------------------------------
120
Util_SafeInternalCalloc(int bugNumber, // IN:
123
const char *file, // IN:
126
void *result = calloc(nmemb, size);
128
if (result == NULL && nmemb != 0 && size != 0) {
129
if (bugNumber == -1) {
130
Panic("Unrecoverable memory allocation failure at %s:%d\n",
133
Panic("Unrecoverable memory allocation failure at %s:%d. Bug "
134
"number: %d\n", file, lineno, bugNumber);
142
*-----------------------------------------------------------------------------
144
* Util_SafeInternalStrdup --
145
* Helper function for strdup
148
* Pointer to the dynamically allocated, duplicate string
151
* May Panic if ran out of memory.
153
*-----------------------------------------------------------------------------
157
Util_SafeInternalStrdup(int bugNumber, // IN:
158
const char *s, // IN:
159
const char *file, // IN:
169
if ((result = _strdup(s)) == NULL) {
171
if ((result = strdup(s)) == NULL) {
173
if (bugNumber == -1) {
174
Panic("Unrecoverable memory allocation failure at %s:%d\n",
177
Panic("Unrecoverable memory allocation failure at %s:%d. Bug "
178
"number: %d\n", file, lineno, bugNumber);
187
*-----------------------------------------------------------------------------
189
* UtilSafeStrndupInternal --
191
* Returns a string consisting of first n characters of 's' if 's' has
192
* length >= 'n', otherwise returns a string duplicate of 's'.
195
* Pointer to the duplicated string.
198
* May Panic if ran out of memory.
200
*-----------------------------------------------------------------------------
204
Util_SafeInternalStrndup(int bugNumber, // IN:
205
const char *s, // IN:
207
const char *file, // IN:
218
null = (char *) memchr(s, '\0', n);
219
size = null ? null - s: n;
220
copy = (char *) malloc(size + 1);
223
if (bugNumber == -1) {
224
Panic("Unrecoverable memory allocation failure at %s:%d\n",
227
Panic("Unrecoverable memory allocation failure at %s:%d. Bug "
228
"number: %d\n", file, lineno, bugNumber);
234
return (char *) memcpy(copy, s, size);