-
Notifications
You must be signed in to change notification settings - Fork 0
/
buddy_list.c
278 lines (225 loc) · 5.24 KB
/
buddy_list.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
#include "buddy_list.h"
#include <errno.h>
#include <stdio.h>
#include <string.h>
#define MIN_ALLOC_BITS 5
#define MAX_ALLOC_BITS 24
#define MIN_ALLOC ((size_t)1 << MIN_ALLOC_BITS)
#define MAX_ALLOC ((size_t)1 << MAX_ALLOC_BITS)
#define DATA_SIZES (MAX_ALLOC_BITS - MIN_ALLOC_BITS + 1)
#define SIZE_HEAP_DATA (sizeof(struct heap_data))
//#define ABUG
static void merge(struct heap_data *h1,struct heap_data *h2,int index);
static void free_block(struct heap_data *h,int index);
static struct heap_data* freelist[DATA_SIZES];
static struct heap_data* head;
static int get_index(size_t size)
{
int i;
for(i = DATA_SIZES - 1; i >= 0; i--){
if(size > (1 << (i + MIN_ALLOC_BITS - 1))){
break;
}
}
return i;
}
#ifdef ABUG
static void printlist(){
for(int i = DATA_SIZES - 1; i >= 0; i--){
if(freelist[i] == NULL){
fprintf(stderr, "%i: Empty\n",i);
}else{
struct heap_data *temp = freelist[i];
fprintf(stderr,"%i: ",i);
while(temp != NULL){
fprintf(stderr, "%p ",(void *) temp);
temp = temp->next;
}
fprintf(stderr, "\n");
}
}
fprintf(stderr, "\n");
}
#endif
static void add_node(struct heap_data *h,int index)
{
struct heap_data *tmp = freelist[index];
freelist[index] = h;
h->prev = NULL;
h->next = tmp;
}
static void remove_node(struct heap_data *h,int index)
{
struct heap_data *current = freelist[index];
struct heap_data *prev = NULL;
if (current == NULL)
return;
if (current == h) {
freelist[index] = h->next;
return;
}
current = freelist[index]->next;
prev = freelist[index];
while (current) {
if (current == h)
break;
prev = current;
current = current->next;
}
prev->next = current->next;
}
void init(){
head = sbrk(MAX_ALLOC+sizeof(struct heap_data));
if(head == (void*) -1){
perror("Cannot allocate memory");
}
head->size = MAX_ALLOC;
head->next = NULL;
head->prev = NULL;
freelist[DATA_SIZES - 1] = head;
}
void *malloc(size_t size){
#ifdef ABUG
fprintf(stderr, "We are in malloc: %lu\n",size);
#endif
if(head == NULL){
init();
}
if(size > (MAX_ALLOC - SIZE_HEAP_DATA) || size == 0){
return NULL;
}
int req = get_index(size + SIZE_HEAP_DATA);
int available = req;
struct heap_data *temphead = freelist[req];
while(temphead == NULL && available < (DATA_SIZES - 1)){
available++;
temphead = freelist[available];
}
if(temphead == NULL){
fprintf(stderr,"Not enough memory\n");
return NULL;
}
while(available > req){
#ifdef ABUG
fprintf(stderr,"available: %i\n",available);
printlist();
#endif
struct heap_data *parent = freelist[available];
freelist[available] = parent->next;
if(parent->next != NULL){
freelist[available]->prev = NULL;
}
available--;
struct heap_data *buddy1 = parent;
struct heap_data *buddy2 = (struct heap_data *) ((size_t) parent + (parent->size >> 1));
buddy1->size = buddy2->size = 1 << (available + MIN_ALLOC_BITS);
buddy1->prev = NULL;
buddy2->prev = buddy1;
buddy1->next = buddy2;
buddy2->next = freelist[available];
freelist[available] = buddy1;
}
struct heap_data *new_parent = freelist[available];
freelist[available] = freelist[available]->next;
if(new_parent->next != NULL){
new_parent->next->prev = NULL;
new_parent->prev = NULL;
}
#ifdef ABUG
printlist();
fprintf(stderr,"return %p, %lu\n",(void *) ((size_t) new_parent + sizeof(struct heap_data)),size);
#endif
return (void *) ((size_t) new_parent + sizeof(struct heap_data));
}
static void *contiguous(struct heap_data *h,size_t size)
{
size_t addr = ((size_t) h) - (size_t) head;
size_t buddy = addr ^ (size);
return (void *) (buddy + ((size_t) head));
}
static void free_block(struct heap_data *h,int index)
{
struct heap_data *aux;
//First block is the one that has just been added
aux = freelist[index]->next;
while (aux) {
if (aux == contiguous(h,h->size)) {
merge(aux,h,index);
return;
}
aux = aux->next;
}
return;
}
static void merge(struct heap_data *h1,struct heap_data *h2,int index)
{
struct heap_data *base;
if (h1 > h2)
base = h2;
else
base = h1;
base->size = base->size << 1;
remove_node(h1,index);
remove_node(h2,index);
add_node(base,index + 1);
if (base->size != MAX_ALLOC)
free_block(base,index + 1);
return;
}
void free(void *ptr)
{
#ifdef ABUG
fprintf(stderr,"We are in free: %p\n",ptr);
#endif
if (ptr == NULL)
return;
struct heap_data *h = (struct heap_data *) ((size_t) ptr - SIZE_HEAP_DATA);
int index = get_index(h->size);
h->next = freelist[index];
freelist[index] = h;
free_block(h,index);
#ifdef ABUG
printlist();
#endif
}
void *calloc(size_t nmemb, size_t size)
{
#ifdef ABUG
fprintf(stderr, "We are in calloc:\n");
#endif
void *ptr;
ptr = malloc(nmemb*size);
if (ptr) {
for (int i = 0; i < nmemb*size; i++) {
*((char *) ptr + i) = '\0';
}
}
return ptr;
}
void *realloc(void *ptr, size_t size)
{
#ifdef ABUG
fprintf(stderr, "We are in realloc: %p, %lu\n",ptr,size);
#endif
void *tmp;
struct heap_data *h;
if (ptr == NULL) {
return malloc(size);
}
h = (struct heap_data *) ((size_t) ptr - SIZE_HEAP_DATA);
// if (h == NULL)
// return malloc(size);
if (size) {
if((h->size - SIZE_HEAP_DATA) < size) {
tmp = malloc(size);
memcpy(tmp,ptr,h->size - SIZE_HEAP_DATA);
free(ptr);
} else {
tmp = ptr;
}
return tmp;
} else {
free(ptr);
}
return NULL;
}