diff options
| author | Matteo Gmür <matteo.gmuer1@ost.ch> | 2025-02-20 12:14:30 +0000 |
|---|---|---|
| committer | Matteo Gmür <matteo.gmuer1@ost.ch> | 2025-02-20 12:14:30 +0000 |
| commit | 27874721a35fe7ccde843c7ab88ab72e74fe6b42 (patch) | |
| tree | c2156c2ec52e181643ab90ae9ac501c9d83eb96c /arch/x86_64/src/memory/heap | |
| parent | cd502936227e48a36d9e933d26aac2ee29d3bc29 (diff) | |
| download | teachos-27874721a35fe7ccde843c7ab88ab72e74fe6b42.tar.xz teachos-27874721a35fe7ccde843c7ab88ab72e74fe6b42.zip | |
Make allocation type configurable
Diffstat (limited to 'arch/x86_64/src/memory/heap')
| -rw-r--r-- | arch/x86_64/src/memory/heap/global_heap_allocator.cpp | 45 |
1 files changed, 38 insertions, 7 deletions
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 |
