From 00fd8cf8f72d6c5c3d6150f3ec833ded9e34b2b1 Mon Sep 17 00:00:00 2001 From: Fabian Imhof Date: Thu, 20 Feb 2025 16:05:19 +0000 Subject: add heap_allocator base class --- arch/x86_64/include/arch/memory/heap/bump_allocator.hpp | 4 +++- .../include/arch/memory/heap/global_heap_allocator.hpp | 6 +++--- arch/x86_64/include/arch/memory/heap/heap_allocator.hpp | 16 ++++++++++++++++ .../include/arch/memory/heap/linked_list_allocator.hpp | 3 ++- 4 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 arch/x86_64/include/arch/memory/heap/heap_allocator.hpp (limited to 'arch/x86_64/include') 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 #include @@ -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 + +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. -- cgit v1.2.3