-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalloc.c
223 lines (200 loc) · 5.21 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
#include "malloc.h"
Header base;
unsigned int h = sizeof(Header);//sizeof(unsigned int) + sizeof(struct Header *);
void printList(){
Header *traversePtr = &base;
unsigned short numNodes = 0;
printf("\n********** begin printList **********\n");
printf("#%4u: %p\n", numNodes++, traversePtr);
while(traversePtr->ptr){
printf("#%4u: %p\n", numNodes++, traversePtr->ptr);
traversePtr = traversePtr->ptr;
}
printf("********** end printList **********\n\n");
}
Header * findFollowingInList(Header* node){
Header *traversePtr = &base;
Header *returnPtr;
if(base.ptr == NULL)
returnPtr = NULL;
while(traversePtr->ptr){
if(traversePtr->ptr > node){
returnPtr = traversePtr->ptr;
break;
}
traversePtr = traversePtr->ptr;
}
return returnPtr;
}
Header * findPreviousInList(Header* node){
Header *traversePtr = &base;
Header *returnPtr;
if(base.ptr == NULL)
returnPtr = &base;
while(traversePtr->ptr){
if (traversePtr->ptr >= node){
returnPtr = traversePtr;
break;
}
traversePtr = traversePtr->ptr;
}
return returnPtr;
}
void * partitionAHole(Header *node, unsigned int nbytes){
unsigned int n = ((nbytes-1) + (h-((nbytes-1)%h)));
void *returnPtr = NULL;
if(node->size > n + 2*h){
node->size = node->size - h - n;
returnPtr = node + h + node->size + h;
(*(Header*)(returnPtr - h)).size = n;
}
else if(node->size == n + 2*h){
node->size = h;
returnPtr = node + 2*h;
(*(Header*)(returnPtr - h)).size = n;
}
else if(node->size == n){
/*since the whole hole is consumed, we have to delete this node*/
if(node->ptr)
findPreviousInList(node)->ptr = node->ptr;
node->ptr = NULL;
returnPtr = node + h;
}
return returnPtr;
}
void * malloc_simple(unsigned int nbytes) {
unsigned int n = ((nbytes-1) + (h-((nbytes-1)%h)));
void * returnPtr = sbrk(n+h);
(*(Header*)(returnPtr)).size = n+h;
(*(Header*)(returnPtr)).ptr = NULL;
return (returnPtr+h);
}
void * malloc_first_fit(unsigned int nbytes) {
void * returnPtr = NULL;
Header * traversePtr;
/*if there is nothing in the free list, directly expand the heap*/
if(!base.ptr)
return malloc_simple(nbytes);
else
traversePtr = base.ptr;
/*look for a hole*/
while(traversePtr){
returnPtr = partitionAHole(traversePtr, nbytes);
if(returnPtr)
return returnPtr;
else
traversePtr = traversePtr->ptr;
};
/*no hole is large enough to fit in the requested size*/
return malloc_simple(nbytes);
}
void * malloc_best_fit(unsigned int nbytes) {
unsigned int n = ((nbytes-1) + (h-((nbytes-1)%h)));
void *returnPtr = NULL;
Header *traversePtr, *bestFitPtr;
/*if there is nothing in the free list, directly expand the heap*/
if(!base.ptr)
return malloc_simple(n);
else{
traversePtr = base.ptr;
bestFitPtr = traversePtr;
}
/*find the best fit hole*/
while(traversePtr){
if((traversePtr->size >= n) && (traversePtr->size <= bestFitPtr->size))
bestFitPtr = traversePtr;
traversePtr = traversePtr->ptr;
};
returnPtr = partitionAHole(bestFitPtr, n);
if(returnPtr)
return returnPtr;
else
/*no hole is large enough to fit in the requested size*/
return malloc_simple(n);
}
void * malloc_worst_fit(unsigned int nbytes) {
unsigned int n = ((nbytes-1) + (h-((nbytes-1)%h)));
void *returnPtr = NULL;
Header *traversePtr, *worstFitPtr;
/*if there is nothing in the free list, directly expand the heap*/
if(!base.ptr)
return malloc_simple(n);
else{
traversePtr = base.ptr;
worstFitPtr = traversePtr;
}
/*find the worst fit hole*/
while(traversePtr){
if((traversePtr->size >= n) && (traversePtr->size >= worstFitPtr->size))
worstFitPtr = traversePtr;
traversePtr = traversePtr->ptr;
};
returnPtr = partitionAHole(worstFitPtr, n);
if(returnPtr)
return returnPtr;
else
/*no hole is large enough to fit in the requested size*/
return malloc_simple(n);
}
void insertToFreeList(Header* node){
Header *traversePtr;
if(base.ptr == NULL)
base.ptr = node;
else if(base.ptr > node){
node->ptr = base.ptr;
base.ptr = node;
return;
}
else{
traversePtr = base.ptr;
while(traversePtr <= node){
if(traversePtr->ptr >= node){
node->ptr = traversePtr->ptr;
traversePtr->ptr = node;
break;
}
else
traversePtr = traversePtr->ptr;
}
}
}
void mergeFreeBlocks(Header *front, Header *back){
front->size += back->size;
front->ptr = back->ptr;
back->size = 0;
back->ptr = NULL;
}
void free_simple(void *addr) {
Header *node = (Header *)(addr - h);
Header *nextNode = findFollowingInList(node);
Header *previousNode = findPreviousInList(node);
insertToFreeList(node);
/*merge with the next hole if it is adjacent to the current block*/
while(nextNode && (node+node->size >= nextNode)){
mergeFreeBlocks(node, nextNode);
nextNode = node->ptr;
}
/*merge with the previous hole if it is adjacent to the current block*/
while(previousNode+previousNode->size > node){
mergeFreeBlocks(previousNode, node);
node = previousNode;
}
}
void free_no_tail(void* addr) {
Header *lastNode = &base;
Header *secondLastNode;
free_simple(addr);
if(!base.ptr)
return;
while(lastNode){
if(lastNode->ptr)
lastNode = lastNode->ptr;
else
break;
}
if(lastNode+lastNode->size >= (Header*)sbrk(0)){
secondLastNode = findPreviousInList(lastNode);
sbrk(-lastNode->size);
secondLastNode->ptr = NULL;
}
}