/
lib_mem.c
240 lines (203 loc) · 5.05 KB
/
lib_mem.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#define LIB_MEM
#include <malloc.h>
#include "lib_mem.h"
static void lib_mem_init_hook(void);
static void *lib_mem_malloc_hook(size_t, const void *);
static void *(*lib_mem_malloc_initial_hook)(size_t, const void *);
void (*__malloc_initialize_hook)(void) = lib_mem_init_hook;
static t_lib_mem_reference lib_mem_references;
static int lib_mem_function_depth = 0;
void lib_mem_increase_function_depth()
{
lib_mem_function_depth++;
}
void lib_mem_decrease_function_depth()
{
lib_mem_function_depth--;
}
static void lib_mem_init_hook(void)
{
lib_mem_malloc_initial_hook = __malloc_hook;
__malloc_hook = &lib_mem_malloc_hook;
}
static void *lib_mem_malloc_hook(size_t size, unused const void *parent)
{
void *result;
t_lib_mem_reference reference;
__malloc_hook = lib_mem_malloc_initial_hook;
result = malloc(size);
if (!result)
return NULL;
reference = malloc(sizeof(*reference));
if (!reference)
{
free(result);
return NULL;
}
reference->dependencies = NULL;
reference->parents = NULL;
reference->address = result;
reference->next = lib_mem_references;
reference->allowed_depth = lib_mem_function_depth;
lib_mem_references = reference;
__malloc_hook = &lib_mem_malloc_hook;
return result;
}
static t_lib_mem_reference lib_mem_find_reference(void *address)
{
t_lib_mem_reference list = lib_mem_references;
while (list)
{
if (list->address == address)
return list;
list = list->next;
}
return NULL;
}
static void lib_mem_remove_from_list(t_lib_mem_list *list, void *what)
{
t_lib_mem_list element = *list;
t_lib_mem_list prev = NULL;
while (element)
{
t_lib_mem_list next = element->next;
if (element->target == what)
{
if (prev == NULL)
*list = next;
else
prev->next = next;
free(element);
}
element = next;
}
}
static void lib_mem_unlink_internal(t_lib_mem_reference from, void *to)
{
t_lib_mem_reference to_ref = lib_mem_find_reference(to);
if (to_ref)
lib_mem_remove_from_list(&(to_ref->dependencies), from);
lib_mem_remove_from_list(&(from->parents), to);
}
static bool lib_mem_remove_reference(t_lib_mem_reference over)
{
t_lib_mem_reference prev = NULL;
bool ret = true;
if (over != lib_mem_references)
while (prev == NULL || prev->next != over)
{
if (prev == NULL)
prev = lib_mem_references;
else
prev = prev->next;
}
if (prev != NULL)
prev->next = over->next;
else
lib_mem_references = over->next;
while (over->dependencies != NULL)
{
t_lib_mem_list next = over->dependencies->next;
lib_mem_unlink_internal(over->dependencies->target, over->address);
over->dependencies = next;
ret = false;
}
while (over->parents != NULL)
{
t_lib_mem_list next = over->parents->next;
lib_mem_unlink_internal(over, over->parents->target);
over->parents = next;
}
free(over->address);
free(over);
return ret;
}
void lib_mem_check_references()
{
t_lib_mem_reference list;
bool check_done = false;
while (!check_done)
{
check_done = true;
list = lib_mem_references;
while (list)
{
t_lib_mem_reference next = list->next;
if (list->allowed_depth > lib_mem_function_depth
&& list->parents == NULL
&& !list->is_returned)
check_done = lib_mem_remove_reference(list);
else if (list->is_returned)
list->is_returned = 0;
list = next;
}
}
}
void lib_mem_remove_links(void *what)
{
t_lib_mem_reference list = lib_mem_references;
t_lib_mem_reference what_ref = lib_mem_find_reference(what);
if (!what_ref)
while (list)
{
if (list->parents) {
lib_mem_remove_from_list(&(list->parents), what);
}
list = list->next;
}
}
void lib_mem_set_return(void *what)
{
t_lib_mem_reference ref = lib_mem_find_reference(what);
if (ref)
{
ref->is_returned = true;
ref->allowed_depth = lib_mem_function_depth - 1;
}
}
void *lib_mem_link_value(void *what, void *with)
{
t_lib_mem_reference what_ref = lib_mem_find_reference(what);
t_lib_mem_reference with_ref = lib_mem_find_reference(with);
__malloc_hook = lib_mem_malloc_initial_hook;
if (what_ref && with_ref)
{
t_lib_mem_list element = malloc(sizeof(*element));
if (element)
{
element->target = what_ref;
element->next = with_ref->dependencies;
with_ref->dependencies = element;
}
}
if (what_ref)
{
t_lib_mem_list element = malloc(sizeof(*element));
if (element)
{
element->target = with;
element->next = what_ref->parents;
what_ref->parents = element;
}
}
__malloc_hook = &lib_mem_malloc_hook;
return what;
}
void lib_mem_unlink(void *what, void *with)
{
t_lib_mem_reference what_ref = lib_mem_find_reference(what);
if (what_ref)
lib_mem_unlink_internal(what_ref, with);
}
void _free(void *what)
{
t_lib_mem_reference what_ref = lib_mem_find_reference(what);
if (what_ref)
lib_mem_remove_reference(what_ref);
else
free(what);
}
bool lib_mem_is_managed_variable(void *what)
{
return lib_mem_find_reference(what) != NULL;
}