From 27874721a35fe7ccde843c7ab88ab72e74fe6b42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matteo=20Gm=C3=BCr?= Date: Thu, 20 Feb 2025 12:14:30 +0000 Subject: Make allocation type configurable --- .../arch/memory/heap/global_heap_allocator.hpp | 13 ++++++- .../src/memory/heap/global_heap_allocator.cpp | 45 ++++++++++++++++++---- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp b/arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp index f391936..a4ece57 100644 --- a/arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp +++ b/arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp @@ -5,6 +5,13 @@ namespace teachos::arch::memory::heap { + enum class heap_allocator_type : uint8_t + { + NONE, + BUMP, + LINKED_LIST + }; + struct global_heap_allocator { static auto allocate(std::size_t size) -> void *; @@ -12,7 +19,11 @@ namespace teachos::arch::memory::heap static auto deallocate(void * pointer, std::size_t size) -> void; private: - static auto get_underlying_allocator() -> linked_list_allocator &; + static heap_allocator_type allocator_type; + + static auto register_heap_allocator_type(heap_allocator_type new_type) -> void; + + static auto create_or_get() -> linked_list_allocator &; }; } // namespace teachos::arch::memory::heap 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 4e3a274..d82606e 100644 --- a/arch/x86_64/src/memory/heap/global_heap_allocator.cpp +++ b/arch/x86_64/src/memory/heap/global_heap_allocator.cpp @@ -1,21 +1,52 @@ #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/linked_list_allocator.hpp" namespace teachos::arch::memory::heap { - auto global_heap_allocator::allocate(std::size_t size) -> void * { return get_underlying_allocator().allocate(size); } + heap_allocator_type global_heap_allocator::allocator_type = heap_allocator_type::NONE; + + auto global_heap_allocator::allocate(std::size_t size) -> void * { return create_or_get().allocate(size); } auto global_heap_allocator::deallocate(void * pointer, std::size_t size) -> void { - get_underlying_allocator().deallocate(pointer, size); + create_or_get().deallocate(pointer, size); } - auto global_heap_allocator::get_underlying_allocator() -> linked_list_allocator & + auto global_heap_allocator::register_heap_allocator_type(heap_allocator_type new_type) -> void { - static linked_list_allocator allocator{HEAP_START, HEAP_START + HEAP_SIZE}; - return allocator; + allocator_type = new_type; + } + + auto global_heap_allocator::create_or_get() -> linked_list_allocator & + { + linked_list_allocator * allocator_ptr = nullptr; + + 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."); + return *allocator_ptr; } } // namespace teachos::arch::memory::heap @@ -26,7 +57,7 @@ auto operator new(std::size_t size) -> void * auto operator delete(void * pointer) noexcept -> void { - teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer, 64); + teachos::arch::exception_handling::panic("Called delete operator without passing required size attribute"); } auto operator delete(void * pointer, std::size_t size) noexcept -> void @@ -41,7 +72,7 @@ auto operator new[](std::size_t size) -> void * auto operator delete[](void * pointer) noexcept -> void { - teachos::arch::memory::heap::global_heap_allocator::deallocate(pointer, 64); + teachos::arch::exception_handling::panic("Called delete[] operator without passing required size attribute"); } auto operator delete[](void * pointer, std::size_t size) noexcept -> void -- cgit v1.2.3