-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc.c
More file actions
257 lines (190 loc) · 5.51 KB
/
malloc.c
File metadata and controls
257 lines (190 loc) · 5.51 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<stdint.h>
#include<stdbool.h>
#include<string.h>
#define HEAP_CAPACITY 64000
#define LOOKUP_CAPACITY 1024
#define HEAP_CAPACITY_WORDS (HEAP_CAPACITY/sizeof(uintptr_t*))
uintptr_t heap[HEAP_CAPACITY_WORDS] = {0};
size_t heap_size = 0;
uintptr_t* base_address = 0;
bool reachable_blks[1024] = {0};
typedef struct Node Node;
typedef struct mem_blk mem_blk;
struct mem_blk{
uintptr_t* start;
size_t size;
};
struct Node{
char x;
Node* left;
Node* right;
};
typedef struct {
size_t cnt;
mem_blk mem_blks[LOOKUP_CAPACITY];
}mem_blk_list;
mem_blk_list allocated_blks = {0};
mem_blk_list free_blks = {
.cnt = 1,
.mem_blks = {
[0] = {.start = heap, .size = sizeof(heap)}
}
};
mem_blk_list temp = {0};
Node* root;
void trace_heap(){
printf("Allocated blocks: \n");
for(size_t i = 0;i<allocated_blks.cnt;i++){
printf("address: %p\t size: %zu reachable: %s\n", allocated_blks.mem_blks[i].start, allocated_blks.mem_blks[i].size, reachable_blks[i]?"true":"false");
}
printf("\nAvailable free blocks: \n");
for(size_t i = 0;i<free_blks.cnt;i++){
printf("address: %p\t size: %zu \n", free_blks.mem_blks[i].start, free_blks.mem_blks[i].size);
}
printf("---------------------------------------------------------------------------------------------------\n");
}
void insert(mem_blk_list* list, void* start_address, size_t size){
assert(list->cnt<LOOKUP_CAPACITY);
list->mem_blks[list->cnt].start = start_address;
list->mem_blks[list->cnt].size = size;
for(size_t i = list->cnt;i>0 && list->mem_blks[i].start < list->mem_blks[i-1].start; --i){
const mem_blk blk = list->mem_blks[i];
list->mem_blks[i] = list->mem_blks[i-1];
list->mem_blks[i-1] = blk;
}
list->cnt++;
}
void remove_blk(mem_blk_list* list, size_t index){
assert(index < list->cnt);
for(size_t i = index;i<list->cnt-1;++i){
list->mem_blks[i] = list->mem_blks[i+1];
}
list->cnt--;
}
void merge_blks(mem_blk_list* des, const mem_blk_list* src){
des->cnt = 0;
for(size_t i = 0;i<src->cnt;i++){
mem_blk current_blk = src->mem_blks[i];
if(des->cnt>0){
mem_blk* previous_blk = &des->mem_blks[des->cnt-1];
if(previous_blk->start + previous_blk->size == current_blk.start){
previous_blk->size += current_blk.size;
}else{
insert(des,current_blk.start,current_blk.size);
}
}else{
insert(des,current_blk.start,current_blk.size);
}
}
}
int comparator(const void* blk1, const void* blk2){
const mem_blk* b1 = blk1;
const mem_blk* b2 = blk2;
return b1->start - b2->start;
}
int find(const mem_blk_list *list, uintptr_t* start_address){
for(size_t i = 0;i<list->cnt;i++){
if(list->mem_blks[i].start == start_address)
return i;
}
return -1;
}
void* allocate(size_t size){
size_t size_in_words = (size + sizeof(uintptr_t) - 1)/sizeof(uintptr_t);
if( size_in_words <= 0)
return NULL;
merge_blks(&temp, &free_blks);
free_blks = temp;
for(size_t i = 0;i<free_blks.cnt;++i){
const mem_blk free_blk = free_blks.mem_blks[i];
if(free_blk.size >= size_in_words){
remove_blk(&free_blks, i);
insert(&allocated_blks, free_blk.start, size_in_words);
const size_t remaining = free_blk.size - size_in_words;
if(remaining>0)
insert(&free_blks, free_blk.start + size_in_words, remaining);
return free_blk.start;
}
}
return NULL;
}
void deallocate(void* blk_address){
if(blk_address == NULL)
return;
const int index = find(&allocated_blks, blk_address);
assert(index>=0);
insert(&free_blks,allocated_blks.mem_blks[index].start, allocated_blks.mem_blks[index].size);
remove_blk(&allocated_blks, (size_t)index);
}
Node* generate_tree(size_t current_level, size_t max_level){
if(current_level<max_level){
Node* node = allocate(sizeof(Node));
node->x = (char)('a'+current_level);
node->left = generate_tree(current_level+1,max_level);
node->right = generate_tree(current_level+1,max_level);
return node;
}else{
return NULL;
}
}
void trace_tree(Node* root){
if(root != NULL){
printf("Value of the node: %c at address: %p\n",root->x,root);
trace_tree(root->left);
trace_tree(root->right);
}else{
return;
}
}
void mark(const uintptr_t* start, const uintptr_t* end){
for(; start<end; start+=1){
const uintptr_t* p = (const uintptr_t*) *start;
for(size_t i = 0;i<allocated_blks.cnt;i++){
mem_blk blk = allocated_blks.mem_blks[i];
if(p >= blk.start && p <= blk.start + blk.size){
if(!reachable_blks[i]){
reachable_blks[i] = true;
mark(blk.start,blk.start+blk.size);
}
}
}
}
}
void collect(){
const uintptr_t* start = __builtin_frame_address(0);
memset(reachable_blks,0,sizeof(reachable_blks));
mark(start, base_address+1);
printf("\nState of the Heap before Deallocation...\n");
trace_heap();
printf("\nDeallocation started..\n\n");
size_t cnt = 0;
uintptr_t* to_deallocate[1024];
//collect the blocks to deallocate;
for(size_t itr = 0; itr<allocated_blks.cnt;itr++){
if(!reachable_blks[itr]){
to_deallocate[cnt++] = allocated_blks.mem_blks[itr].start;
}
}
for(size_t itr = 0; itr<cnt;itr++){
deallocate(to_deallocate[itr]);
}
}
int main(){
base_address = __builtin_frame_address(0);
Node* root = generate_tree(0,1);
trace_tree(root);
printf("The root of the tree is %p\n",root);
//unreachable pointers
for(size_t itr = 1 ;itr<=5;itr++){
allocate(itr);
}
collect();
trace_heap();
//allocate to see if the free blocks are getting used;
allocate(1);
trace_heap();
return 1;
}