fix: Various general fixes and changes

This commit is contained in:
Daniel Hammer
2025-03-07 23:57:20 +01:00
parent 6c5f6ecd47
commit 8cbb8d4472
8 changed files with 25 additions and 42 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
#pragma once
#include <System/Registers.hpp>
#include <Platform/Registers.hpp>
#include <Terminal/terminal.hpp>
void Panic(const char *meditationString, System::Registers registers);
+2 -2
View File
@@ -9,10 +9,10 @@
void* operator new(std::size_t size)
{
return Memory::g_allocator->Request(size);
return Memory::g_heap->Request(size);
}
void operator delete(void* block)
{
Memory::g_allocator->Free(block);
Memory::g_heap->Free(block);
}
+1 -1
View File
@@ -21,7 +21,7 @@ kcp::cstringstream::~cstringstream()
}
kcp::cstringstream& kcp::cstringstream::operator<<(char c) {
this->string = (const char *)Memory::g_allocator->Realloc((void *)this->string, this->size + 1);
this->string = (const char *)Memory::g_heap->Realloc((void *)this->string, this->size + 1);
if (!this->string)
{
+1 -1
View File
@@ -41,7 +41,7 @@ namespace kcp
}
void push_back(T value) {
array = (T *)Memory::g_allocator->Realloc(array, sizeof(T) * (sz + 1));
array = (T *)Memory::g_heap->Realloc(array, sizeof(T) * (sz + 1));
array[sz++] = value;
}
+12 -20
View File
@@ -19,7 +19,7 @@ namespace Memory
#error Sorry, Unimplemented!
#endif
Allocator::Allocator(LargestSection section)
HeapAllocator::HeapAllocator(LargestSection section)
{
g_section = section;
@@ -36,22 +36,21 @@ namespace Memory
current_ref->next = start_block_ptr;
kout << "[Mem] Allocator created for memory section starting at 0x" << base::hex << section.address << Kt::newline;
kout << "[Mem] HeapAllocator object created for memory section starting at 0x" << base::hex << section.address << Kt::newline;
}
int i = 1;
void* Allocator::Request(size_t size, bool phys) {
void* HeapAllocator::Request(size_t size, bool phys) {
Block* current_block_ptr{head.next};
Block* current_prev_ptr{(Block *)&head};
while (true) {
if (current_block_ptr == nullptr) {
kerr << "An allocator was forced to return a Null pointer. (1)" << Kt::newline;
kerr << "A HeapAllocator was forced to return a Null pointer. (1)" << Kt::newline;
return nullptr;
}
if (current_prev_ptr == nullptr) {
kerr << "An allocator was forced to return a Null pointer. (2)" << Kt::newline;
kerr << "An HeapAllocator was forced to return a Null pointer. (2)" << Kt::newline;
return nullptr;
}
@@ -88,21 +87,21 @@ namespace Memory
continue;
} else {
kerr << "An allocator was forced to return a Null pointer. (3)" << Kt::newline;
kerr << "An HeapAllocator was forced to return a Null pointer. (3)" << Kt::newline;
return nullptr;
}
}
if (current_block_ptr != nullptr) {
if (current_block_ptr->next == nullptr) {
kerr << "An allocator was forced to return a Null pointer. (4)" << Kt::newline;
kerr << "An HeapAllocator was forced to return a Null pointer. (4)" << Kt::newline;
return nullptr;
}
current_block_ptr = current_block_ptr->next;
} else
{
kerr << "An allocator was forced to return a Null pointer. (5)" << Kt::newline;
kerr << "An HeapAllocator was forced to return a Null pointer. (5)" << Kt::newline;
return nullptr;
}
}
@@ -110,7 +109,7 @@ namespace Memory
return nullptr;
}
void* Allocator::Realloc(void* ptr, size_t size) {
void* HeapAllocator::Realloc(void* ptr, size_t size) {
void *new_block = this->Request(size);
if (ptr == nullptr) {
@@ -118,7 +117,7 @@ namespace Memory
}
if (new_block == nullptr) {
kerr << "Reallocation failed due to memory starvation" << Kt::newline;
kerr << "HeapAllocator: Reallocation failed due to memory starvation" << Kt::newline;
return nullptr;
}
@@ -131,7 +130,7 @@ namespace Memory
return new_block;
}
void Allocator::Free(void *pagePtr) {
void HeapAllocator::Free(void *pagePtr) {
if (!IsHDDMVirtAddr((uint64_t)pagePtr)) {
pagePtr = (void *)HHDM(pagePtr);
}
@@ -152,16 +151,9 @@ namespace Memory
// Attach the merged block to the structure
head.next->next = fmrNext;
}
void Allocator::Stress() {
// ~410 MiB
for (size_t i = 0; i < 100000; i++) {
kout << "[Mem] Stress allocation " << base::dec << i << ": " << "0x" << base::hex << (uint64_t)this->Request(PAGE_SIZE, false) << Kt::newline;
}
}
// Traverses the Allocator's linked list for debugging
void Allocator::Walk() {
void HeapAllocator::Walk() {
Block* current = {head.next};
size_t i{0};
+3 -5
View File
@@ -2,7 +2,7 @@
#include "Memmap.hpp"
namespace Memory {
class Allocator {
class HeapAllocator {
LargestSection g_section;
struct Block {
@@ -16,14 +16,12 @@ namespace Memory {
Block head;
public:
Allocator(LargestSection section);
HeapAllocator(LargestSection section);
void* Request(size_t size, bool phys = false);
void* Realloc(void* ptr, size_t size);
void Free(void *pagePtr);
void Stress();
void Walk();
};
extern Allocator* g_allocator;
extern HeapAllocator* g_heap;
};
+5 -12
View File
@@ -22,7 +22,7 @@
using namespace Kt;
namespace Memory {
Allocator* g_allocator;
HeapAllocator* g_heap;
uint64_t HHDMBase;
};
@@ -215,14 +215,6 @@ extern "C" void kmain() {
Hal::BridgeLoadGDT();
#endif
// RGB lines
for (std::size_t i = 500; i < 800; i++) {
volatile std::uint32_t *fb_ptr = static_cast<volatile std::uint32_t *>(framebuffer->address);
fb_ptr[i * (framebuffer->pitch / 4) + (i - 5*5)] = 0xFF0000; // Red
fb_ptr[i * (framebuffer->pitch / 4) + (i - 10*5)] = 0x00FF00; // Green
fb_ptr[i * (framebuffer->pitch / 4) + (i - 15*5)] = 0x0000FF; // Blue
}
uint64_t hhdm_offset = hhdm_request.response->offset;
kout << "[Mem] HHDM offset: 0x" << base::hex << hhdm_offset << newline;
@@ -230,8 +222,9 @@ extern "C" void kmain() {
Memory::HHDMBase = hhdm_offset;
if (!system_table_request. response || !system_table_request.response->address) {
kerr << "[Efi] EFI System Table not supported" << newline;
kout << "[Mp] Firmware type: PC BIOS" << newline;
} else {
kout << "[Mp] Firmware type: EFI" << newline;
kout << "[Efi] EFI system table at 0x" << base::hex << (uint64_t)system_table_request.response->address << newline;
}
@@ -239,9 +232,9 @@ extern "C" void kmain() {
kout << "[Mem] Creating Allocator for system conventional memory" << newline;
auto result = Memory::Scan(memmap_request.response);
auto allocator = Memory::Allocator(result);
auto allocator = Memory::HeapAllocator(result);
Memory::g_allocator = &allocator;
Memory::g_heap = &allocator;
} else {
Panic("Guru Meditation Error: System memory map missing!", System::Registers{});
}