From 4380590af239a44fbde17f3df9afbdcb73e0000e Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Thu, 18 May 2023 11:29:44 -0700 Subject: [PATCH] Add new and delete operator implementations to the kernel heap. For now delete does nothing. --- zion/CMakeLists.txt | 4 +++- zion/memory/kernel_heap.cpp | 20 +++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/zion/CMakeLists.txt b/zion/CMakeLists.txt index 2f858cb..5bd066e 100644 --- a/zion/CMakeLists.txt +++ b/zion/CMakeLists.txt @@ -17,12 +17,14 @@ target_include_directories(zion # -c -- Don't run the linker (only necessary for the assembler) # -ffreestanding +# -fno-rtti -- No runtime type information (might mess with polymorphism?) +# -fno-exceptions -- Disable exceptions. # -nostdlib -- Don't include the standard library. # -mabi=sysv -- Explicitly specify the ABI since we will rely on it. # -mno-red-zone -- Don't put data below the stack pointer (clobbered by interrupts). # -mcmodel=kernel -- Assume the kernel code is running in the higher half. # -mgeneral-regs-only -- Prevent GCC from using a whole host of nonsense registers (that we have to enable). -set(_Z_COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -c -ffreestanding -nostdlib -mabi=sysv -mno-red-zone -mcmodel=kernel -mgeneral-regs-only") +set(_Z_COMPILE_FLAGS "${CMAKE_CXX_FLAGS} -c -ffreestanding -fno-rtti -fno-exceptions -nostdlib -mabi=sysv -mno-red-zone -mcmodel=kernel -mgeneral-regs-only") set(_Z_LINK_SCRIPT "${CMAKE_CURRENT_SOURCE_DIR}/linker.ld") diff --git a/zion/memory/kernel_heap.cpp b/zion/memory/kernel_heap.cpp index 311aae1..d318fea 100644 --- a/zion/memory/kernel_heap.cpp +++ b/zion/memory/kernel_heap.cpp @@ -3,8 +3,22 @@ #include "debug/debug.h" #include "memory/paging_util.h" +namespace { + +static KernelHeap* gKernelHeap = nullptr; + +KernelHeap& GetKernelHeap() { + if (!gKernelHeap) { + panic("Kernel Heap not initialized."); + } + return *gKernelHeap; +} +} // namespace + KernelHeap::KernelHeap(uint64_t lower_bound, uint64_t upper_bound) - : next_addr_(lower_bound), upper_bound_(upper_bound) {} + : next_addr_(lower_bound), upper_bound_(upper_bound) { + gKernelHeap = this; +} void* KernelHeap::Allocate(uint64_t size) { if (next_addr_ + size >= upper_bound_) { @@ -15,3 +29,7 @@ void* KernelHeap::Allocate(uint64_t size) { next_addr_ += size; return reinterpret_cast(address); } + +void* operator new(uint64_t size) { return GetKernelHeap().Allocate(size); } + +void operator delete(void*, uint64_t) {}