-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalloc.c
480 lines (333 loc) · 11.6 KB
/
malloc.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
#include "malloc.h"
// malloc.c created by Gabriel Dos Santos on 12/16/2021
// Important structs and defines used in malloc implementation
typedef struct ptr_with_size{
void * ptr;
long size;
} ptr_with_size;
//Metadata struct is 8 byte aligned * important for system optimization *
struct header {
struct header * prev;
struct header * next;
long size;
};
typedef struct header * Header;
#define INTMAX (2147483647)
#define PSUB(x, y) ((uintptr_t) x - (uintptr_t) y)
#define PADD(x, y) ((uintptr_t) x + (uintptr_t) y)
#define FREED (0)
#define USED (1)
#define ALIGNMENT (8)
#define ALLOCSIZE(x) ((int) (x + sizeof(struct header)))
#define SPLITSIZE(x) ((int) (x + sizeof(struct header) + ALIGNMENT))
void printFreeList();
void printAllocList();
void printLastMalloc();
char HeapInitalized = 0;
Header lastMalloc = NULL;
int HeapInitalizer() {
Header old_break = (Header) sbrk(sizeof(struct header));
//Insufficient space in Heap to create header
if(old_break == (void *) -1){
return 1;
}
//Initalize the Heap header
old_break->prev = NULL;
old_break->next = NULL;
old_break->size = 0;
HeapInitalized = 1;
return 0;
}
void printFreeList() {
int i = 0;
for(Header freeTop = (Header) PSUB(sbrk(0), sizeof(struct header)); freeTop != NULL; freeTop = freeTop->prev){
app_printf(2, "%d:%u:%u ", i++, freeTop->size, freeTop);
}
}
void printLastMalloc() {
app_printf(2, "LastMalloc: %u\n ", lastMalloc);
}
void printAllocList(){
int i = 0;
for(Header allocTop = lastMalloc; allocTop != NULL; allocTop = allocTop->prev){
app_printf(2, "%d:%u:%u ", i++, allocTop->size, allocTop);
}
}
// void * freeListAdd(uint64_t numbytes);
// Attempts to add an allocation into the free list
// On success it will add the new allocation and stitches together the list
// On failure it will return NULL and callee must deal with requesting more memory via sbrk
void * freeListAdd(uint64_t numbytes, Header currBlock) {
//Won't fit in the freeList
if(numbytes > INTMAX){
return NULL;
}
//Traverse the free list
for(; currBlock != NULL; currBlock = currBlock->prev) {
//Found a suficiently large enough region
if(currBlock->size >= (int) numbytes) {
//Determine whether remaining free space is useable (ie enough space for a header and smallest block)
if(currBlock->size >= SPLITSIZE(numbytes)) {
// Split the region
// STEP 1: UPDATE THE FREE LIST (replace node with new)
Header newFreeElt = (Header) PADD(currBlock, ALLOCSIZE(numbytes));
newFreeElt->next = currBlock->next;
newFreeElt->prev = currBlock->prev;
if(newFreeElt->next){
newFreeElt->next->prev = newFreeElt;
}
if(newFreeElt->prev){
newFreeElt->prev->next = newFreeElt;
}
newFreeElt->size = currBlock->size - ALLOCSIZE(numbytes);
currBlock->size = numbytes;
} else {
// Claim the whole region
// STEP 1: UPDATE THE FREE LIST (evict the current node and stitch)
Header prevFreeElt = currBlock->prev;
Header nextFreeElt = currBlock->next;
assert(currBlock->size >= (long int) numbytes);
if(prevFreeElt){
prevFreeElt->next = prevFreeElt->next->next;
}
if(nextFreeElt){
nextFreeElt->prev = nextFreeElt->prev->prev;
}
}
// STEP 2: UPDATE THE ALLOC LIST
if(!lastMalloc){
//Empty alloc list (this is the first element)
lastMalloc = currBlock;
lastMalloc->prev = NULL;
lastMalloc->next = NULL;
} else {
//Just add to the end of the alloc list
lastMalloc->next = currBlock;
currBlock->next = NULL;
currBlock->prev = lastMalloc;
lastMalloc = currBlock;
}
// STEP 3: RETURN THE NEWLY ALLOC'D POINTER
return (void *) PADD(currBlock, sizeof(struct header));
}
}
//Unavailable opening in free list
return NULL;
}
// heapTopAdd(uint64_t numbytes, Header currBlock)
// Adds a new element to the end of the free list (pre-gauranteed space)
// Upon success it returns a pointer to the payload
void * heapTopAdd(uint64_t numbytes, Header currBlock) {
// STEP 1: UPDATE THE FREE LIST
Header newFreeHead = (Header) PADD(currBlock, ALLOCSIZE(numbytes));
if(currBlock->prev){
currBlock->prev->next = newFreeHead;
}
newFreeHead->prev = currBlock->prev;
newFreeHead->next = NULL;
newFreeHead->size = 0;
// STEP 2; UPDATE THE ALLOC LIST
currBlock->prev = lastMalloc;
currBlock->next = NULL;
currBlock->size = numbytes;
if(lastMalloc){
lastMalloc->next = currBlock;
}
lastMalloc = currBlock;
return (void *) PADD(currBlock, sizeof(struct header));
}
void * malloc(uint64_t numbytes) {
//Sizeless alloc: do nothing
if(!numbytes || (numbytes > INTMAX)){
return NULL;
}
numbytes = ROUNDUP(numbytes, 8);
//First time calling malloc, must initialize free list
if(!HeapInitalized){
if(HeapInitalizer()){
//Failed to initalize the heap
return NULL;
}
}
//Initialize variables
Header freeListHead = (Header) PSUB(sbrk(0), sizeof(struct header));
//Check whether alloc fits within the free list
void * returnPointer = NULL;
if((returnPointer = freeListAdd(numbytes, freeListHead))) {
return returnPointer;
}
//Failed first-fit free-list lookup
//Check sufficient space in remaining heap
if((void *) -1 == sbrk(ALLOCSIZE(numbytes))) {
return NULL;
}
//Add allocation to the top of the heap
if((returnPointer = heapTopAdd(numbytes, freeListHead))){
return returnPointer;
}
//Every attempt has failed (should not reach here)
return NULL;
}
// void * findAddrFit(Header freeBlock, Header currBlock)
// Traverses a linked list to find an element
// that fits in between two nodes to maintain order
void * findAddrFit(Header freeBlock, Header currBlock) {
Header lastElt = NULL;
for(; freeBlock < currBlock; currBlock = currBlock->prev) {
lastElt = currBlock;
}
return lastElt;
}
void free(void *firstbyte) {
//Null pointer: do nothing
if(!firstbyte){
return;
}
Header freeBlock = (Header) PSUB(firstbyte, sizeof(struct header));
Header freeListHead = (Header) PSUB(sbrk(0), sizeof(struct header));
// STEP 1: UPDATE THE ALLOC LIST
Header prevAlloc = freeBlock->prev;
Header nextAlloc = freeBlock->next;
if(prevAlloc){
prevAlloc->next = prevAlloc->next->next;
}
if(nextAlloc){
nextAlloc->prev = nextAlloc->prev->prev;
}
if(lastMalloc == freeBlock) {
lastMalloc = prevAlloc;
}
// STEP 2: UPDATE THE FREE LIST
Header nextFreeElt = findAddrFit(freeBlock, freeListHead);
Header prevFreeElt = nextFreeElt->prev;
if(prevFreeElt){
prevFreeElt->next = freeBlock;
}
if(nextFreeElt){
nextFreeElt->prev = freeBlock;
}
freeBlock->prev = prevFreeElt;
freeBlock->next = nextFreeElt;
return;
}
void * calloc(uint64_t num, uint64_t sz) {
Header oldHeapHead = (Header) PSUB(sbrk(0), sizeof(struct header));
//Integer overflow check
if((num * sz) / sz != num){
return NULL;
}
void * newAddr = malloc(sz*num);
//Insufficient memory
if(!newAddr){
return NULL;
}
memset(newAddr, 0, sz*num);
return newAddr;
}
void * realloc(void * ptr, uint64_t sz) {
// Free the old pointer
free(ptr);
//Special Cases:
// (1) No preexisting data so just allocate
if(ptr == NULL){
return malloc(sz);
}
// (2) No new size so just free
if(sz == 0){
return NULL;
}
// Regular Case:
// 1) Malloc a new block of smaller sz
void * newAlloc = malloc(sz);
if(newAlloc == NULL){
return NULL;
}
uint64_t copySize;
Header oldPtrHead = (Header) PSUB(ptr, sizeof(struct header));
if(sz > (uint64_t) oldPtrHead->size){
copySize = oldPtrHead->size;
} else {
copySize = sz;
}
// 2) memcpy the old into the new
memcpy(newAlloc, ptr, copySize);
return newAlloc;
}
void defrag() {
Header freeListTop = (Header) PSUB(sbrk(0), sizeof(struct header));
freeListTop = freeListTop->prev; //Don't include heapHead into coalescing
Header freeListSubTop;
if(freeListTop){
freeListSubTop = freeListTop->prev;
}
for(; freeListTop != NULL; freeListTop = freeListTop->prev, freeListSubTop = freeListSubTop->prev) {
//There is no element capable of coalescing
if(freeListSubTop == NULL) {
break;
}
//Determine whether adjacent elements
if(freeListTop == (Header) PADD(freeListSubTop, ALLOCSIZE(freeListSubTop->size))) {
freeListSubTop->size += ALLOCSIZE(freeListTop->size);
freeListSubTop->next = freeListTop->next;
if(freeListTop->next){
freeListTop->next->prev = freeListSubTop;
}
}
}
}
int heap_info(heap_info_struct * info) {
Header freeHead = (Header) PSUB(sbrk(0), sizeof(struct header));
Header allocHead = lastMalloc;
//Acquire free-list info (free space and largest free check)
int freeSpace = 0;
int biggestFree = 0;
for(Header currBlock = freeHead; currBlock != NULL; currBlock = currBlock->prev){
int currSize = currBlock->size;
freeSpace += ALLOCSIZE(currSize);
if(currSize > biggestFree){
biggestFree = currSize;
}
}
info->free_space = freeSpace;
info->largest_free_chunk = biggestFree;
//Acquire malloc-list info (length, allocSize, allocPointer (in descending order))
int allocLength = 0;
for(Header currBlock = allocHead; currBlock != NULL; currBlock = currBlock->prev) {
allocLength++;
}
info->num_allocs = allocLength;
//No allocations present in the heap
if(allocLength == 0){
info->size_array = NULL;
info->ptr_array = NULL;
return 0;
}
//Allocating necessary arrays
long * sizeArray = malloc(sizeof(long)*allocLength);
void ** ptrArray = malloc(sizeof(void *)*allocLength);
ptr_with_size * allocList = malloc(sizeof(struct ptr_with_size)*allocLength);
if(!(sizeArray && ptrArray && allocList)){
//Insufficient space to complete the info struct/sort
free(sizeArray);
free(ptrArray);
free(allocList);
return -1;
}
int index = 0;
for(Header currBlock = allocHead; currBlock != NULL; currBlock = currBlock->prev, index++) {
//Traverse entire alloc linked list to store pointers and size in array
allocList[index].ptr = (void *) PADD(currBlock, sizeof(struct header));
allocList[index].size = currBlock->size;
}
//Sort the struct into descending order
_Quicksort(allocList, allocLength, sizeof(struct ptr_with_size), &Ptr_comparator);
//Export the sorted data in the malloc'd lists
for(index = 0; index < allocLength; index++) {
sizeArray[index] = allocList[index].size;
ptrArray[index] = allocList[index].ptr;
}
free(allocList);
info->size_array = sizeArray;
info->ptr_array = ptrArray;
return 0;
}