-
Notifications
You must be signed in to change notification settings - Fork 0
/
BuddyAllocator.cpp
446 lines (368 loc) · 16.5 KB
/
BuddyAllocator.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
//
// Created by Hakan Halil on 1.08.20.
//
#include "BuddyAllocator.h"
#include <iostream>
#include <cmath>
#include "Node.h"
const char* BUDDY_INIT_EXCEPTION_MSG = "Allocator cannot be initialized with size less than twice of minimum block size";
const char* BUDDY_INIT_MISALIGNED_MEMORY_EXCEPTION_MSG = "Misaligned memory";
const char* BUDDY_INIT_WITH_NULLPTR_EXCEPTION_MSG = "Allocator cannot be initialized using nullptr as the memory block to be managed";
const char* BUDDY_FREE_EXCEPTION_MSG = "Input address is not managed by the Allocator";
Allocator::Allocator(void *addr, size_t size) {
if (addr == nullptr) {
throw Exception(BUDDY_INIT_WITH_NULLPTR_EXCEPTION_MSG);
}
if ((uintptr_t) addr % alignof(Node*)) {
throw Exception(BUDDY_INIT_MISALIGNED_MEMORY_EXCEPTION_MSG);
}
if (size < min_block_size * 2) {
// Is throwing exception in constructor a good practice?
throw Exception(BUDDY_INIT_EXCEPTION_MSG);
}
base_ptr = (uint8_t *)(addr);
actual_size = size;
// max_memory_log is the ceiled log, example:
// 500 size -> log2(512)
max_memory_log = floor(log2(size)); //ceil(log2(size));
max_memory_size = (1 << max_memory_log);
actualVirtualSizeDiff = 0; //max_memory_size - actual_size;
actualVirtualSizeDiffRoundedToMinAlloc = 0; //round_up(actualVirtualSizeDiff, min_block_size);
unusedSpace = max_memory_size - actual_size;
// 4 for current test
unusedBlocksCount = 0; // ceil(unusedSpace / float(min_block_size));
free_list_count = max_memory_log - min_block_log + 1;
free_list_level_limit = free_list_count - 1;
// init free lists
freeLists = (Node**) base_ptr;
for (int i = 0; i < free_list_count; ++i) {
freeLists[i] = ((Node*) base_ptr) + i;
freeLists[i] = nullptr;
}
// TODO: I can probably get the last index of freeList and incr it by sizeof(Node*) [8 bytes]
this->SplitTable = (uint8_t *) (base_ptr) + free_list_count * sizeof(Node *);
this->TableSize = ((unsigned) 1 << (free_list_count - 1)) / 8;
if (this->TableSize == 0 ) {this->TableSize = 1;}
for (int j = 0; j < TableSize; ++j) {
this->SplitTable[j] = 0;
}
// TODO: I can probably get the last index of SplitTable and incr it by sizeof(uint8_t) [1 byte]
this->FreeTable = (uint8_t *) (base_ptr) + (free_list_count * sizeof(Node *)) + (TableSize * sizeof(uint8_t));
for (int k = 0; k < TableSize; ++k) {
this->FreeTable[k] = 0;
}
this->overheadSize = (free_list_count * sizeof(Node *)) + (this->TableSize * sizeof (uint8_t)) + (this->TableSize * sizeof (uint8_t));
this->overhead_blocks_count = ceil(overheadSize / float(min_block_size));
// updates all necessary inner structures with current state
initInnerStructures();
}
// TODO: Unused block logic is broken, therefore DISABLED for now
// TODO: Alignment
void Allocator::initInnerStructures() {
// last level first index => 2^max_level - 1
size_t firstUnusedBlockIndex = (1 << free_list_level_limit) - 1;
size_t tempUnusedBlockIndex = firstUnusedBlockIndex;
// Must add extra unused blocks first
for (int i = 0; i < this->unusedBlocksCount; ++i) {
markParentAsSplit(tempUnusedBlockIndex);
tempUnusedBlockIndex++;
}
lastInnerStructureBlockIndex = getBlockIndexFromAddr(base_ptr + (min_block_size * (overhead_blocks_count - 1)), free_list_level_limit);
// take unused blocks into consideration
lastInnerStructureBlockIndex += unusedBlocksCount;
size_t currentBlockIndex = lastInnerStructureBlockIndex;
size_t currentLevel = free_list_level_limit;
size_t tempIndex = currentBlockIndex;
for (int i = 0; i < this->overhead_blocks_count; ++i) {
markParentAsSplit(tempIndex);
tempIndex--;
}
// while not at root index
while (!isRoot(currentBlockIndex)) {
if (isRightBuddy(currentBlockIndex)) {
// nothing to do in this case
} else if (isLeftBuddy(currentBlockIndex)) {
void *ptr = getPtrFromBlockIndex(currentBlockIndex, currentLevel);
// TODO: This function is not really safe, I can replace with index & level -> ptr
Node *rightBuddy = FindRightBuddyOf(ptr, max_memory_log - currentLevel);
rightBuddy->next = nullptr;
PushNewNode(&this->freeLists[currentLevel], rightBuddy);
flipFreeTableIndexForBlockBuddies(currentBlockIndex);
}
markParentAsSplit(currentBlockIndex);
currentBlockIndex = getParentIndex(currentBlockIndex);
currentLevel--;
}
}
void *Allocator::Allocate(size_t size) {
int i = findBestFitIndex(size);
// practically speaking, we can never allocate as much as our whole managed memory space,
// because we will always have inner structures at lowest level,
// which in turn means we support at max (1 << max_memory_log - 1) sized allocations, therefore we use >=
// in other words, if we get i = 0, we will fail - return nullptr
if (this->max_memory_log - i >= this->max_memory_log) {
return nullptr;
} else if (this->freeLists[i] != nullptr) {
// we have a block with this size in the free list
void * res = Pop(&this->freeLists[i]);
size_t blockIndexOfRes = getBlockIndexFromAddr((uint8_t*)res, i);
markParentAsSplit(blockIndexOfRes);
flipFreeTableIndexForBlockBuddies(blockIndexOfRes);
return res;
} else {
// we need to split a bigger block
void * block = Allocate(1 << (max_memory_log - i + 1));
if (block != nullptr) {
size_t blockIndex = getBlockIndexFromAddr((uint8_t*)block, i);
// with the allocate on top we are getting the bigger chunk
// split and put the extra (right child) to the free list, which is in the current level
size_t buddyIndex = findBuddyIndex(blockIndex);
Node * buddy = (Node *)getPtrFromBlockIndex(buddyIndex, i);
buddy->next = nullptr;
PushNewNode(&this->freeLists[i], (Node*)buddy);
markParentAsSplit(blockIndex);
flipFreeTableIndexForBlockBuddies(blockIndex);
}
return block;
}
}
void Allocator::Free(void *ptr) {
// Very interesting read by Raymond Chen, which suggests that comparisons on pointers is not well defined:
// https://devblogs.microsoft.com/oldnewthing/20170927-00/?p=97095
// TODO: Define custom exception class
// if (((uintptr_t) ptr < (uintptr_t)base_ptr) || ((uintptr_t) ptr > (uintptr_t)base_ptr + actual_size)) {
// throw Exception(BUDDY_FREE_EXCEPTION_MSG);
// }
size_t allocationLevel = findLevelOfAllocatedBlock(ptr);
// can be used for debugging
// size_t allocationSize = 1 << (max_memory_log - allocationLevel);
size_t blockIndex = getBlockIndexFromAddr((uint8_t*)ptr, allocationLevel);
size_t currentLevel = allocationLevel;
size_t currentIndex = blockIndex;
#ifdef DEBUG
if (isInnerStructure(blockIndex) || isNotAllocated(blockIndex, allocationLevel, ptr)) {
return;
}
#endif
// traversing upwards
while (!isRoot(currentIndex)) {
bool isFreeBuddy = isFreeBlockBuddies(currentIndex);
if (!isFreeBuddy) {
// stopping here and will add ourselves to the free lists of our level
// current block will become free therefore we flip
flipFreeTableIndexForBlockBuddies(currentIndex);
break;
}
// we will certainly go 1 level up, so parent will no longer be split
unmarkParentAsSplit(currentIndex);
// current block will become free therefore we flip
flipFreeTableIndexForBlockBuddies(currentIndex);
// our buddy is free, therefore we need to remove it from its free list
// adding to the actual new free list will be done out of the loop
size_t buddyIndex = findBuddyIndex(currentIndex);
Node *buddyNode = (Node*)getPtrFromBlockIndex(buddyIndex, currentLevel);
RemoveNode(&this->freeLists[currentLevel], buddyNode);
currentIndex = (currentIndex - 1) / 2;
currentLevel--;
}
Node* newNode = (Node*)getPtrFromBlockIndex(currentIndex, currentLevel);
newNode->next = nullptr;
PushNewNode(&this->freeLists[currentLevel], newNode);
}
void Allocator::printTree(uint8_t *arr, std::ostream &os, const char *mark) {
std::string serializedTree;
for (int j = 0; j < this->TableSize; ++j) {
uint8_t splitByte = arr[j];
for (int i = 0; i < 8; ++i, splitByte >>= 1) {
if (splitByte & 0x1) {
serializedTree.append("1");
} else {
serializedTree.append("0");
}
}
}
// the string are read from left to right, unlike the bit map
// first char is 0th bit ( 2 ^ 0)
size_t ix = 0;
for (int k = 0; k < this->free_list_level_limit; ++k) {
size_t countOfElementsToBePrinted = 1 << k;
os << (1 << (max_memory_log - k)) << ": \t\t";
for (int i = 0; i < countOfElementsToBePrinted; ++i) {
os << ((serializedTree[ix] == '1') ? mark : "_") << " ";
ix++;
}
os << "\n";
}
os << "\n";
}
void Allocator::Debug(std::ostream &os) {
os << "Debug information: \n\n";
os << "Asked size was: " << this->max_memory_size - this->unusedSpace << std::endl;
os << "Virtual size was: " << this->max_memory_size << std::endl;
os << "Size after inner structures was: " << this->max_memory_size - this->unusedBlocksCount * this->min_block_size - (this->overhead_blocks_count * min_block_size) << std::endl << std::endl;
exposeFreeMemory(os);
}
void Allocator::exposeInnerStructures(std::ostream &os) {
os << "Inner structures details:" << "\n";
uint8_t * addrIter = base_ptr;
os << "Address space range: [" << (void*)addrIter << ";" << (void*)((uint8_t *)addrIter + this->actual_size) << "]" << std::endl;
for (int j = 0; j < this->free_list_count; ++j) {
os << "Free list " << j + 1 << " : " << (void*)addrIter << std::endl;
addrIter += sizeof(Node*);
}
os << "Split table structure details:" << "\n";
for (int j = 0; j < this->TableSize; ++j) {
os << "Split Tables " << j + 1 << " : " << (void*)addrIter << std::endl;
addrIter += sizeof(uint8_t*);
}
os << "Split table status: " << "\n";
printTree(this->SplitTable, os, "S");
os << "Free table structure details:" << "\n";
for (int j = 0; j < this->TableSize; ++j) {
os << "Free Tables " << j + 1 << " : " << (void*)addrIter << std::endl;
addrIter += sizeof(uint8_t*);
}
os << "Free table status: " << "\n";
printTree(this->FreeTable, os, "F");
}
void Allocator::exposeFreeMemory(std::ostream &os) {
size_t totalFreeMemory = 0;
for (int i = 0; i < this->free_list_count; ++i) {
size_t freeBlockCountOnCurrentLevel = GetLength(&freeLists[i]);
os << freeBlockCountOnCurrentLevel << " available block" << (freeBlockCountOnCurrentLevel != 1 ? "s" : "") << " with size " << (1 << (max_memory_log - i)) << "\n";
totalFreeMemory += ((1 << (max_memory_log - i)) * freeBlockCountOnCurrentLevel);
}
// TODO: max_size --> to actual_size if any memory alloc is supported
os << "Free memory size as of now: " << totalFreeMemory << "\n";
os << "Total allocated memory size as of now: " << this->max_memory_size - totalFreeMemory << "\n";
os << "Allocated for inner structures: " << this->overhead_blocks_count * min_block_size << "\n";
os << "Allocated for users: " << (this->max_memory_size - totalFreeMemory) - (this->overhead_blocks_count * min_block_size) - (this->actualVirtualSizeDiffRoundedToMinAlloc - this->actualVirtualSizeDiff) << "\n\n";
}
void Allocator::CheckForLeaks() {
std::cout<<"CHECKING AT EXIT" << std::endl;
}
size_t Allocator::getBlockIndexFromAddr(uint8_t *ptr, size_t level) {
// find offset between base_ptr and input ptr
// divide to find index offset from first block on that level
// add to first index on that level
return ((ptr - base_ptr) >> (max_memory_log - level)) + (1 << level) - 1;
}
size_t Allocator::getParentIndex(size_t index) {
return (index - 1) / 2;
}
uint8_t *Allocator::getPtrFromBlockIndex(size_t index, size_t level) {
// find index offset from first index on that level
// multiply it by block size on that level
// add resulting bytes offset to base_ptr
return base_ptr + ((index - (1 << level) + 1) << (this->max_memory_log - level));
}
bool Allocator::isRoot(size_t index) {
return index == 0;
}
bool Allocator::isLeftBuddy(size_t index) {
return index % 2 == 1;
}
bool Allocator::isRightBuddy(size_t index) {
return index % 2 == 0;
}
Node *Allocator::FindRightBuddyOf(void *pVoid, int i) {
// we have to shift 2^i bytes to the right to find the buddy
return (Node*)(((uint8_t *)pVoid) + (1 << i));
}
size_t Allocator::calculateEnclosingPowerOf2Size(size_t size) {
return (unsigned) 1 << unsigned (floor(log2(size)));
}
size_t Allocator::findBestFitIndex(size_t requested_memory) {
size_t free_list_index = free_list_level_limit;
size_t size = this->min_block_size;
// if we ever get to index = 0, the allocation will result in nullptr
while (size < requested_memory && free_list_index != 0) {
free_list_index--;
// multiply by 2^1
size <<= 1;
}
return free_list_index;
}
void Allocator::markParentAsSplit(size_t index) {
index = (index - 1) / 2;
SplitTable[index / 8] |= (unsigned)1 << (index % 8);
}
void Allocator::unmarkParentAsSplit(size_t index) {
index = (index - 1) / 2;
SplitTable[index / 8] &= ~((unsigned)1 << (index % 8));
}
bool Allocator::isSplitBlockByIndex(size_t index) {
return (SplitTable[index / 8] >> (index % 8)) & 1;
}
bool Allocator::isFreeBlockBuddies(size_t index) {
index = (index - 1) / 2;
return (FreeTable[index / 8] >> (index % 8)) & 1;
}
void Allocator::flipFreeTableIndexForBlockBuddies(size_t blockIndex) {
size_t index = (blockIndex - 1) / 2;
FreeTable[index / 8] ^= (unsigned)1 << (index % 8);
}
bool Allocator::isSplitByAddrAndLevel(void *adr, size_t level) {
size_t blockIndex = getBlockIndexFromAddr((uint8_t *)(adr), level);
return isSplitBlockByIndex(blockIndex);
}
size_t Allocator::findLevelOfAllocatedBlock(void *addr) {
size_t currentLevel = free_list_level_limit;
while (currentLevel > 0) {
// if parent is split, addr is in current level
if (isSplitByAddrAndLevel(addr, currentLevel - 1)) {
return currentLevel;
}
currentLevel--;
}
return 0;
}
size_t Allocator::findBuddyIndex(size_t index) {
// root node
if (index == 0) {
return 0;
}
return ((index - 1) ^ 1) + 1;
}
size_t Allocator::previousPowerOfTwo(size_t num) {
while (num & num - 1) {
num = num & num -1;
}
return num;
}
size_t Allocator::round_up(size_t num, size_t factor) {
return num + factor - 1 - (num + factor - 1) % factor;
}
bool Allocator::isInnerStructure(size_t index) {
return ((this->lastInnerStructureBlockIndex - this->overhead_blocks_count) < index) && (index <= this->lastInnerStructureBlockIndex);
}
bool Allocator::isNotAllocated(size_t index, size_t i, void *pVoid) {
// if parent is not split, there is no way for the current block to be allocated
if (!this->isSplitBlockByIndex((index - 1) / 2)) {
return true;
}
// if this returns 0, then both the current block and its buddy are allocated
if (!isFreeBlockBuddies(index)) {
return false;
} else {
// one of the buddies is free, but is it the current one?
// check if current node is present in free list, if yes -> it is not allocated
return IsNodePresent(&this->freeLists[i], (Node*) pVoid);
}
}
Allocator::~Allocator() {
size_t totalFreeMemory = getCurrentFreeMemory();
size_t allocatedUserMemory = (this->max_memory_size - totalFreeMemory) - (this->overhead_blocks_count * min_block_size) - (this->actualVirtualSizeDiffRoundedToMinAlloc - this->actualVirtualSizeDiff);
if (allocatedUserMemory > 0) {
std::cout << "Destroying Allocator, in spite of memory leak of: " << allocatedUserMemory << " bytes\n\n";
}
}
size_t Allocator::getCurrentFreeMemory() {
size_t totalFreeMemory = 0;
for (int i = 0; i < this->free_list_count; ++i) {
size_t freeBlockCountOnCurrentLevel = GetLength(&freeLists[i]);
totalFreeMemory += ((1 << (max_memory_log - i)) * freeBlockCountOnCurrentLevel);
}
return totalFreeMemory;
}
Exception::Exception(const char *what) : runtime_error(what) {}