-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem_account.c
111 lines (93 loc) · 2.61 KB
/
mem_account.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
#include <stddef.h>
extern void * __libc_malloc(size_t);
extern void * __libc_calloc(size_t, size_t);
extern void * __libc_realloc(void *, size_t);
extern void * __libc_reallocarray(void *, size_t, size_t);
extern void * __libc_memalign(size_t, size_t);
extern void * __libc_valloc(size_t);
extern void * __libc_pvalloc(size_t);
extern void * __libc_free(void *);
extern size_t malloc_usable_size (void *);
size_t mem_account = 0;
size_t max_mem_account = 0;
void * malloc(size_t size)
{
void *ptr;
size_t usable_size;
ptr = __libc_malloc(size);
usable_size = malloc_usable_size(ptr);
mem_account += usable_size;
if (mem_account > max_mem_account) max_mem_account = mem_account;
return ptr;
}
void * calloc(size_t num, size_t size)
{
void *ptr;
size_t usable_size;
ptr = __libc_calloc(num, size);
usable_size = malloc_usable_size(ptr);
mem_account += usable_size;
if (mem_account > max_mem_account) max_mem_account = mem_account;
return ptr;
}
void * realloc(void *old_ptr, size_t size)
{
size_t old_usable_size;
void *new_ptr;
size_t new_usable_size;
old_usable_size = malloc_usable_size(old_ptr);
new_ptr = __libc_realloc(old_ptr, size);
new_usable_size = malloc_usable_size(new_ptr);
mem_account += new_usable_size - old_usable_size;
if (mem_account > max_mem_account) max_mem_account = mem_account;
return new_ptr;
}
void * reallocarray(void *old_ptr, size_t num, size_t size)
{
size_t old_usable_size;
void *new_ptr;
size_t new_usable_size;
old_usable_size = malloc_usable_size(old_ptr);
new_ptr = __libc_reallocarray(old_ptr, num, size);
new_usable_size = malloc_usable_size(new_ptr);
mem_account += new_usable_size - old_usable_size;
if (mem_account > max_mem_account) max_mem_account = mem_account;
return new_ptr;
}
void * memalign(size_t alignment, size_t size)
{
void *ptr;
size_t usable_size;
ptr = __libc_memalign(alignment, size);
usable_size = malloc_usable_size(ptr);
mem_account += usable_size;
if (mem_account > max_mem_account) max_mem_account = mem_account;
return ptr;
}
void * valloc(size_t size)
{
void *ptr;
size_t usable_size;
ptr = __libc_valloc(size);
usable_size = malloc_usable_size(ptr);
mem_account += usable_size;
if (mem_account > max_mem_account) max_mem_account = mem_account;
return ptr;
}
void * pvalloc(size_t size)
{
void *ptr;
size_t usable_size;
ptr = __libc_pvalloc(size);
usable_size = malloc_usable_size(ptr);
mem_account += usable_size;
if (mem_account > max_mem_account) max_mem_account = mem_account;
return ptr;
}
void free(void *ptr)
{
size_t usable_size;
usable_size = malloc_usable_size(ptr);
__libc_free(ptr);
mem_account -= usable_size;
}