-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathallocator.c
executable file
·355 lines (269 loc) · 10.6 KB
/
allocator.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// allocator.c
#include "allocator.h"
#include <stdio.h>
#include <unistd.h>
#include <stddef.h>
#include <string.h>
#include <math.h>
/**
NOTE TO MARKER:
Coding HW2 was collaborated on with Marc Karp (1356562)
The following code does the required functions of hw2 but presented
many problems with pointer arithmetic to counter this we worked around
alot of the problems. Please note when tracing code. Thanks. **/
void* custom_malloc(size_t size)
{
if((double) size == 0)
{
return NULL; // requested size 0
}
// Add size of block and round up to next power of 2
size = sizeof(struct block) + size;
/*double exp = log2l((double) size);
exp = ceil(exp);*/ // CANT USE MATH.H WITH MAKEFILE NEEDE -lm
size_t return_size = 1;
while(return_size < size)
{
return_size = return_size << 1;
}
double request_size = (double) return_size;
double block_size = MAX_SIZE;
if(head == NULL)
{
//First malloc
//First malloc will fit data to head (heads size is changed accordingly by splitting below) [1]
head = sbrk(MAX_SIZE); // HEAP EXTENDED TO 1MiB and set start point to head
head->size = MAX_SIZE;
head->free = false; //will decrease size of head below
head->data = (void*)head + (sizeof(struct block)); // data will be in memory addresses directly after block metadata
// ONLY set data pointer once a malloc is called NOT when splitting blocks
struct block *split_block = head;
for (int i = 0; i < MAX_EXP; i++) // initiliaze merge_buddy to NULL
{
head->merge_buddy[i] = NULL;
}
// split blocks -> decrease sizes going left initially
while (block_size != request_size)
{
int count = 0;
block_size = head->size/2;
split_block = (void*) head + (int) block_size; // always split from head onwards
split_block->size = block_size;
split_block->next = head->next;
split_block->buddy = (((void*) split_block)- ((int) block_size)); //set buddy to block of same size adjacent to block
split_block->free = true;
for (int i = 0; i < MAX_EXP; i++) // initiliaze merge_buddy to NULL
{
split_block->merge_buddy[i] = NULL;
}
split_block->merge_buddy[0] = head->buddy; // split blocks to the right have only one merge buddy block pointer
while (head->merge_buddy[count] != NULL && count <=20)
{
count++;
}
head->merge_buddy[count] = split_block; // head must keep track of all merge buddies
head->size = block_size;
head->next = split_block;
head->buddy = split_block;
}
// fix to delete extra element from merge buddy array
int i = 0;
while (head->merge_buddy[i] != NULL)
{
i++;
}
head->merge_buddy[i-1] = NULL;
struct block *return_block = (void*)head + (sizeof(struct block)); // return block must point to where data can be added from ie. without block metadata
return return_block;
}
else
{
//2nd malloc onwards
struct block *curr = head;
struct block *return_block;
while(curr)
{
// searching smallest blocks first
if (curr->free == true && curr->size == request_size)
{
curr->free = false;
curr->data = (void*)curr + (sizeof(struct block));
return_block = (void*)curr + (sizeof(struct block));
return return_block;
}
else if(curr->free == true && curr->size > request_size)
{
// split the first block we find thats bigger than requested size
curr->free = false; //will decrease size of curr below
curr->data = (void*)curr + (sizeof(struct block));
struct block *split_block = curr;
while (block_size != request_size)
{
block_size = curr->size/2;
split_block = (void*) curr + (int) block_size;
split_block->size = block_size;
split_block->next = curr->next;
split_block->buddy = (((void*) split_block)- ((int) block_size));
split_block->free = true;
for (int i = 0; i < MAX_EXP; i++) // initiliaze merge_buddy to NULL
{
split_block->merge_buddy[i] = NULL;
}
split_block->merge_buddy[0] = curr->buddy;
int count = 0;
while (curr->merge_buddy[count] != NULL && count <=20) // current block must keep track of all split buddies despite changing size
{
count++;
}
curr->merge_buddy[count] = split_block;
curr->size = block_size;
curr->next = split_block;
curr->buddy = split_block;
count++;
}
//fix to delete last element from merge buddy array - above added final buddy for no reason
int i = 0;
while (curr->merge_buddy[i] != NULL)
{
i++;
}
curr->merge_buddy[i-1] = NULL;
return_block = (void*)curr + (sizeof(struct block));
return return_block;
}
curr = curr->next;
}
if(curr == head && head->free == true)
{
return NULL; //requested size could not be fulfilled
}
}
}
/** Mark a data block as free and merge free buddy blocks **/
void custom_free(void* ptr)
{
if (ptr == NULL)
{
//DO NOTHING
}
else
{
struct block *free_block = ptr - (int) sizeof(struct block);
// should we check if block is valid is in list by traversing head?
// what happens to the block metadata in each partition once we merge does it still exist?
free_block->free = true;
free_block->data = NULL;
bool valid_block = true;
while (free_block->size != MAX_SIZE && valid_block)
{
if (free_block->buddy == free_block->next)
{
//freeing a left child
struct block *right_buddy = free_block->buddy;
if ((right_buddy->size == free_block->size) && right_buddy->free == true)
{
free_block->size = (free_block->size *2);
free_block->next = right_buddy->next;
free_block->buddy = right_buddy->merge_buddy[0];
int i = 0;
while (free_block->merge_buddy[i] != NULL)
{
i++;
}
//fix for seg fault at last merge
if (i-1 == 0)
{
free_block->merge_buddy[i] = NULL;
}
else
{
free_block->merge_buddy[i-1] = NULL;
}
}
else
{
valid_block = false;
}
}
else
{
//freeing a right child
struct block *left_buddy = free_block->buddy;
if ((left_buddy->size == free_block->size) && left_buddy->free == true)
{
left_buddy->size = (left_buddy->size *2);
left_buddy->next = free_block->next;
int i = 0;
while (left_buddy->merge_buddy[i] != NULL)
{
i++;
}
left_buddy->buddy = left_buddy->merge_buddy[i-1];
left_buddy->merge_buddy[i-1] = NULL;
free_block = left_buddy;
}
else
{
valid_block = false;
}
}
//remove element at end
if (free_block->size == MAX_SIZE)
{
free_block->merge_buddy[0] = NULL;
}
}
}
}
/** Change the memory allocation of the data to have at least the requested size **/
void* custom_realloc(void* ptr, size_t size)
{
if (ptr == NULL)
{
return NULL;
}
struct block *src = ptr - sizeof(struct block);
if (src->size == size)
{
return ptr;
}
struct block *dest = (struct block*) custom_malloc(size);
memcpy(dest, src, (size_t) src->size);
custom_free(ptr);
return (void*) dest + sizeof(struct block);
}
/*------------------------------------*\
| DEBUG FUNCTIONS |
\*------------------------------------*/
/** Prints the metadata of a block **/
void print_block(struct block* b) {
if(!b) {
printf("NULL block\n");
}
else {
int i = 0;
printf("Strt = %p\n",b);
printf("Size = %lu\n",b->size);
printf("Free = %s\n",(b->free)?"true":"false");
printf("Data = %p\n",b->data);
printf("Next = %p\n",b->next);
printf("Buddy = %p\n",b->buddy);
printf("Merge Buddies = ");
while(b->merge_buddy[i] && i < MAX_EXP) {
printf("%p, ",b->merge_buddy[i]);
i++;
}
printf("\n\n");
}
}
/** Prints the metadata of all blocks **/
void print_list() {
struct block* curr = head;
printf("--HEAP--\n");
if(!head) printf("EMPTY\n");
while(curr) {
print_block(curr);
curr = curr->next;
}
printf("--END--\n");
}