diff options
| author | Fabian Imhof <fabian.imhof@ost.ch> | 2025-02-20 16:05:19 +0000 |
|---|---|---|
| committer | Fabian Imhof <fabian.imhof@ost.ch> | 2025-02-20 16:05:19 +0000 |
| commit | 00fd8cf8f72d6c5c3d6150f3ec833ded9e34b2b1 (patch) | |
| tree | 7ba87d65fea944b7799fc4176f1bfd4615d20f69 /arch/x86_64/src/memory | |
| parent | 27874721a35fe7ccde843c7ab88ab72e74fe6b42 (diff) | |
| download | teachos-00fd8cf8f72d6c5c3d6150f3ec833ded9e34b2b1.tar.xz teachos-00fd8cf8f72d6c5c3d6150f3ec833ded9e34b2b1.zip | |
add heap_allocator base class
Diffstat (limited to 'arch/x86_64/src/memory')
| -rw-r--r-- | arch/x86_64/src/memory/heap/global_heap_allocator.cpp | 48 |
1 files changed, 29 insertions, 19 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 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"); } |
