aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatteo Gmür <matteo.gmuer1@ost.ch>2025-02-20 12:14:30 +0000
committerMatteo Gmür <matteo.gmuer1@ost.ch>2025-02-20 12:14:30 +0000
commit27874721a35fe7ccde843c7ab88ab72e74fe6b42 (patch)
treec2156c2ec52e181643ab90ae9ac501c9d83eb96c
parentcd502936227e48a36d9e933d26aac2ee29d3bc29 (diff)
downloadteachos-27874721a35fe7ccde843c7ab88ab72e74fe6b42.tar.xz
teachos-27874721a35fe7ccde843c7ab88ab72e74fe6b42.zip
Make allocation type configurable
-rw-r--r--arch/x86_64/include/arch/memory/heap/global_heap_allocator.hpp13
-rw-r--r--arch/x86_64/src/memory/heap/global_heap_allocator.cpp45
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