-
Notifications
You must be signed in to change notification settings - Fork 0
/
malloc.c
92 lines (86 loc) · 3.44 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
/***********************************************************************************************************************
implementation file : malloc.c
Author : <Costas Chatzopoulos -1115201300202- 29/10/2017>
Purpose : memory allocator
***********************************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "malloc.h"
#include "list.h"
#include "hashtable.h"
static listPtr level[8];
void *allocate(listPtr *list, size_t cbytes) {
assert(cbytes > 0 && cbytes <= PAGE);
void *pointer = NULL;
struct Page *page = NULL;
if (!listExists(list)) {
listCreate(list);
page = createPage(cbytes);
listInsert(*list, page);
pointer = getSegment(page);
} else {
page = listGetFirstData(*list);
pointer = getSegment(page);
if (pointer == NULL) {
do {
pointer = NULL;
page = listNext(*list);
if (page == NULL)
break;
pointer = getSegment(page);
} while (pointer == NULL);
if (pointer == NULL) {
page = createPage(cbytes);
listInsert(*list, page);
pointer = getSegment(page);
}
}
}
hashInsert(pointer, page);
return pointer;
}
void *mymalloc(size_t cbytes) {
assert(cbytes > 0);
if (cbytes > PAGE) {
return malloc(cbytes); /*CASE OF SYSTEM MALLOC*/
} else if (cbytes <= PAGE && cbytes > SEGMENT_2048) {
return allocate(&level[7], PAGE); /*CASE OF 4096 BYTE ALLOCATE*/
} else if (cbytes <= SEGMENT_2048 && cbytes > SEGMENT_1024) {
return allocate(&level[6], SEGMENT_2048); /*CASE OF 2048 BYTE ALLOCATE*/
} else if (cbytes <= SEGMENT_1024 && cbytes > SEGMENT_512) {
return allocate(&level[5], SEGMENT_1024); /*CASE OF 1024 BYTE ALLOCATE*/
} else if (cbytes <= SEGMENT_512 && cbytes > SEGMENT_256) {
return allocate(&level[4], SEGMENT_512); /*CASE OF 512* BYTE ALLOCATE*/
} else if (cbytes <= SEGMENT_256 && cbytes > SEGMENT_128) {
return allocate(&level[3], SEGMENT_256); /*CASE OF 256 BYTE ALLOCATE*/
} else if (cbytes <= SEGMENT_128 && cbytes > SEGMENT_64) {
return allocate(&level[2], SEGMENT_128); /*CASE OF 128 BYTE ALLOCATE*/
} else if (cbytes <= SEGMENT_64 && cbytes > SEGMENT_32) {
return allocate(&level[1], SEGMENT_64); /*CASE OF 64 BYTE ALLOCATE*/
} else if (cbytes <= SEGMENT_32) {
return allocate(&level[0], SEGMENT_32); /*CASE OF 32 BYTE ALLOCATE*/
}
}
void myfree(void *ptr) {
assert(ptr != NULL);
Pair pair = hashSearch(ptr);
if (pair != NULL) {
if (pair->freed) {
fprintf(stderr, "ERROR! Double free for pointer: %p at page(%d) %p\n", pair->key,
(unsigned int) pair->value->level, pair->value->segment);
exit(134);
} else {
pair->freed = true;
freeSegment(pair->value, ptr);
}
} else
free(ptr);
}
void printPointer(void *ptr) {
assert(ptr != NULL);
Pair pair = hashSearch(ptr);
if (pair != NULL) {
IT_PrintValue(stdout, pair->value, pair->key, pair->hash);
}
}