-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFixedSizeAllocator.cpp
More file actions
58 lines (52 loc) · 1.95 KB
/
FixedSizeAllocator.cpp
File metadata and controls
58 lines (52 loc) · 1.95 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
#include "FixedSizeAllocator.h"
#include <iostream>
#include <cinttypes>
size_t FixedSizeAllocator::calcSpaceForManager(const size_t bits, const size_t blockSize)
{
return sizeof(FixedSizeAllocator) + BitArray::calcSpaceForArray(bits) + (blockSize * bits);
}
void* FixedSizeAllocator::_alloc()
{
unsigned long freeIndex = 0;
const bool isFreeFound = bit_array_.findFirstSetBit(freeIndex);
if (!isFreeFound)
{
printf("No free blocks found here!");
return nullptr;
}
bit_array_.set(freeIndex, 0);
return reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(start_) + block_size_ * freeIndex);
}
void FixedSizeAllocator::_free(void* ptr)
{
const size_t index = (reinterpret_cast<uintptr_t>(ptr) - reinterpret_cast<uintptr_t>(start_)) / block_size_;
bit_array_.set(index, 1);
}
bool FixedSizeAllocator::_contains(const void* ptr) const
{
const uintptr_t ptrAddress = reinterpret_cast<uintptr_t>(ptr);
const uintptr_t startAddress = reinterpret_cast<uintptr_t>(start_);
const uintptr_t endAddress = reinterpret_cast<uintptr_t>(this) + heap_size_;
if (ptrAddress > startAddress && ptrAddress < endAddress)
return true;
return false;
}
// PRINT / DEBUG
void FixedSizeAllocator::ShowFreeBlocks(const FixedSizeAllocator* manager)
{
printf("\nPRINTING ALL FREE BLOCKS\n=========================\n");
/*for (unsigned int i = 0; i < manager->_nFreeDesc; i++) {
if (manager->_freeDesc[i].offset != 0) {
printf("Address => [%p] | From => [%d] | Size => [%d]\n", &manager->_freeDesc[i], manager->_freeDesc[i].offset, manager->_freeDesc[i].size);
}
}*/
}
void FixedSizeAllocator::ShowOutstandingAllocations(const FixedSizeAllocator* manager)
{
printf("\nPRINTING ALL ALLOCATED BLOCKS\n=========================\n");
/*for (unsigned int i = 0; i < manager->_nUsedDesc; i++) {
if (manager->_usedDesc[i].offset != 0) {
printf("Address => [%p] | From => [%d] | Size => [%d]\n", &manager->_usedDesc[i], manager->_usedDesc[i].offset, manager->_usedDesc[i].size);
}
}*/
}