| 1 |
#include <stdlib.h> |
| 2 |
#include <stdio.h> |
| 3 |
#include <string.h> |
| 4 |
#include <errno.h> |
| 5 |
#ifdef USE_MALLOC_WRAPPERS |
| 6 |
/* Don't wrap ourselves */ |
| 7 |
# undef USE_MALLOC_WRAPPERS |
| 8 |
#endif |
| 9 |
#include "malloc_wrap.h" |
| 10 |
|
| 11 |
void *wrap_calloc(size_t nmemb, size_t size, |
| 12 |
const char *file, unsigned int line, const char *func) { |
| 13 |
void *p = calloc(nmemb, size); |
| 14 |
if (NULL == p) { |
| 15 |
fprintf(stderr, |
| 16 |
"[%s] Failed to allocate %zu bytes at %s line %u: %s\n", |
| 17 |
func, nmemb * size, file, line, strerror(errno)); |
| 18 |
exit(EXIT_FAILURE); |
| 19 |
} |
| 20 |
return p; |
| 21 |
} |
| 22 |
|
| 23 |
void *wrap_malloc(size_t size, |
| 24 |
const char *file, unsigned int line, const char *func) { |
| 25 |
void *p = malloc(size); |
| 26 |
if (NULL == p) { |
| 27 |
fprintf(stderr, |
| 28 |
"[%s] Failed to allocate %zu bytes at %s line %u: %s\n", |
| 29 |
func, size, file, line, strerror(errno)); |
| 30 |
exit(EXIT_FAILURE); |
| 31 |
} |
| 32 |
return p; |
| 33 |
} |
| 34 |
|
| 35 |
void *wrap_realloc(void *ptr, size_t size, |
| 36 |
const char *file, unsigned int line, const char *func) { |
| 37 |
void *p = realloc(ptr, size); |
| 38 |
if (NULL == p) { |
| 39 |
fprintf(stderr, |
| 40 |
"[%s] Failed to allocate %zu bytes at %s line %u: %s\n", |
| 41 |
func, size, file, line, strerror(errno)); |
| 42 |
exit(EXIT_FAILURE); |
| 43 |
} |
| 44 |
return p; |
| 45 |
} |
| 46 |
|
| 47 |
char *wrap_strdup(const char *s, |
| 48 |
const char *file, unsigned int line, const char *func) { |
| 49 |
char *p = strdup(s); |
| 50 |
if (NULL == p) { |
| 51 |
fprintf(stderr, |
| 52 |
"[%s] Failed to allocate %zu bytes at %s line %u: %s\n", |
| 53 |
func, strlen(s), file, line, strerror(errno)); |
| 54 |
exit(EXIT_FAILURE); |
| 55 |
} |
| 56 |
return p; |
| 57 |
} |