-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
78 lines (64 loc) · 2.11 KB
/
Copy pathMain.cpp
File metadata and controls
78 lines (64 loc) · 2.11 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
#include "memoryallocator.h"
#include <cstring> // For memset
#include <climits> // For UINT_MAX
MemoryAllocator::MemoryAllocator(size_t size) : totalSize(size) {
memoryPool = new char[size];
head = reinterpret_cast<Block*>(memoryPool);
head->size = size - sizeof(Block);
head->free = true;
head->next = nullptr;
}
MemoryAllocator::~MemoryAllocator() {
delete[] memoryPool;
}
void* MemoryAllocator::allocate(size_t size) {
Block* bestFit = findBestFitBlock(size);
if (!bestFit) {
return nullptr; // No suitable block found
}
if (bestFit->size > size + sizeof(Block)) {
splitBlock(bestFit, size);
}
bestFit->free = false;
return reinterpret_cast<void*>(reinterpret_cast<char*>(bestFit) + sizeof(Block));
}
void MemoryAllocator::deallocate(void* ptr) {
if (!ptr) return; // account for if pointer does not exist
Block* block = reinterpret_cast<Block*>(reinterpret_cast<char*>(ptr) - sizeof(Block));
block->free = true;
mergeFreeBlocks();
}
MemoryAllocator::Block* MemoryAllocator::findBestFitBlock(size_t size) {
Block* bestFit = nullptr;
Block* current = head;
size_t minSize = UINT_MAX;
while (current) {
if (current->free && current->size >= size) {
if (current->size < minSize) {
minSize = current->size;
bestFit = current;
}
}
current = current->next;
}
return bestFit;
}
void MemoryAllocator::splitBlock(Block* block, size_t size) {
Block* newBlock = reinterpret_cast<Block*>(reinterpret_cast<char*>(block) + sizeof(Block) + size);
newBlock->size = block->size - size - sizeof(Block);
newBlock->free = true;
newBlock->next = block->next;
block->size = size;
block->next = newBlock;
}
void MemoryAllocator::mergeFreeBlocks() {
Block* current = head;
while (current && current->next) {
if (current->free && current->next->free) {
current->size += sizeof(Block) + current->next->size;
current->next = current->next->next;
} else {
current = current->next;
}
}
}