-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallocator.cpp
More file actions
214 lines (190 loc) · 4.88 KB
/
allocator.cpp
File metadata and controls
214 lines (190 loc) · 4.88 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
/*
* allocator.cpp
*
* Created on: Apr 15, 2018
* Author: hhx008
*/
# include <iostream>
# include "allocator.h"
# include "assert.h"
inline void BlockAllocator::deleteNode(node * n) {
node * prev = n->prev;
node * next = n->next;
prev->next = next;
next->prev = prev;
n->next = n->prev = NULL;
}
inline void BlockAllocator::insertNode(node * head, node * n) {
node * tmp = head->next;
n->prev = head;
head->next = n;
n->next = tmp;
tmp->prev = n;
assert(n->prev && n->next);
}
BlockAllocator::BlockAllocator(int map, int r) : maxAllocPower(map), allocRange(r) {
assert(r % (1 << map) == 0);
allocFreelist = new node* [maxAllocPower+1];
for (int i=0; i<=maxAllocPower; i++) {
allocFreelist[i] = new node();
allocFreelist[i]->prev = allocFreelist[i];
allocFreelist[i]->next = allocFreelist[i];
}
allocNodes = new node [allocRange];
for (int i=0; i<allocRange; i++) {
allocNodes[i].id = i;
allocNodes[i].power = -1;
allocNodes[i].free = true;
allocNodes[i].prev = allocNodes[i].next = NULL;
}
usage = 0;
// init freelist
initialized = false;
};
BlockAllocator::~BlockAllocator() {
delete [] allocNodes;
for (int i=0; i<=maxAllocPower; i++)
delete allocFreelist[i];
delete [] allocFreelist;
};
bool BlockAllocator::breakdown(int power) {
if (power == 0 || power > maxAllocPower)
return false;
if (allocFreelist[power]->next == allocFreelist[power])
if (!breakdown(power + 1))
return false;
assert(allocFreelist[power]->next != allocFreelist[power]);
node * first = allocFreelist[power]->next;
node * second = first + (1 << first->power)/2;
// delete first from freelist[power]
deleteNode(first);
first->power--;
second->power = first->power;
second->free = true;
// add first & second to freelist[power-1]
insertNode(allocFreelist[power-1], second);
insertNode(allocFreelist[power-1], first);
return true;
}
BlockAllocator::node* BlockAllocator::merge(node * n) {
node * buddy, * first = n;
bool flag;
int fsize;
while (first->power < maxAllocPower) {
// indicate it's first buddy or second buddy
fsize = 1 << first->power;
if ((first->id / fsize) % 2)
flag = false;
else flag = true;
if (flag)
buddy = first + fsize;
else buddy = first - fsize;
// buddy merge-able
if (buddy->free && buddy->power == first->power) {
// delete n & its buddy from freelist
deleteNode(buddy);
// generate new region & insert
// buddy is second
if (flag) {
buddy->power = -1;
first->power++;
} else { // buddy is first
first->power = -1;
buddy->power++;
first = buddy;
}
} else break;
}
return first;
};
void BlockAllocator::initFreelist() {
assert(!initialized);
if (initialized)
return;
// init freelist
node * head = allocFreelist[maxAllocPower];
int plus = 1 << maxAllocPower;
for (int i=0; i<allocRange; i+=plus) {
if (allocNodes[i].power < 0)
allocNodes[i].power = maxAllocPower;
}
for (int i=0; i < allocRange; i++) {
if (allocNodes[i].power >= 0 && allocNodes[i].free)
insertNode(allocFreelist[allocNodes[i].power], &allocNodes[i]);
}
initialized = true;
};
void BlockAllocator::markUsed(int id, int power) {
assert(!initialized);
if (initialized)
return;
int tpower = power;
node * buddy, * first = allocNodes + id;
bool flag;
int fsize;
first->power = power;
first->free = false;
while (tpower < maxAllocPower) {
// indicate it's first buddy or second buddy
fsize = 1 << tpower;
if ((first->id / fsize) % 2)
flag = false;
else flag = true;
if (flag)
buddy = first + fsize;
else buddy = first - fsize;
if (buddy->power >= 0)
break;
// buddy's unmarked
else buddy->power = tpower;
if (!flag)
buddy = first;
tpower++;
}
// usage += (1 << power);
};
int BlockAllocator::allocate(int power) {
assert(initialized);
if (!initialized)
return -1;
if (allocFreelist[power]->next == allocFreelist[power])
if (!breakdown(power + 1))
return -1;
assert(allocFreelist[power]->next != allocFreelist[power]);
node * tmp = allocFreelist[power]->next;
assert(tmp);
tmp->free = false;
deleteNode(tmp);
return tmp->id;
};
void BlockAllocator::free(int id) {
assert(initialized);
if (!initialized)
return;
node * tmp = allocNodes + id;
int power = tmp->power;
assert(!tmp->free);
tmp->free = true;
// add id back to freelist
node * finaln = merge(tmp);
insertNode(allocFreelist[finaln->power], finaln);
// usage -= (1 << power);
};
void BlockAllocator::printList() {
for (int i=0; i<=maxAllocPower; i++) {
std::cout << "===== [" << i << "] =====" << std::endl;
node * tmp = allocFreelist[i]->next;
while (tmp != allocFreelist[i]) {
std::cout << "id: " << tmp->id << ", prev:" << tmp->prev->id << ", power: " << tmp->power << std::endl;
tmp = tmp->next;
}
}
};
void BlockAllocator::printNodes() {
for (int i=0; i<allocRange; i++) {
if (allocNodes[i].free)
std::cout << allocNodes[i].power;
else std::cout << "F";
}
std::cout << std::endl;
};