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
+1 -1
View File
@@ -17,7 +17,7 @@ namespace kcp {
do { do {
if (*str == delimiter || *str == '\0') { if (*str == delimiter || *str == '\0') {
auto cstr = current_stream.cstr(); auto cstr = current_stream.c_str();
auto cstr_len = Lib::strlen(cstr); auto cstr_len = Lib::strlen(cstr);
result.push_back((const char*)Memory::g_heap->Request(sizeof(char) * (cstr_len + 1))); result.push_back((const char*)Memory::g_heap->Request(sizeof(char) * (cstr_len + 1)));
+3 -3
View File
@@ -23,9 +23,9 @@ kcp::cstringstream::~cstringstream()
kcp::cstringstream& kcp::cstringstream::operator<<(char c) { kcp::cstringstream& kcp::cstringstream::operator<<(char c) {
this->string = (char *)Memory::g_heap->Realloc((void *)this->string, this->size + 2); this->string = (char *)Memory::g_heap->Realloc((void *)this->string, this->size + 2);
if (!this->string) if (this->string == nullptr)
{ {
kerr << "Streaming failed due to failed allocation" << Kt::newline; kerr << "kcp::cstringstream: Streaming failed due to failed allocation" << Kt::newline;
return *this; return *this;
} }
@@ -85,6 +85,6 @@ kcp::cstringstream& kcp::cstringstream::operator<<(base nb)
return *this; return *this;
} }
const char* kcp::cstringstream::cstr() { const char* kcp::cstringstream::c_str() {
return this->string; return this->string;
} }
+1 -1
View File
@@ -39,6 +39,6 @@ namespace kcp {
cstringstream& operator<<(base nb); cstringstream& operator<<(base nb);
const char* cstr(); const char* c_str();
}; };
}; };
+11 -11
View File
@@ -32,7 +32,7 @@ namespace Memory
void HeapAllocator::InsertToFreelist(void* ptr, std::size_t size) { void HeapAllocator::InsertToFreelist(void* ptr, std::size_t size) {
auto prev_next = head.next; auto prev_next = head.next;
head.next = (Node*)ptr; head.next = (Node*)ptr;
head.next->next = prev_next; head.next->next = prev_next;
head.next->size = size; head.next->size = size;
@@ -47,13 +47,10 @@ namespace Memory
size_t size = 0x1000 * n; size_t size = 0x1000 * n;
InsertToFreelist(ptr, size); InsertToFreelist(ptr, size);
}
kout << "HeapAllocator: expanded heap by " << base::dec << n << " pages" << Kt::newline;
}
HeapAllocator::HeapAllocator() HeapAllocator::HeapAllocator()
{ {
kout << "HeapAllocator: constructor called" << Kt::newline;
InsertPagesToFreelist(8); InsertPagesToFreelist(8);
} }
@@ -70,6 +67,8 @@ namespace Memory
prev->next = current->next; prev->next = current->next;
Header* header = (Header*)current; Header* header = (Header*)current;
header->magic = headerMagic;
header->size = size; header->size = size;
void* block = (void*)((uintptr_t)header + sizeof(Header)); void* block = (void*)((uintptr_t)header + sizeof(Header));
@@ -109,15 +108,16 @@ namespace Memory
void HeapAllocator::Free(void* ptr) { void HeapAllocator::Free(void* ptr) {
Header* header = GetHeader(ptr); Header* header = GetHeader(ptr);
auto size = header->size; auto size = header->size;
if (header->magic != headerMagic) {
Panic("Bad magic in HeapAllocator header", nullptr);
return;
}
auto actualSize = size + sizeof(Header); auto actualSize = size + sizeof(Header);
void* actualBlock = (void*)header; void* actualBlock = (void*)header;
auto prev_next = head.next; InsertToFreelist(actualBlock, size);
// Relink the full node back into the list
head.next = (Node*)actualBlock;
head.next->size = actualSize;
head.next->next = prev_next;
} }
// Traverses the Allocator's linked list for debugging // Traverses the Allocator's linked list for debugging
+4 -1
View File
@@ -3,14 +3,17 @@
namespace Memory { namespace Memory {
class HeapAllocator { class HeapAllocator {
static constexpr std::size_t headerMagic = 0x6CEF9AB4;
struct Node { struct Node {
size_t size; size_t size;
Node* next; Node* next;
}; };
struct Header { struct Header {
std::size_t magic;
std::size_t size; std::size_t size;
}__attribute__((packed)); }__attribute__((packed)) ;
Node head{}; Node head{};