-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_allocator.cpp
230 lines (200 loc) · 6.25 KB
/
memory_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
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
#include "memory_allocator.h"
MemoryAllocator::MemoryAllocator()
{
id = 0;
}
MemoryAllocator::MemoryAllocator(int size)
: m_size(size)
{
id = 0;
}
MemoryAllocator MemoryAllocator::makeFromHoles(const QList<Segment> &holes, int size)
{
// initialize new allocator with c
MemoryAllocator allocator(size);
allocator.m_holes = holes;
// initialize the holes
allocator.cleanupHoles();
if(allocator.m_holes.back().endingAddress() > size)
throw QString("A hole exceded the allocated size");
if (allocator.m_holes[0].startingAddress() != 0) {
Segment seg(allocator.id++,
"Old Process",
0,
allocator.m_holes[0].startingAddress(),
SegmentType::PROCESS);
allocator.m_segments.push_back(seg);
}
for(int i = 1, n = allocator.m_holes.size(); i < n + 1; i++) {
int startingAddress = allocator.m_holes[i-1].endingAddress();
int segmentSize = i == n ?
size - startingAddress :
allocator.m_holes[i].startingAddress() - startingAddress;
Segment seg(allocator.id++,
"Old Process",
startingAddress,
segmentSize,
SegmentType::PROCESS);
if(segmentSize < 0) {
throw QString("Overlaping holes");
}
else if (segmentSize > 0) {
allocator.m_segments.push_back(seg);
}
}
return allocator;
}
void MemoryAllocator::deleteProcess(int processId)
{
auto first = m_segments.begin();
auto result = m_segments.begin();
while (first != m_segments.end()) {
if (first->processId() != processId) {
if (result != first)
*result = std::move(*first);
++result;
} else {
Segment seg(-1,
"Hole",
first->startingAddress(),
first->size(),
SegmentType::HOLE);
m_holes.push_back(seg);
}
++first;
}
m_segments.erase(result, m_segments.end());
cleanupHoles();
}
Segment MemoryAllocator::addSegment(const Segment &seg, AllocationType type)
{
switch(type) {
case AllocationType::FIRST_FIT :
return addSegmentIn(seg, comp_address_func);
break;
case AllocationType::BEST_FIT :
return addSegmentIn(seg, comp_size_func);
break;
case AllocationType::WORST_FIT :
return addSegmentIn(seg, comp_size_backward_func);
break;
default:
throw QString("Unknown AllocationType");
}
}
void MemoryAllocator::compact()
{
std::sort(m_segments.begin(), m_segments.end(), comp_address_func);
m_segments[0].setStartingAddress(0);
for(int i = 1; i < m_segments.size(); i++) {
m_segments[i].setStartingAddress(m_segments[i-1].endingAddress());
}
Segment hole(-1,
"Hole",
m_segments.back().endingAddress(),
m_size - m_segments.back().endingAddress(),
SegmentType::HOLE);
m_holes.clear();
m_holes.push_back(hole);
}
QList<Segment> MemoryAllocator::segments() const
{
return m_segments;
}
QList<Segment> MemoryAllocator::holes() const
{
return m_holes;
}
QList<Segment> MemoryAllocator::memoryLayout() const
{
QList<Segment> memory = m_segments + m_holes;
std::sort(memory.begin(), memory.end(), comp_address_func);
return memory;
}
Segment MemoryAllocator::addSegmentIn(
Segment seg,
std::function<bool(const Segment&, const Segment&)> comp_func)
{
std::sort(m_holes.begin(), m_holes.end(), comp_func);
bool done = false;
for(int i = 0; i < m_holes.size(); i++) {
if(m_holes[i].size() >= seg.size()) {
seg.setStartingAddress(m_holes[i].startingAddress());
int endAddress = m_holes[i].endingAddress();
m_holes[i].setStartingAddress(seg.endingAddress());
m_holes[i].setSize(
endAddress -
m_holes[i].startingAddress());
done = true;
if(m_holes[i].size() == 0) {
m_holes.erase(m_holes.begin() + i);
}
break;
}
}
if(!done)
throw QString("Segment Does't fit in Memory");
m_segments.push_back(seg);
return seg;
}
void MemoryAllocator::cleanupHoles()
{
std::sort(m_holes.begin(), m_holes.end(), comp_address_func);
auto first = m_holes.begin();
first++;
auto result = m_holes.begin();
while (first != m_holes.end()) {
if (first->startingAddress() != result->endingAddress()) {
result = first;
} else {
result->setSize(result->size() + first->size());
}
++first;
}
m_holes.erase(result + 1, m_holes.end());
}
int MemoryAllocator::size() const
{
return m_size;
}
void MemoryAllocator::setSize(int size)
{
m_size = size;
}
QList<Segment> MemoryAllocator::addProcess(QList<Segment> process, AllocationType type)
{
if(!process.size())
throw QString("Empty process");
if(type == AllocationType::WORST_FIT)
std::sort(process.begin(), process.end(), comp_size_backward_func);
else
std::sort(process.begin(), process.end(), comp_size_func);
QList<Segment> list;
for(Segment& seg: process) {
seg.setProcessId(id);
if(seg.type() != SegmentType::PROCESS) {
deleteProcess(id);
throw QString("Expected Process not Hole");
}
try {
list.push_back(addSegment(seg, type));
} catch (...){
deleteProcess(id);
throw QString("One or more segments doesn't fit im memory");
}
}
id++;
return list;
}
QDebug operator<<(QDebug dbg, const MemoryAllocator &allocator) {
dbg.nospace() << "Memory Allocator with size: " << allocator.size() << "\n\n"
<< "Memory segments: " << allocator.m_segments.size() << "\n\n";
for(const auto& seg : allocator.m_segments) {
dbg.nospace() << seg << "\n";
}
dbg.nospace() << "Hole segments: " << allocator.m_holes.size() << "\n\n";
for(const auto& seg : allocator.m_holes) {
dbg.nospace() << seg << "\n";
}
return dbg.nospace();
}