-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemwatch.c
147 lines (102 loc) · 3.14 KB
/
memwatch.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#define _GNU_SOURCE
#include <stdio.h>
#include <dlfcn.h>
#include <malloc.h>
#include "memwatch.h"
/* Function pointer typedefs */
typedef void * (*mallocptr)(size_t);
typedef void (*freeptr)(void *);
/* Pointer to the real malloc function */
static mallocptr real_malloc = NULL;
/* Pointer to the real free function */
static freeptr real_free = NULL;
/* Callback pointer */
static mallocfailptr malloc_fail_hook = NULL;
/* How much is currently allocated */
static size_t allocated_mem = 0;
/* Current memory limit */
/* size_t is unsigned, so -1 wraps around */
static size_t maxsize = -1;
void memwatch_init(void)
{
/* We need to disable -Wpedantic for GCC for warning:
* ISO C forbids conversion of object pointer to function pointer type [-Wpedantic]
*/
#if defined(__GNUC__)
_Pragma("GCC diagnostic push")
_Pragma("GCC diagnostic ignored \"-Wpedantic\"")
#endif
/* Find the real_malloc and free */
real_malloc = (mallocptr)dlsym(RTLD_NEXT, "malloc");
real_free = (freeptr)dlsym(RTLD_NEXT, "free");
#if defined(__GNUC__)
_Pragma("GCC diagnostic pop")
#endif
if(real_malloc == NULL)
printf("Error: Cannot find real_malloc! Error: %s\n", dlerror());
if(real_free == NULL)
printf("Error: Cannot find real_free! Error: %s\n", dlerror());
}
void * malloc(size_t size)
{
void * ptr = NULL;
size_t memsize = 0; /* amount of memory actually allocated */
/* This is the first time malloc is being called
If so, we need to initialize */
if(real_malloc == NULL)
memwatch_init();
/* check if we are beyond the limit. If so, call malloc_fail_hook() */
if(((allocated_mem + size) > maxsize) && (malloc_fail_hook != NULL))
malloc_fail_hook(allocated_mem, size, maxsize);
/* actually allocate */
ptr = real_malloc(size);
memsize = malloc_usable_size(ptr);
/* Add to allocated memory */
allocated_mem += memsize;
/* Some debugging */
/*printf("Allocating %ld bytes (Actual: %ld)\n", size, memsize); */
/*printf("Allocated memory now %ld\n", allocated_mem);*/
return ptr;
}
void free(void * ptr)
{
size_t memsize = 0;
/* umm ? */
if(real_free == NULL)
memwatch_init();
/* How much are we deleting */
memsize = malloc_usable_size(ptr);
/* Free it and subtract from allocated memory counter */
real_free(ptr);
allocated_mem -= memsize;
/*printf("Freeing %ld bytes\n", memsize);*/
/*printf("Allocated memory now %ld\n", allocated_mem);*/
}
size_t memwatch_query_allocated_memory(void)
{
return allocated_mem;
}
size_t memwatch_set_max_memory(size_t max)
{
size_t cur = maxsize;
maxsize = max;
/*! \todo Is this correct to do here? */
if((allocated_mem > maxsize) && (malloc_fail_hook != NULL))
malloc_fail_hook(allocated_mem, 0, maxsize);
return cur;
}
size_t memwatch_get_max_memory(void)
{
return maxsize;
}
void memwatch_set_malloc_fail_hook(mallocfailptr mhook)
{
malloc_fail_hook = mhook;
}
int memwatch_running(void)
{
if(real_free == NULL || real_malloc == NULL)
return 0;
else
return 1;
}