fix(heap): Fix HeapAllocator class
This commit is contained in:
@@ -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)));
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
@@ -39,6 +39,6 @@ namespace kcp {
|
|||||||
|
|
||||||
cstringstream& operator<<(base nb);
|
cstringstream& operator<<(base nb);
|
||||||
|
|
||||||
const char* cstr();
|
const char* c_str();
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -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{};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user