-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_allocator.cpp
executable file
·184 lines (172 loc) · 4.2 KB
/
my_allocator.cpp
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
///--------------------------------------------------------------------------------------
/*
AUTHOR: Akash Agarwal
1405231007-Computer Science Department
IET LUCKNOW
LIFE MOTTO: while(!(suceed=try()))
*/
///--------------------------------------------------------------------------------------
//#INCLUDES
#include <iostream>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include "my_allocator.h"
using namespace std;
//GLOBAL VARIABLES
bool isinitialized = 0;
void *memory_start;
void *last_valid_address;
int _basic_block_size;
int _length;
int req_mem;
mem_ptr *freeLists[32]; //Because INT_MAX = 2^31
//therefore there should be ceil(log2(INT_MAX)) = 32 freelists.
//METHODS' DEFINITION
unsigned int upper_powerof_two(unsigned int x)
{
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x++;
return x;
}
void* remove_addr(int index)
{
void *addr = freeLists[index]->ptr.back();
freeLists[index]->ptr.pop_back();
if(!freeLists[index]->ptr.size())
{
delete freeLists[index];
freeLists[index] = NULL;
}
return addr;
}
void addtoList(int index,void *addr)
{
if(!freeLists[index])
freeLists[index] = new mem_ptr;
freeLists[index]->ptr.push_back(addr);
}
void* return_addr(int index,void* addr)
{
if(pow(2,index) == req_mem)
return addr;
addtoList(index-1,addr);
cout << "addr" << (void*)addr << endl;
cout << pow(2,index)/2 << endl;
cout << (void*)((char*)addr + (int)pow(2,index)/2) << endl;
return_addr(index-1,((char*)addr + (int)pow(2,index)/2));
}
unsigned int init_allocator(unsigned int _basic_block_size, unsigned int _length)
{
_length = upper_powerof_two(_length);
_basic_block_size = upper_powerof_two(_basic_block_size);
cout << _length << endl
<< _basic_block_size;
if(_basic_block_size <= sizeof(mem_ptr))// sizeof(mem_ptr) = 16
_basic_block_size = 32;
::_basic_block_size = _basic_block_size;
if(_length < _basic_block_size)
_length = _basic_block_size;
::_length = _length;
cout << endl << ::_length
<< endl << ::_basic_block_size;
memory_start = sbrk(0);
cout << endl << memory_start;
sbrk(_length);
last_valid_address = sbrk(0);
cout << endl << last_valid_address;
isinitialized = 1;
int index = log2(_length);
freeLists[index] = new mem_ptr;
freeLists[index]->ptr.push_back(memory_start);
// cout << freeLists[index]->ptr[0];
return 0;
}
int release_allocator()
{
sbrk(-_length);
last_valid_address = sbrk(0);
for (int i = 0; i < 32; ++i)
{
delete freeLists[i];
freeLists[i] = NULL;
}
// cout << endl << last_valid_address;
return 0;
}
void* my_malloc(unsigned int _length)
{
_length = upper_powerof_two(_length);
req_mem = _length;
cout <<endl<< _length << endl;
if(!isinitialized)
{
cout << "INIT\n";
init_allocator(32,_length);
}
cout << "here\n";
int index = log2(_length);
// if(freeLists[index])
// return remove_addr(index);
while(!freeLists[index])
{
if(index > 32)
return NULL;
index++;
}
return return_addr(index,remove_addr(index));
}
int my_free(void* addr,int size)
{
int index = log2(size);
void *buddy;
int find_buddy = ((((char*)memory_start - (char*)addr))/size);
if(find_buddy % 2 != 0 )
buddy = (char*)addr - size;
else
buddy = (char*)addr + size;
if(buddy < memory_start || buddy > last_valid_address)
buddy = NULL;
if(!buddy)
return 0;
if(freeLists[index])
{
for(int it = 0;it < freeLists[index]->ptr.size();it++)
{
if(freeLists[index]->ptr[it] == buddy)
{
freeLists[index]->ptr.erase(freeLists[index]->ptr.begin() + it);
if(find_buddy % 2 != 0)
addr = buddy;
break;
}
else
{
freeLists[index]->ptr.push_back(addr);
return 0;
}
}
my_free(addr,size*2);
}
freeLists[index] = new mem_ptr;
freeLists[index]->ptr.push_back(addr);
return 0;
}
void show_freeList()
{
for (int i = 5; i < 32; ++i)
{
if(freeLists[i] != NULL)
{
for(std::vector<void*>::iterator it = freeLists[i]->ptr.begin() ; it != freeLists[i]->ptr.end() ; it++)
cout << "i= " << i << "address= " << *it<< "\n";
}
}
}