/
pageFrameAllocator.cpp
155 lines (129 loc) · 4.74 KB
/
pageFrameAllocator.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
#include "pageFrameAllocator.hpp"
#define PAGESIZE 4096 // 4KBs
fBlock *head = nullptr;
fBlock *freeBlock = head;
size_t totalMemory;
size_t usedMemory = 0;
static bootInfo bootInformation{};
uintptr_t allocateFrame(size_t requestSize){
if (head == nullptr){
head = initFreeList();
freeBlock = head;
}
bootInformation = getBootInfo();
size_t freeMemorySize = totalMemory = bootInformation.memory.freeMemSize;
size_t roundedRequestSize = roundUpToPageBoundary(requestSize);
fBlock *selectedBlock = nullptr;
if (roundedRequestSize > freeMemorySize){
e9_printf("ERROR!!");
asm volatile("hlt");
}
// best-fit algorithm to search for the appropriate freeBlock
if (!freeBlock->next && freeBlock->size >= roundedRequestSize) {
selectedBlock = freeBlock;
} else {
// Traverse through the linked list
while (freeBlock->next) {
if (freeBlock->size >= roundedRequestSize) {
if (!selectedBlock || selectedBlock->size > freeBlock->size) {
selectedBlock = freeBlock;
}
}
freeBlock = freeBlock->next;
}
}
freeBlock = head;
if (selectedBlock == nullptr){
// coalescing setup (merge blocks)
e9_printf("coalescing setup");
while(freeBlock->next){
if (freeBlock->prevSize != 0){
if ((freeBlock->prevSize + freeBlock->size) >= roundedRequestSize){
if (selectedBlock != nullptr){
if ((selectedBlock->size + selectedBlock->prevSize) > (freeBlock->size + freeBlock->prevSize)){
selectedBlock = freeBlock;
}
}
selectedBlock = freeBlock;
}
}
freeBlock = freeBlock->next;
}
freeBlock = head;
if (selectedBlock == nullptr){
e9_printf("coalescing halt");
// implement disk paging
asm volatile ("hlt");
}
// actual coalescing
fBlock* prevBlock = selectedBlock->previous;
prevBlock->size += selectedBlock->size;
prevBlock->next = selectedBlock->next;
selectedBlock = prevBlock;
}
selectedBlock->inUse = true;
auto* newBlock = (fBlock*)((char*)(selectedBlock) + roundedRequestSize);
newBlock->size = (selectedBlock->size - (roundedRequestSize));
newBlock->inUse = false;
newBlock->next = selectedBlock->next;
newBlock->prevSize = 0;
head = freeBlock = newBlock;
bootInformation.memory.freeMemSize = bootInformation.memory.freeMemSize - roundedRequestSize;
setBootInfo(bootInformation);
usedMemory += roundedRequestSize;
memoryset(selectedBlock, 0, roundedRequestSize);
insert(&addressSizeHT, (void*)selectedBlock, roundedRequestSize);
return toPhysicalAddr(selectedBlock);
}
void freeFrame(void* allocatedFrame){
allocatedFrame = toVirtualAddr(allocatedFrame);
int allocatedFrameSize = search(&addressSizeHT, (allocatedFrame));
if (allocatedFrameSize == -1){
e9_printf("ERROR: Double Free / Wrong Memory Freeing attempt. Blocked.");
asm volatile("hlt");
}
fBlock* freedBlock = (fBlock*)((allocatedFrame));
freedBlock->size = allocatedFrameSize;
freedBlock->inUse = false;
if (freedBlock == head) {
head = freeBlock = freedBlock;
} else {
freedBlock->next = head;
head->previous = freedBlock;
head = freedBlock;
}
deleteKey(&addressSizeHT, allocatedFrame);
usedMemory -= allocatedFrameSize;
if (bootInformation.memory.freeMemSize != 0){
bootInformation.memory.freeMemSize += allocatedFrameSize;
setBootInfo(bootInformation);
}
}
size_t roundUpToPageBoundary(size_t size){
if ((size % PAGESIZE) == 0){
return size;
}
return (size + PAGESIZE - 1) & ~(PAGESIZE - 1);
}
uint64_t roundDown(uint64_t n, uint64_t alignTo){
return n & ~(alignTo - 1);
}
uint64_t roundUp(uint64_t n, uint64_t alignTo) {
return (n + alignTo - 1) & ~(alignTo - 1);
}
uintptr_t toPhysicalAddr(void *addr){
if (bootInformation.memory.hhdmOffset == 0){
bootInformation = getBootInfo();
}
return (uintptr_t)((uintptr_t)addr - bootInformation.memory.hhdmOffset);
}
void *toVirtualAddr(void *addr){
// getNextLevelPointer failed
if ((uintptr_t)addr == (uintptr_t)-1) {
return nullptr;
}
if (bootInformation.memory.hhdmOffset == 0){
bootInformation = getBootInfo();
}
return (void*)((uintptr_t)addr + bootInformation.memory.hhdmOffset);
}