Skip to content

Commit c55ac21

Browse files
committed
Print error message when malloc/calloc/realloc failed, always
1 parent e321f26 commit c55ac21

File tree

1 file changed

+38
-26
lines changed

1 file changed

+38
-26
lines changed

src/drivers/common/mem_alloc.c

Lines changed: 38 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
5252
#endif
5353

5454
#if 0
55-
/*----< ncmpii_init_malloc_tracing() >----------------------------------------*/
55+
/*----< ncmpii_init_malloc_tracing() >---------------------------------------*/
5656
void ncmpii_init_malloc_tracing(void)
5757
{
5858
ncmpii_mem_alloc = 0;
@@ -88,7 +88,7 @@ void walker(const void *node, const VISIT which, const int depth) {
8888
fprintf(stdout, "Warning: malloc yet to be freed (buf=%p size=%zd filename=%s func=%s line=%d)\n", f->buf, f->size, f->filename, f->func, f->lineno);
8989
}
9090

91-
/*----< ncmpii_add_mem_entry() >----------------------------------------------*/
91+
/*----< ncmpii_add_mem_entry() >---------------------------------------------*/
9292
/* add a new malloc entry to the table */
9393
static
9494
void ncmpii_add_mem_entry(void *buf,
@@ -178,8 +178,8 @@ int ncmpii_del_mem_entry(void *buf)
178178
}
179179
#endif
180180

181-
/*----< NCI_Malloc_fn() >-----------------------------------------------------*/
182-
/* This subroutine is esstentially the same as calling malloc().
181+
/*----< NCI_Malloc_fn() >----------------------------------------------------*/
182+
/* This subroutine is essentially the same as calling malloc().
183183
* According to malloc man page, If size is 0, then malloc() returns either
184184
* NULL, or a unique pointer value that can later be successfully passed to
185185
* free(). Thus, there is no need to check whether size is zero.
@@ -190,46 +190,52 @@ void *NCI_Malloc_fn(size_t size,
190190
const char *filename)
191191
{
192192
void *buf = malloc(size);
193-
#ifdef PNETCDF_DEBUG
193+
194194
if (size > 0 && buf == NULL)
195195
fprintf(stderr, "malloc(%zd) failed in file %s func %s line %d\n",
196196
size, filename, func, lineno);
197-
#endif
197+
198198
#ifdef PNC_MALLOC_TRACE
199199
ncmpii_add_mem_entry(buf, size, lineno, func, filename);
200200
#endif
201+
201202
return buf;
202203
}
203204

204205

205206
/*----< NCI_Strdup() >-------------------------------------------------------*/
206-
/* This subroutine is esstentially the same as calling strdup().
207+
/* This subroutine is essentially the same as calling strdup().
207208
*/
208209
void *NCI_Strdup_fn(const char *src,
209210
const int lineno,
210211
const char *func,
211212
const char *filename)
212213
{
214+
size_t len;
215+
void *buf;
216+
213217
if (src == NULL) return NULL;
214218

215-
size_t len = strlen(src);
216-
void *buf = malloc(len + 1);
217-
#ifdef PNETCDF_DEBUG
219+
len = strlen(src);
220+
buf = malloc(len + 1);
221+
218222
if (len >= 0 && buf == NULL)
219223
fprintf(stderr, "malloc(%zd) failed in file %s func %s line %d\n",
220224
len, filename, func, lineno);
221-
#endif
225+
222226
if (len > 0) memcpy(buf, src, len);
227+
223228
((char*)buf)[len] = '\0';
229+
224230
#ifdef PNC_MALLOC_TRACE
225231
ncmpii_add_mem_entry(buf, len, lineno, func, filename);
226232
#endif
233+
227234
return buf;
228235
}
229236

230-
231-
/*----< NCI_Calloc_fn() >-----------------------------------------------------*/
232-
/* This subroutine is esstentially the same as calling calloc().
237+
/*----< NCI_Calloc_fn() >----------------------------------------------------*/
238+
/* This subroutine is essentially the same as calling calloc().
233239
* According to calloc man page, If nelem is 0, then calloc() returns either
234240
* NULL, or a unique pointer value that can later be successfully passed to
235241
* free(). Thus, there is no need to check whether nelem is zero.
@@ -241,20 +247,21 @@ void *NCI_Calloc_fn(size_t nelem,
241247
const char *filename)
242248
{
243249
void *buf = calloc(nelem, elsize);
244-
#ifdef PNETCDF_DEBUG
250+
245251
if (nelem > 0 && buf == NULL)
246252
fprintf(stderr, "calloc(%zd, %zd) failed in file %s func %s line %d\n",
247253
nelem, elsize, filename, func, lineno);
248-
#endif
254+
249255
#ifdef PNC_MALLOC_TRACE
250256
ncmpii_add_mem_entry(buf, nelem * elsize, lineno, func, filename);
251257
#endif
258+
252259
return buf;
253260
}
254261

255262

256-
/*----< NCI_Realloc_fn() >----------------------------------------------------*/
257-
/* This subroutine is esstentially the same as calling realloc().
263+
/*----< NCI_Realloc_fn() >---------------------------------------------------*/
264+
/* This subroutine is essentially the same as calling realloc().
258265
* According to realloc man page, if ptr is NULL, then the call is equivalent
259266
* to malloc(size), for all values of size; if size is equal to zero, and ptr
260267
* is not NULL, then the call is equivalent to free(ptr). Unless ptr is NULL,
@@ -268,6 +275,8 @@ void *NCI_Realloc_fn(void *ptr,
268275
const char *func,
269276
const char *filename)
270277
{
278+
void *buf;
279+
271280
if (ptr == NULL) return NCI_Malloc_fn(size, lineno, func, filename);
272281

273282
if (size == 0) {
@@ -277,24 +286,25 @@ void *NCI_Realloc_fn(void *ptr,
277286

278287
#ifdef PNC_MALLOC_TRACE
279288
if (ncmpii_del_mem_entry(ptr) != 0)
280-
fprintf(stderr, "realloc failed in file %s func %s line %d\n",
289+
fprintf(stderr, "realloc failed in file %s func %s line %d\n",
281290
filename, func, lineno);
282291
#endif
283-
void *buf = (void *) realloc(ptr, size);
284-
#ifdef PNETCDF_DEBUG
292+
293+
buf = (void *) realloc(ptr, size);
285294
if (buf == NULL)
286-
fprintf(stderr, "realloc failed in file %s func %s line %d\n",
295+
fprintf(stderr, "realloc failed in file %s func %s line %d\n",
287296
filename, func, lineno);
288-
#endif
297+
289298
#ifdef PNC_MALLOC_TRACE
290299
ncmpii_add_mem_entry(buf, size, lineno, func, filename);
291300
#endif
301+
292302
return buf;
293303
}
294304

295305

296-
/*----< NCI_Free_fn() >-------------------------------------------------------*/
297-
/* This subroutine is esstentially the same as calling free().
306+
/*----< NCI_Free_fn() >------------------------------------------------------*/
307+
/* This subroutine is essentially the same as calling free().
298308
* According to free man page, free() frees the memory space pointed to by ptr,
299309
* which must have been returned by a previous call to malloc(), calloc() or
300310
* realloc(). Otherwise, or if free(ptr) has already been called before,
@@ -306,11 +316,13 @@ void NCI_Free_fn(void *ptr,
306316
const char *filename)
307317
{
308318
if (ptr == NULL) return;
319+
309320
#ifdef PNC_MALLOC_TRACE
310321
if (ncmpii_del_mem_entry(ptr) != 0)
311-
fprintf(stderr, "free failed in file %s func %s line %d\n",
322+
fprintf(stderr, "free failed in file %s func %s line %d\n",
312323
filename, func, lineno);
313324
#endif
325+
314326
free(ptr);
315327
}
316328

0 commit comments

Comments
 (0)