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 | |
| parent | 27874721a35fe7ccde843c7ab88ab72e74fe6b42 (diff) | |
| download | teachos-00fd8cf8f72d6c5c3d6150f3ec833ded9e34b2b1.tar.xz teachos-00fd8cf8f72d6c5c3d6150f3ec833ded9e34b2b1.zip | |
add heap_allocator base class
Diffstat (limited to 'arch')
6 files changed, 56 insertions, 25 deletions
diff --git a/arch/x86_64/include/arch/memory/heap/bump_allocator.hpp b/arch/x86_64/include/arch/memory/heap/bump_allocator.hpp index 545b72f..209f8b3 100644 --- a/arch/x86_64/include/arch/memory/heap/bump_allocator.hpp +++ b/arch/x86_64/include/arch/memory/heap/bump_allocator.hpp @@ -1,6 +1,8 @@ #ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_BUMP_ALLOCATOR_HPP #define TEACHOS_ARCH_X86_64_MEMORY_HEAP_BUMP_ALLOCATOR_HPP +#include "arch/memory/heap/heap_allocator.hpp" + #include <atomic> #include <cstdint> @@ -10,7 +12,7 @@ namespace teachos::arch::memory::heap * @brief Simple heap allocator, which allocates linearly and leaks all allocated memory, because it does not really * deallocate anything. */ - struct bump_allocator + struct bump_allocator : heap_allocator { /** * @brief Constructor. 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 a4ece57..e8555df 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 @@ -14,6 +14,8 @@ namespace teachos::arch::memory::heap struct global_heap_allocator { + static auto register_heap_allocator_type(heap_allocator_type new_type) -> void; + static auto allocate(std::size_t size) -> void *; static auto deallocate(void * pointer, std::size_t size) -> void; @@ -21,9 +23,7 @@ namespace teachos::arch::memory::heap private: 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 &; + static auto create_or_get() -> heap_allocator &; }; } // namespace teachos::arch::memory::heap diff --git a/arch/x86_64/include/arch/memory/heap/heap_allocator.hpp b/arch/x86_64/include/arch/memory/heap/heap_allocator.hpp new file mode 100644 index 0000000..9e37bd4 --- /dev/null +++ b/arch/x86_64/include/arch/memory/heap/heap_allocator.hpp @@ -0,0 +1,16 @@ +#ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_HEAP_ALLOCATOR_HPP +#define TEACHOS_ARCH_X86_64_MEMORY_HEAP_HEAP_ALLOCATOR_HPP + +#include <cstdint> + +namespace teachos::arch::memory::heap +{ + struct heap_allocator + { + virtual auto allocate(std::size_t size) -> void * = 0; + + virtual auto deallocate(void * pointer, std::size_t size) -> void = 0; + }; +} // namespace teachos::arch::memory::heap + +#endif // TEACHOS_ARCH_X86_64_MEMORY_HEAP_HEAP_ALLOCATOR_HPP diff --git a/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp b/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp index 99c9218..966e28e 100644 --- a/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp +++ b/arch/x86_64/include/arch/memory/heap/linked_list_allocator.hpp @@ -1,6 +1,7 @@ #ifndef TEACHOS_ARCH_X86_64_MEMORY_HEAP_LINKED_LIST_ALLOCATOR_HPP #define TEACHOS_ARCH_X86_64_MEMORY_HEAP_LINKED_LIST_ALLOCATOR_HPP +#include "arch/memory/heap/heap_allocator.hpp" #include "arch/memory/heap/memory_block.hpp" #include "arch/shared/mutex.hpp" @@ -10,7 +11,7 @@ namespace teachos::arch::memory::heap * @brief Sorted by address list of memory holes (free memory). Uses free holes itself to save the information, * containing the size and pointer to the next hole. Resulting in a singly linked list. */ - struct linked_list_allocator + struct linked_list_allocator : heap_allocator { /** * @brief Constructor. 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"); } |
