-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_allocator.c
375 lines (276 loc) · 9.51 KB
/
my_allocator.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
/*
File: my_allocator.c
Author: <your name>
Department of Computer Science
Texas A&M University
Date : <date>
Modified:
This file contains the implementation of the module "MY_ALLOCATOR".
*/
/*--------------------------------------------------------------------------*/
/* DEFINES */
/*--------------------------------------------------------------------------*/
/* -- (none) -- */
/*--------------------------------------------------------------------------*/
/* INCLUDES */
/*--------------------------------------------------------------------------*/
#include<stdlib.h>
#include "my_allocator.h"
#include <cmath>
#include <cstdint>
/*--------------------------------------------------------------------------*/
/* DATA STRUCTURES */
/*--------------------------------------------------------------------------*/
/* -- (none) -- */
/*--------------------------------------------------------------------------*/
/* CONSTANTS */
/*--------------------------------------------------------------------------*/
/* -- (none) -- */
/*--------------------------------------------------------------------------*/
/* FORWARDS */
/*--------------------------------------------------------------------------*/
/* -- (none) -- */
/*--------------------------------------------------------------------------*/
/* FUNCTIONS FOR MODULE MY_ALLOCATOR */
/*--------------------------------------------------------------------------*/
/* Don't forget to implement "init_allocator" and "release_allocator"! */
bool power_of_two(int n)
{
if (n == 0)
return 0;
while (n != 1)
{
if (n%2 != 0)
return 0;
n = n/2;
}
return 1;
}
extern int index_search(std::vector<Flist> flist, Header* head){
if (head->size == 0) return 0;
int seg_size = flist[0].seg_size;
int index = log2(head->size) - log2(seg_size);
return index;
}
extern Header* add_to_list(std::vector<Flist> flist, Header* head){
//get index
int index = index_search(flist, head);
//if flist is empty, just add to it
if (flist[index].first == NULL) {flist[index].first = head; flist[index].last = head;}
else{
flist[index].last->next = head;
head -> prev = flist[index].last;
flist[index].last = head;
}
}
extern void* remove_from_list(std::vector<Flist> flist, Header* head){
//get index
int index = index_search(flist, head);
//IF HEADER IS FIRST
if (flist[index].first == head)
flist[index].first = head -> next;
//IF HEADER IS LAST
else if (flist[index].last == head)
flist[index].last = head -> prev;
else if (head->prev == NULL && head->next == NULL) return 0;
//IF HEADER IS FIRST AND LAST
else if (flist[index].first == head && flist[index].last == head){
flist[index].first = NULL;
flist[index].last = NULL;
}
else if (head->next == NULL){
flist[index].last = head->prev;
head->prev->next = NULL;
}
else if (head->prev == NULL){
flist[index].first = head->next;
head->next->prev = NULL;
}
//OTHERWISE
else{
Header* tmp = head -> prev;
head->prev->next = head->next;
head->next->prev = tmp;
delete tmp;
}
}
Header* allocator_find_bi(int value){
int space; // The "next 1"
if (!power_of_two(value)) // If the number isnt a power of two
space = floor(log2(value)) + 1;
else(space = log2(value) + 1);
space = pow(2, space);
// Space now holds the next 2^n value
// Find the index of the freelist that holds the block size we want
int index = log2(space) - log2(flist[0].seg_size);
if (index < 0) return NULL; // No size in our free list
// Find a free block
Header* free_block = NULL;
Header* head;
// This loop goes through all the blocks that have a size greater than or equal to the desired value, and returns the first free block
for (int i = index; i < flist.size(); i++){
head = flist[i].first;
for (Header* ptr = head; (ptr != NULL && ptr->next != ptr); ptr = ptr->next){
if (ptr->isFree == true){
free_block = ptr;
break;}
}
}
if (free_block == NULL) return NULL; // If we didn't
return free_block;
}
Header* segment_cut(Header* h){
remove_from_list(flist, h);
intptr_t new_head = (intptr_t)h + sizeof(Header) + h->size/2;
h->size = h->size/2;
Header* h2 = (Header*)new_head;
add_to_list(flist, h);
add_to_list(flist, h2);
return h2;
}
//Need int return for exponential function
////////////////////// This function was taken from StackOverflow. It's simply an int version of pow() provided in the C library.
int IntPow(int x, unsigned int pow)
{
int ret = 1;
while ( pow != 0 )
{
if ( (pow & 1) == 1 )
ret *= x;
x *= x;
pow >>= 1;
}
return ret;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
extern Header* allocator_coalesce(Header* h){
//finding the buddy
// Get relative address
intptr_t rel_addr = ((intptr_t)h - (intptr_t)memstart);
//XOR with size of block
intptr_t xor_addr = ((intptr_t)rel_addr ^ (intptr_t)(h->size));
//Get absolute address
intptr_t abs_addr = xor_addr + (intptr_t)memstart;
Header* buddy = (Header*)abs_addr;
//Check if buddy can be coalesced
if(buddy -> isFree == false || h -> size != buddy -> size) return 0; // Can't do anything
else if(buddy -> isFree == true && h -> size == buddy -> size){
//coalesce
//find which one will absorb the other, which is leftmost in physical memory
Header* leftmost;
if (buddy > h) {leftmost = h; h = NULL;}
else {leftmost = buddy; buddy = NULL;}
//Remove the new block from the list and add it back into right place after adjusting memory;
remove_from_list(flist, leftmost);
leftmost -> size = leftmost->size*2;
add_to_list(flist, leftmost);
return leftmost;
}
}
extern unsigned int init_allocator(unsigned int _basic_block_size,
unsigned int _length){
// Handle impossible conditions
if (_basic_block_size <= 0 || _length <=0 ) return 0;
else if (_length < _basic_block_size) return 0;
else {
// Reserve next power of 2 of space
int space;
if (!power_of_two(_length)) // If the number isnt a power of two
space = floor(log2(_length)) + 1;
else(space = log2(_length));
space = pow(2, space);
std::cout << " \n Length Allocated : " << space << std::endl;
// Same for block size
int block;
if (!power_of_two(_basic_block_size)) // If the number isnt a power of two
block = floor(log2(_basic_block_size)) + 1;
else(block = log2(_basic_block_size));
block = pow(2, block);
std::cout << " \n Block Size Allocated : " << block << std::endl;
//Find how many free lists you need, set the correct amount
int num_lists = 0;
for (int i = space; i >= block; i = i/2)
num_lists++;
flist.resize(num_lists);
std::cout << " \n Number of free lists is : " << num_lists << std::endl;
//Initialize the free lists
for (int i = 0; i < num_lists; i++){
flist[i] = Flist();
flist[i].first = NULL;
flist[i].last = NULL;
flist[i].seg_size = block*IntPow(2, i);
}
//Load the free lists with blocks
int current_bytes = _length;
for (int i = num_lists - 1; i >= 0; i--){
flist[i].first = new Header();
flist[i].last = flist[i].first;
flist[i].first->next = NULL;
flist[i].first->prev = NULL;
if (_length < block * pow(2, i) ) {flist[i].first->size = 0;}
else{
if (current_bytes > block * IntPow(2, i-1) ){
current_bytes = current_bytes - block * IntPow(2, i);
flist[i].first->size = block * IntPow(2, i);
}
else{
flist[i].first->size = 0;}
}
flist[i].first->isFree = true;
std::cout << "Size of block in freelist " << i << " is " << flist[i].first->size << std::endl;
}
// Remove all blocks that have a size of zero, or blocks that should not be there
for (int i = num_lists - 1; i >= 0; i--){
if (flist[i].first->size==0){
flist[i].first == NULL;
flist[i].last == NULL;
}
}
// Malloc the memory pool
int memory;
for (int i = num_lists - 1; i >= 0; i--)
memory = memory + flist[i].first->size;
memstart = malloc(memory);
return space;
}
}
extern void release_allocator(){
// Free the memory allocated
free(memstart);
//Trick to deallocate memory for flist vector, swap with empty vector (free memory)
std::vector<Flist>().swap(flist);
}
extern Addr my_malloc(unsigned int _length) {
/*
Allocate _length number of bytes of free memory and returns the
address of the allocated portion. Returns 0 when out of memory.
This preliminary implementation simply hands the call over the
the C standard library!
Of course this needs to be replaced by your implementation.
*/
// Find the next highest byte value we want to look for
if (_length == 0) return 0;
Header *h = allocator_find_bi(_length + sizeof(Header));
if (h == NULL){
return 0;
}
//See if we should split the block
if ((h->size)/2 >= (_length + sizeof(Header))){ // If we can split block in two and still have room, split it
Header *h2 = segment_cut(h);
add_to_list(flist, h2);
return my_malloc(_length);
} else {
//cannot further split, return the address
h->isFree = false;
return (void *)(h+1);
//the address returned is for the memory, not including the header
}
return malloc((size_t)_length);
}
extern int my_free(Addr _a) {
Header* h = (Header*) _a -1;
h->isFree = true;
//Coalesce, allocator_coalesce takes care of adding back into list
Header *after_coalesce = allocator_coalesce(h);
return 0;
}