From 00fd8cf8f72d6c5c3d6150f3ec833ded9e34b2b1 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Thu, 20 Feb 2025 16:05:19 +0000 Subject: add heap_allocator base class --- arch/x86_64/src/kernel/main.cpp | 4 +- .../src/memory/heap/global_heap_allocator.cpp | 48 +++++++++++++--------- 2 files changed, 32 insertions(+), 20 deletions(-) (limited to 'arch/x86_64/src') diff --git a/arch/x86_64/src/kernel/main.cpp b/arch/x86_64/src/kernel/main.cpp index ed9fc58..7c03644 100644 --- a/arch/x86_64/src/kernel/main.cpp +++ b/arch/x86_64/src/kernel/main.cpp @@ -50,8 +50,10 @@ namespace teachos::arch::kernel video::vga::text::newline(); memory::initialize_memory_management(); - // stack_overflow_test(0); + + memory::heap::global_heap_allocator::register_heap_allocator_type(memory::heap::heap_allocator_type::LINKED_LIST); + heap_test(); } } // namespace teachos::arch::kernel diff --git a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp index d82606e..243f5d8 100644 --- a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp +++ b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp @@ -1,10 +1,10 @@ - #include "arch/memory/heap/global_heap_allocator.hpp" #include "arch/exception_handling/assert.hpp" #include "arch/exception_handling/panic.hpp" #include "arch/memory/heap/bump_allocator.hpp" #include "arch/memory/heap/concept.hpp" +#include "arch/memory/heap/heap_allocator.hpp" #include "arch/memory/heap/linked_list_allocator.hpp" namespace teachos::arch::memory::heap @@ -23,29 +23,33 @@ namespace teachos::arch::memory::heap allocator_type = new_type; } - auto global_heap_allocator::create_or_get() -> linked_list_allocator & + auto global_heap_allocator::create_or_get() -> heap_allocator & { - linked_list_allocator * allocator_ptr = nullptr; + static heap_allocator * allocator_ptr = nullptr; - switch (allocator_type) + if (allocator_ptr == nullptr) { - case heap_allocator_type::NONE: - // Nothing to do. - break; - case heap_allocator_type::BUMP: { - static bump_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE}; - allocator_ptr = &allocator; - break; - } - case heap_allocator_type::LINKED_LIST: { - static linked_list_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE}; - allocator_ptr = &allocator; - break; + switch (allocator_type) + { + case heap_allocator_type::NONE: + // Nothing to do. + break; + case heap_allocator_type::BUMP: { + static bump_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE}; + allocator_ptr = &allocator; + break; + } + case heap_allocator_type::LINKED_LIST: { + static linked_list_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE}; + allocator_ptr = &allocator; + break; + } } + exception_handling::assert(allocator_ptr != nullptr, + "Attempted to allocate or deallocate using the global_heap_allocator before " + "register_heap_allocation_type was called."); } - exception_handling::assert(allocator_ptr != nullptr, - "Attempted to allocate or deallocate using the global_heap_allocator before " - "register_heap_allocation_type was called."); + return *allocator_ptr; } } // namespace teachos::arch::memory::heap @@ -57,6 +61,9 @@ auto operator new(std::size_t size) -> void * auto operator delete(void * pointer) noexcept -> void { + if (pointer == nullptr) + { + } teachos::arch::exception_handling::panic("Called delete operator without passing required size attribute"); } @@ -72,6 +79,9 @@ auto operator new[](std::size_t size) -> void * auto operator delete[](void * pointer) noexcept -> void { + if (pointer == nullptr) + { + } teachos::arch::exception_handling::panic("Called delete[] operator without passing required size attribute"); } -- cgit v1.2.3