~n-muench/ubuntu/oneiric/open-vm-tools/open-vm-tools.fix-836277

« back to all changes in this revision

Viewing changes to lib/misc/utilMem.c

  • Committer: Evan Broder
  • Date: 2010-03-21 23:26:53 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: broder@mit.edu-20100321232653-5a57r7v7ch4o6byv
Merging shared upstream rev into target branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*********************************************************
 
2
 * Copyright (C) 2009 VMware, Inc. All rights reserved.
 
3
 *
 
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.
 
7
 *
 
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.
 
12
 *
 
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.
 
16
 *
 
17
 *********************************************************/
 
18
 
 
19
/*
 
20
 * utilMem.c --
 
21
 *
 
22
 *    misc util functions
 
23
 */
 
24
 
 
25
#include <stdarg.h>
 
26
#include <string.h>
 
27
#include <stdlib.h>
 
28
 
 
29
#include "vm_assert.h"
 
30
 
 
31
/*
 
32
 *-----------------------------------------------------------------------------
 
33
 *
 
34
 * Util_SafeInternalMalloc --
 
35
 *      Helper function for malloc
 
36
 *
 
37
 * Results:
 
38
 *      Pointer to the dynamically allocated memory.
 
39
 *
 
40
 * Side effects:
 
41
 *      May Panic if ran out of memory.
 
42
 *
 
43
 *-----------------------------------------------------------------------------
 
44
 */
 
45
 
 
46
void *
 
47
Util_SafeInternalMalloc(int bugNumber,        // IN:
 
48
                        size_t size,          // IN:
 
49
                        const char *file,     // IN:
 
50
                        int lineno)           // IN:
 
51
{
 
52
   void *result = malloc(size);
 
53
 
 
54
   if (result == NULL && size != 0) {
 
55
      if (bugNumber == -1) {
 
56
         Panic("Unrecoverable memory allocation failure at %s:%d\n",
 
57
               file, lineno);
 
58
      } else {
 
59
         Panic("Unrecoverable memory allocation failure at %s:%d.  Bug "
 
60
               "number: %d\n", file, lineno, bugNumber);
 
61
      }
 
62
   }
 
63
   return result;
 
64
}
 
65
 
 
66
 
 
67
/*
 
68
 *-----------------------------------------------------------------------------
 
69
 *
 
70
 * Util_SafeInternalRealloc --
 
71
 *      Helper function for realloc
 
72
 *
 
73
 * Results:
 
74
 *      Pointer to the dynamically allocated memory.
 
75
 *
 
76
 * Side effects:
 
77
 *      May Panic if ran out of memory.
 
78
 *
 
79
 *-----------------------------------------------------------------------------
 
80
 */
 
81
 
 
82
void *
 
83
Util_SafeInternalRealloc(int bugNumber,        // IN:
 
84
                         void *ptr,            // IN:
 
85
                         size_t size,          // IN:
 
86
                         const char *file,     // IN:
 
87
                         int lineno)           // IN:
 
88
{
 
89
   void *result = realloc(ptr, size);
 
90
 
 
91
   if (result == NULL && size != 0) {
 
92
      if (bugNumber == -1) {
 
93
         Panic("Unrecoverable memory allocation failure at %s:%d\n",
 
94
               file, lineno);
 
95
      } else {
 
96
         Panic("Unrecoverable memory allocation failure at %s:%d.  Bug "
 
97
               "number: %d\n", file, lineno, bugNumber);
 
98
      }
 
99
   }
 
100
   return result;
 
101
}
 
102
 
 
103
 
 
104
/*
 
105
 *-----------------------------------------------------------------------------
 
106
 *
 
107
 * Util_SafeInternalCalloc --
 
108
 *      Helper function for calloc
 
109
 *
 
110
 * Results:
 
111
 *      Pointer to the dynamically allocated memory.
 
112
 *
 
113
 * Side effects:
 
114
 *      May Panic if ran out of memory.
 
115
 *
 
116
 *-----------------------------------------------------------------------------
 
117
 */
 
118
 
 
119
void *
 
120
Util_SafeInternalCalloc(int bugNumber,        // IN:
 
121
                        size_t nmemb,         // IN:
 
122
                        size_t size,          // IN:
 
123
                        const char *file,     // IN:
 
124
                        int lineno)           // IN:
 
125
{
 
126
   void *result = calloc(nmemb, size);
 
127
 
 
128
   if (result == NULL && nmemb != 0 && size != 0) {
 
129
      if (bugNumber == -1) {
 
130
         Panic("Unrecoverable memory allocation failure at %s:%d\n",
 
131
               file, lineno);
 
132
      } else {
 
133
         Panic("Unrecoverable memory allocation failure at %s:%d.  Bug "
 
134
               "number: %d\n", file, lineno, bugNumber);
 
135
      }
 
136
   }
 
137
   return result;
 
138
}
 
139
 
 
140
 
 
141
/*
 
142
 *-----------------------------------------------------------------------------
 
143
 *
 
144
 * Util_SafeInternalStrdup --
 
145
 *      Helper function for strdup
 
146
 *
 
147
 * Results:
 
148
 *      Pointer to the dynamically allocated, duplicate string
 
149
 *
 
150
 * Side effects:
 
151
 *      May Panic if ran out of memory.
 
152
 *
 
153
 *-----------------------------------------------------------------------------
 
154
 */
 
155
 
 
156
char *
 
157
Util_SafeInternalStrdup(int bugNumber,        // IN:
 
158
                        const char *s,        // IN:
 
159
                        const char *file,     // IN:
 
160
                        int lineno)           // IN:
 
161
{
 
162
   char *result;
 
163
 
 
164
   if (s == NULL) {
 
165
      return NULL;
 
166
   }
 
167
 
 
168
#if defined(_WIN32)
 
169
   if ((result = _strdup(s)) == NULL) {
 
170
#else
 
171
   if ((result = strdup(s)) == NULL) {
 
172
#endif
 
173
      if (bugNumber == -1) {
 
174
         Panic("Unrecoverable memory allocation failure at %s:%d\n",
 
175
               file, lineno);
 
176
      } else {
 
177
         Panic("Unrecoverable memory allocation failure at %s:%d.  Bug "
 
178
               "number: %d\n", file, lineno, bugNumber);
 
179
      }
 
180
   }
 
181
 
 
182
   return result;
 
183
}
 
184
 
 
185
 
 
186
/*
 
187
 *-----------------------------------------------------------------------------
 
188
 *
 
189
 * UtilSafeStrndupInternal --
 
190
 *
 
191
 *      Returns a string consisting of first n characters of 's' if 's' has
 
192
 *      length >= 'n', otherwise returns a string duplicate of 's'.
 
193
 *
 
194
 * Results:
 
195
 *      Pointer to the duplicated string.
 
196
 *
 
197
 * Side effects:
 
198
 *      May Panic if ran out of memory.
 
199
 *
 
200
 *-----------------------------------------------------------------------------
 
201
 */
 
202
 
 
203
char *
 
204
Util_SafeInternalStrndup(int bugNumber,        // IN:
 
205
                         const char *s,        // IN:
 
206
                         size_t n,             // IN:
 
207
                         const char *file,     // IN:
 
208
                         int lineno)           // IN:
 
209
{
 
210
   size_t size;
 
211
   char *copy;
 
212
   const char *null;
 
213
 
 
214
   if (s == NULL) {
 
215
      return NULL;
 
216
   }
 
217
 
 
218
   null = (char *) memchr(s, '\0', n);
 
219
   size = null ? null - s: n;
 
220
   copy = (char *) malloc(size + 1);
 
221
 
 
222
   if (copy == NULL) {
 
223
      if (bugNumber == -1) {
 
224
         Panic("Unrecoverable memory allocation failure at %s:%d\n",
 
225
               file, lineno);
 
226
      } else {
 
227
         Panic("Unrecoverable memory allocation failure at %s:%d.  Bug "
 
228
               "number: %d\n", file, lineno, bugNumber);
 
229
      }
 
230
   }
 
231
 
 
232
   copy[size] = '\0';
 
233
 
 
234
   return (char *) memcpy(copy, s, size);
 
235
}