fix(heap): Fix HeapAllocator class

This commit is contained in:
Daniel Hammer
2025-04-15 20:45:34 +02:00
parent 2e45efe855
commit 35cf3f7c80
5 changed files with 20 additions and 17 deletions
+11 -11
View File
@@ -32,7 +32,7 @@ namespace Memory
void HeapAllocator::InsertToFreelist(void* ptr, std::size_t size) {
auto prev_next = head.next;
head.next = (Node*)ptr;
head.next->next = prev_next;
head.next->size = size;
@@ -47,13 +47,10 @@ namespace Memory
size_t size = 0x1000 * n;
InsertToFreelist(ptr, size);
kout << "HeapAllocator: expanded heap by " << base::dec << n << " pages" << Kt::newline;
}
}
HeapAllocator::HeapAllocator()
{
kout << "HeapAllocator: constructor called" << Kt::newline;
InsertPagesToFreelist(8);
}
@@ -70,6 +67,8 @@ namespace Memory
prev->next = current->next;
Header* header = (Header*)current;
header->magic = headerMagic;
header->size = size;
void* block = (void*)((uintptr_t)header + sizeof(Header));
@@ -109,15 +108,16 @@ namespace Memory
void HeapAllocator::Free(void* ptr) {
Header* header = GetHeader(ptr);
auto size = header->size;
if (header->magic != headerMagic) {
Panic("Bad magic in HeapAllocator header", nullptr);
return;
}
auto actualSize = size + sizeof(Header);
void* actualBlock = (void*)header;
auto prev_next = head.next;
// Relink the full node back into the list
head.next = (Node*)actualBlock;
head.next->size = actualSize;
head.next->next = prev_next;
InsertToFreelist(actualBlock, size);
}
// Traverses the Allocator's linked list for debugging
+4 -1
View File
@@ -3,14 +3,17 @@
namespace Memory {
class HeapAllocator {
static constexpr std::size_t headerMagic = 0x6CEF9AB4;
struct Node {
size_t size;
Node* next;
};
struct Header {
std::size_t magic;
std::size_t size;
}__attribute__((packed));
}__attribute__((packed)) ;
Node head{};