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