-
Notifications
You must be signed in to change notification settings - Fork 0
/
mymalloc.c
357 lines (313 loc) · 7.5 KB
/
mymalloc.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
356
357
// Jack Anderson
// jja54
#include "mymalloc.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "mymalloc.h"
// Don't change or remove these constants.
#define MINIMUM_ALLOCATION 16
#define SIZE_MULTIPLE 8
#define PTR_ADD_BYTES(ptr, byte_offs) ((void*)(((char*)(ptr)) + (byte_offs)))
//My extra functions
typedef struct Block //16 byte struct!!
{
unsigned int size; //4 bytes
unsigned int used; //4 bytes
struct Block* next; //4 bytes
struct Block* prev; //4 bytes
} Block;
struct Block* head = NULL;
struct Block* tail = NULL;
void* heap_at_start = NULL;
void print_linked_list()
{
Block* block = NULL;
int block_num = 0;
for(block = head; block != NULL; block = block->next)
{
printf("Block %d is of size %u", block_num, (unsigned int)block->size);
block_num++;
if (block->used == 1)
{
printf(" and is USED! %u", (unsigned int)block);
if (block == tail)
{
printf(" I'm the TAIL!\n");
}
else if (block == head)
{
printf(" I'm the HEAD!\n");
}
else printf("\n");
}
else
{
printf(" and is FREE! %u", (unsigned int)block);
if (block == tail)
{
printf(" I'm the TAIL!\n");
}
else if (block == head)
{
printf(" I'm the HEAD!\n");
}
else printf("\n");
}
}
printf("\n");
}
Block* best_fit(unsigned int size)
{
if (head == NULL) //linked list hasn't started yet, so immediately leave
{
return NULL;
}
Block* current_best = NULL;
int current_best_gap;
//loop through the blocks
Block* block = NULL;
for(block = head; block != NULL; block = block->next)
{
if (block->used == 0)
{
if (block->size >= size)
{
if (current_best == NULL) //first iteration
{
current_best = block;
current_best_gap = (block->size - size);
}
if (block->size == size)
{
return block; //if it's perfect size, just return that!
}
else if ((block->size - size) < current_best_gap)
{
current_best = block;
current_best_gap = (block->size - size);
}
}
}
if (block == tail)
{
break;
}
}
if (current_best == NULL) return NULL;
else return current_best;
}
Block* create_block(unsigned int size, Block* last_block)
{
//HERE IS WHERE WE GO INTO BEST FIT CHECK
Block* block = best_fit(size);
if (block == NULL) //was no best fit, need to bump up sbrk
{
void* struct_address = sbrk(sizeof(Block));
Block* block = struct_address;
block->size = size;
block->next = NULL;
block->used = 1;
if (last_block != NULL)
{
block->prev = last_block;
last_block->next = block;
}
else
{
block->prev = NULL;
}
tail = block;
return block;
}
else
{
//time to check for splitting:
if (block->size > size && (block->size - size) >= 32) //can be split
{
unsigned int old_size = block->size;
//block is our current FULL allocated block
block->size = size;
block->used = 1;
Block* new_freed = block;
new_freed = PTR_ADD_BYTES(new_freed, sizeof(Block));
new_freed = PTR_ADD_BYTES(new_freed, size);
new_freed->used = 0;
new_freed->size = (old_size - size - sizeof(Block));
new_freed->prev = block;
new_freed->next = block->next;
block->next = new_freed;
}
else if ((block->size - size) < 32) //avoiding create degenerate block
{
block->used = 1;
//don't change the size of the block, leave it as is
}
if (last_block == block) //reusing the tail
{
tail = block;
block->next = NULL;
//prev remains the same
block->used = 1;
}
return block;
}
}
unsigned int round_up_size(unsigned int data_size)
{
if(data_size == 0)
return 0;
else if(data_size < MINIMUM_ALLOCATION)
return MINIMUM_ALLOCATION;
else
return (data_size + (SIZE_MULTIPLE - 1)) & ~(SIZE_MULTIPLE - 1);
}
void* my_malloc(unsigned int size)
{
if(size == 0)
return NULL;
size = round_up_size(size);
// ------- Don't remove anything above this line. -------
if (heap_at_start == NULL) //haven't started yet
{
heap_at_start = sbrk(0);
}
if (head == NULL) //first hasn't been created yet
{
head = create_block(size, tail); //allocate the block node with sbrk
tail = head;
void* p = sbrk(size); //p points to a block of "size" bytes
return p; //return the address of malloc'd block
}
//if not the first creation
Block* new_block = create_block(size, tail); //allocate the block node
if (new_block == tail)
{
void* p = sbrk(size); //p points to a block of "size" bytes
return p; //return the address of malloc'd block
}
else
{
unsigned int p; //bump up to give space where mem goes
p = ((unsigned int)new_block + 16);
return (void*)p;
}
}
int check_adjacent_is_free(Block* block)
{
if (block != NULL)
{
if (block->used == 0)
{
return 1;
}
}
return 0;
}
Block* coalesce_next(Block* block, Block* next)
{
unsigned int block_size = block->size;
unsigned int next_size = next->size;
unsigned int new_size = block_size + next_size + sizeof(Block);
block->size = new_size; //change the size to the new
block->next = next->next; //give the block the next of the next
//prev remains the same
if (next->next != NULL)
{
next->next->prev = block; //set new next's prev to this!
}
if (next == tail)
{
tail = block; //if next block was the tail, make the original block new tail
}
return block;
}
Block* coalesce_prev(Block* block, Block* prev)
{
unsigned int block_size = block->size;
unsigned int prev_size = prev->size;
unsigned int new_size = block_size + prev_size + sizeof(Block);
prev->size = new_size; //change the size to the new
prev->next = block->next;
if (block->next != NULL)
{
block->next->prev = prev;
}
if (block == tail)
{
tail = prev; //if block was tail, prev becomes tail
prev->next = NULL;
if (prev->prev != NULL)
{
prev->prev->next = NULL; //new tail shouldn't have a next
}
}
return prev;
}
Block* coalesce_both(Block* block, Block* next, Block* prev)
{
unsigned int block_size = block->size;
unsigned int prev_size = prev->size;
unsigned int next_size = next->size;
unsigned int new_size = block_size + sizeof(Block) + prev_size + next_size + sizeof(Block);
prev->size = new_size;
prev->next = next->next;
if (next->next != NULL)
{
next->next->prev = prev; //set new next's prev to this!
}
if (next == tail)
{
tail = prev;
}
return prev;
}
void my_free(void* ptr)
{
if(ptr == NULL)
{
return;
}
Block* block = ptr - 16; //access the struct before the malloc'd block
block->used = 0; //make it free
//check if able to coalesce:
Block* next = block->next;
Block* prev = block->prev;
int next_is_free = check_adjacent_is_free(next);
int prev_is_free = check_adjacent_is_free(prev);
if (next_is_free && prev_is_free)
{
block = coalesce_both(block, next, prev);
}
else if (next_is_free && !prev_is_free)
{
block = coalesce_next(block, next);
}
else if (prev_is_free && !next_is_free)
{
block = coalesce_prev(block, prev);
}
//now we have coalesced the blocks if possible, check if it was the tail
if ((unsigned int)block == (unsigned int)tail) //deallocing last block, just move brk
{
if (tail != head)
{
tail = tail->prev;
block->prev->next = NULL;
}
unsigned int neg_size = (0 - (unsigned int)block->size - sizeof(Block));
ptr = sbrk(neg_size); //bump back the break
void* heap_at_bump_back = sbrk(0);
if (heap_at_start == heap_at_bump_back) //our list is now empty
{
head = NULL;
tail = NULL;
}
}
else if (tail != NULL && (unsigned int)tail->used == 0)
{
unsigned int neg_size = (0 - (unsigned int)tail->size - sizeof(Block));
ptr = sbrk(neg_size); //bump back the break
}
return;
}