-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrap_allocation.c
93 lines (76 loc) · 1.92 KB
/
wrap_allocation.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <dlfcn.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "alloc_stats.h"
mstat_t MEM_STAT = { 0 };
void *malloc(size_t size)
{
static void *(*real)(size_t) = NULL;
void *p;
if (real == NULL)
resolve_symbol((void **)&real, "malloc");
p = real(size);
++MEM_STAT.alloc;
if (VERBOSE)
fprintf(stderr, "malloc(size=%zu) -> %p\n", size, p);
return p;
}
void free(void *ptr)
{
static void (*real)(void *) = NULL;
if (real == NULL)
resolve_symbol((void **)&real, "free");
if (ptr == NULL)
return;
++MEM_STAT.free;
if (VERBOSE)
fprintf(stderr, "free(ptr=%p)\n", ptr);
real(ptr);
}
void *calloc(size_t nmemb, size_t size)
{
static void *(*real)(size_t, size_t) = NULL;
void *p;
if (real == NULL)
resolve_symbol((void **)&real, "calloc");
p = real(nmemb, size);
++MEM_STAT.calloc;
if (VERBOSE)
fprintf(stderr, "calloc(nmemb=%zu, size=%zu) -> %p\n", nmemb, size, p);
return p;
}
void *realloc(void *ptr, size_t size)
{
static void *(*real)(void *, size_t) = NULL;
void *p;
if (real == NULL)
resolve_symbol((void **)&real, "realloc");
p = real(ptr, size);
if (ptr == NULL)
++MEM_STAT.alloc;
else
++MEM_STAT.realloc;
if (VERBOSE)
fprintf(stderr, "realloc(ptr=%p, size=%zu) -> %p\n", ptr, size, p);
return p;
}
void *reallocarray(void *ptr, size_t nmemb, size_t size)
{
static void *(*real)(void *, size_t, size_t) = NULL;
void *p;
if (real == NULL)
resolve_symbol((void **)&real, "reallocarray");
p = real(ptr, nmemb, size);
if (ptr == NULL)
++MEM_STAT.alloc;
else
++MEM_STAT.realloc;
if (VERBOSE)
fprintf(stderr, "realloc(ptr=%p, nmemb=%zu, size=%zu) -> %p\n",
ptr, nmemb, size, p);
return p;
}