From 845a96f5e6bfbbbeba19bf3df07f0e9de53d9a88 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 29 Oct 2025 11:40:49 +0100 Subject: kapi: export frame_allocator interface --- .../x86_64/include/x86_64/memory/region_allocator.hpp | 7 ++++--- arch/x86_64/src/kapi/memory.cpp | 12 ++++++++++++ arch/x86_64/src/memory/region_allocator.cpp | 6 +++--- kapi/include/kapi/memory.hpp | 6 ++++-- kapi/include/kapi/memory/frame_allocator.hpp | 19 +++++++++++++++++++ 5 files changed, 42 insertions(+), 8 deletions(-) create mode 100644 kapi/include/kapi/memory/frame_allocator.hpp diff --git a/arch/x86_64/include/x86_64/memory/region_allocator.hpp b/arch/x86_64/include/x86_64/memory/region_allocator.hpp index a918195..913b0bb 100644 --- a/arch/x86_64/include/x86_64/memory/region_allocator.hpp +++ b/arch/x86_64/include/x86_64/memory/region_allocator.hpp @@ -3,6 +3,7 @@ #include "kapi/memory/address.hpp" #include "kapi/memory/frame.hpp" +#include "kapi/memory/frame_allocator.hpp" #include @@ -15,7 +16,7 @@ namespace teachos::memory::x86_64 * @brief Allocates memory linearly using memory areas read from the multiboot2 information pointer and leaks any * deallocated frames. */ - struct region_allocator + struct region_allocator final : frame_allocator { struct memory_information { @@ -43,7 +44,7 @@ namespace teachos::memory::x86_64 * * @return next free physical frame or nullopt if none was found. */ - auto allocate_frame() -> std::optional; + auto allocate() -> std::optional override; /** * @brief Deallocates a previously allocated physical frame. @@ -54,7 +55,7 @@ namespace teachos::memory::x86_64 * * @param physical_frame Previously allocated physical_frame that should be deallocated. */ - auto deallocate_frame(frame const & physical_frame) -> void; + auto release(frame frame) -> void override; private: /** diff --git a/arch/x86_64/src/kapi/memory.cpp b/arch/x86_64/src/kapi/memory.cpp index 55e6ba9..be47941 100644 --- a/arch/x86_64/src/kapi/memory.cpp +++ b/arch/x86_64/src/kapi/memory.cpp @@ -1,6 +1,7 @@ #include "kapi/memory.hpp" #include "kapi/memory/frame.hpp" +#include "kapi/memory/frame_allocator.hpp" #include "kapi/system.hpp" #include "x86_64/boot/boot.hpp" @@ -21,6 +22,7 @@ namespace teachos::memory namespace { auto constinit is_initialized = std::atomic_flag{}; + auto constinit allocator = static_cast(nullptr); auto create_memory_information() -> region_allocator::memory_information { @@ -34,6 +36,16 @@ namespace teachos::memory }; } // namespace + auto active_allocator() -> frame_allocator & + { + if (!allocator) + { + system::panic("[x86_64] The frame allocator has not been set yet."); + } + + return *allocator; + } + auto init() -> void { if (is_initialized.test_and_set()) diff --git a/arch/x86_64/src/memory/region_allocator.cpp b/arch/x86_64/src/memory/region_allocator.cpp index 91a5d49..11ffce2 100644 --- a/arch/x86_64/src/memory/region_allocator.cpp +++ b/arch/x86_64/src/memory/region_allocator.cpp @@ -51,7 +51,7 @@ namespace teachos::memory::x86_64 } } - auto region_allocator::allocate_frame() -> std::optional + auto region_allocator::allocate() -> std::optional { if (!m_current_region) { @@ -80,8 +80,8 @@ namespace teachos::memory::x86_64 return allocated; } - return allocate_frame(); + return allocate(); } - auto region_allocator::deallocate_frame(frame const &) -> void {} + auto region_allocator::release(frame) -> void {} } // namespace teachos::memory::x86_64 diff --git a/kapi/include/kapi/memory.hpp b/kapi/include/kapi/memory.hpp index 3ad5ac5..3daaa86 100644 --- a/kapi/include/kapi/memory.hpp +++ b/kapi/include/kapi/memory.hpp @@ -1,11 +1,13 @@ #ifndef TEACHOS_KAPI_MEMORY_HPP #define TEACHOS_KAPI_MEMORY_HPP -#include "kapi/memory/address.hpp" // IWYU pragma: export -#include "kapi/memory/frame.hpp" // IWYU pragma: export +#include "kapi/memory/address.hpp" // IWYU pragma: export +#include "kapi/memory/frame.hpp" // IWYU pragma: export +#include "kapi/memory/frame_allocator.hpp" // IWYU pragma: export namespace teachos::memory { + auto active_allocator() -> frame_allocator &; auto init() -> void; } // namespace teachos::memory diff --git a/kapi/include/kapi/memory/frame_allocator.hpp b/kapi/include/kapi/memory/frame_allocator.hpp new file mode 100644 index 0000000..f9393ee --- /dev/null +++ b/kapi/include/kapi/memory/frame_allocator.hpp @@ -0,0 +1,19 @@ +#ifndef TEACHOS_KAPI_MEMORY_FRAME_ALLOCATOR_HPP +#define TEACHOS_KAPI_MEMORY_FRAME_ALLOCATOR_HPP + +#include "kapi/memory/frame.hpp" + +#include + +namespace teachos::memory +{ + + struct frame_allocator + { + virtual auto allocate() -> std::optional = 0; + virtual auto release(frame frame) -> void = 0; + }; + +} // namespace teachos::memory + +#endif // TEACHOS_KAPI_MEMORY_FRAME_ALLOCATOR_HPP \ No newline at end of file -- cgit v1.2.3