-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemoryManager.cpp
More file actions
255 lines (224 loc) · 6.69 KB
/
MemoryManager.cpp
File metadata and controls
255 lines (224 loc) · 6.69 KB
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
#include "MemoryManager.h"
void MemoryManager::initialize(size_t sizeInWords)
{
if (sizeInWords <= 65536 && sizeInWords > 0)
{
if(storage)
shutdown();
this->sizeInWords = sizeInWords;
//Creates the memory array
storage = new uint8_t[sizeInWords*getWordSize()];
//Initializes first hole as entire array
blocks.push_back(new block(0,sizeInWords, true));
numHoles = 1;
}
}
unsigned MemoryManager::getWordSize()
{
return wordSize;
}
void MemoryManager::shutdown()
{
if (storage != nullptr)
{
//Needs to delete ll nodes and potentially free get list stuff
delete[] storage;
storage = nullptr;
auto iter = blocks.begin();
while(iter != blocks.end())
{
block* current = *iter;
iter++;
delete current;
}
blocks.clear();
}
}
void* MemoryManager::getList()
{
//As long as a memory array has been intiliazed and some memory has been allocated into the linked list such that
//the holes list is not in its intial state
if (storage)
{
uint16_t length = numHoles;
uint16_t* list = new uint16_t[(2*length + 1)];
list[0] = length;
int counter = 1;
for (auto iter = blocks.begin(); iter != blocks.end(); iter++)
{
if((*iter)->hole)
{
list[counter] = (*iter)->start;
counter++;
list[counter] = ((*iter)->end - (*iter)->start);
counter++;
}
}
return list;
}
//cout << "Invalid List" << endl;
return nullptr;
}
void* MemoryManager::allocate(size_t sizeInBytes)
{
uint16_t* list = static_cast<uint16_t*> (getList());
int blockLength = std::ceil((float)sizeInBytes/wordSize);
int holeInd = allocator(blockLength, list);
cout << holeInd << endl;
delete[] list;
if (holeInd == -1)
{
cout << "invalid list" << endl;
return nullptr;
}
//Find the block
auto iter = blocks.begin();
for (iter; iter != blocks.end(); iter++)
{
if ((*iter)->start == holeInd)
break;
}
//Could not find hole
block* current = *iter;
if (iter == blocks.end())
{
cout <<"Could not find hole" << endl;
return nullptr;
}
//Need to find start and end of new hole
int newStart = holeInd + blockLength;
int newEnd = current->end;
//Update the changing hole to a memblock
current->end = current->start + blockLength;
current->hole = false;
numHoles--;
//If the new block doesn't fit perfectly we need to append a hole to the end
if (newEnd - newStart != 0)
{
cout << "Adding Hole" << endl;
iter++;
blocks.insert(iter,new block(newStart, newEnd, true));
numHoles++;
}
return &storage[holeInd*wordSize];
}
void MemoryManager::free(void *address)
{
//convert address from bytes to blocks
int blockInd = ((uint8_t*)address - storage)/wordSize;
//Find the block
auto iter = blocks.begin();
for(iter; iter != blocks.end(); iter++)\
{
if ((*iter)->start == blockInd)
break;
}
block* current = *iter;
//If you cant find the block
if (iter == blocks.end() || current->hole == true)
return;
//Turn block to hole
current->hole = true;
numHoles++;
//3 cases, Block left hole right, block right and hole left, and hole left and hole right (end and start are the same as if a block was to the right or left respectivley)
auto prev = iter;
bool start = true;
auto next = iter;
bool end = true;
//To prevent seg faults, if we can move forward or backward we know we are not at the end/start
if(prev != blocks.begin())
{
prev--;
start = false;
}
if (next != blocks.end())
{
next++;
end = false;
}
if(!start && (*prev)->hole && !end && (*next)->hole)
{
block* prevBlock = *prev;
block* nextBlock = *next;
current->start = prevBlock->start;
current->end = nextBlock->end;
blocks.erase(prev);
blocks.erase(next);
delete prevBlock;
delete nextBlock;
numHoles--;
numHoles--;
}else if((start || (!start && !(*prev)->hole) && (*next)->hole))
{
//Block is at the start or there is a bloack to the left and there is a hole to the right merge into big hole
// (If there is not a hole this simply becomes a hole with the same start and end and we dont have to do anything)
block* nextBlock = *next;
current->end = nextBlock->end;
blocks.erase(next);
delete nextBlock;
numHoles--;
}else if (end || (!end && !(*next)->hole) && (*prev)->hole)
{
//Same as above case but at the end of the list
block* prevBlock = *prev;
current->start = prevBlock->start;
blocks.erase(prev);
delete prevBlock;
numHoles--;
}
}
void MemoryManager::setAllocator(std::function<int(int, void *)> allocator)
{
this->allocator = allocator;
}
void* MemoryManager::getMemoryStart()
{
return storage;
}
unsigned MemoryManager::getMemoryLimit()
{
return sizeInWords * wordSize;
}
void MemoryManager::printBlocks() {
for (auto iter = blocks.begin(); iter != blocks.end(); iter++)
{
std::cout << "Start: " << (*iter)->start << std::endl;
std::cout << "Length: " << (*iter)->end - (*iter)->start << std::endl;
std::cout << "Hole: " << (*iter)->hole << std::endl;
std::cout << std::endl;
}
}
// int bestFit(int sizeInWords, void *list)
// {
// uint16_t* listU = (uint16_t*)list;
// int minInd = -1;
// int min = -1;
// for (int i = 1; i < listU[0]; i = i+2)
// {
// //if the length of the hole is less than min (and its not first big enough hole) and greater than or equal
// //to the sizeInWords set it to the new min
// if (listU[i+1] >= sizeInWords && (min == -1 || listU[i+1] < min))
// {
// min = listU[i+1];
// minInd = listU[i];
// }
// }
// return minInd;
// };
// int worstFit(int sizeInWords, void *list)
// {
// uint16_t* listU = (uint16_t*)list;
// int minInd = -1;
// int min = -1;
// for (int i = 1; i < listU[0]; i = i+2)
// {
// //if the length of the hole is greater than min and greater than or equal
// //to the sizeInWords set it to the new min
// if (listU[i+1] >= sizeInWords && listU[i+1] > min)
// {
// min = listU[i+1];
// minInd = listU[i];
// }
// }
// return minInd;
// };