From 47209f32e5b68f2b1190afd9c409da7dcc369514 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Tue, 23 Dec 2025 20:30:57 +0100 Subject: kapi/memory: implement multi-frame allocation --- kapi/include/kapi/memory.hpp | 9 +++++++++ kapi/include/kapi/memory/frame_allocator.hpp | 24 ++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) (limited to 'kapi') diff --git a/kapi/include/kapi/memory.hpp b/kapi/include/kapi/memory.hpp index da666da..c2e9df8 100644 --- a/kapi/include/kapi/memory.hpp +++ b/kapi/include/kapi/memory.hpp @@ -10,6 +10,7 @@ #include #include +#include namespace teachos::memory { @@ -45,6 +46,14 @@ namespace teachos::memory //! @return An engaged std::optional iff. a frame could be allocated, std::nullopt otherwise. auto allocate_frame() -> std::optional; + //! @qualifier kernel-defined + //! Allocate multiple new frames of physical memory + //! + //! @warning This function will panic if no frame allocator has been registered. + //! + //! @return An engaged std::optional iff. @p count frames could be allocated, std::nullopt otherwise. + auto allocate_many_frames(std::size_t count) -> std::optional>; + //! @qualifier kernel-defined //! Map a page onto a frame. //! diff --git a/kapi/include/kapi/memory/frame_allocator.hpp b/kapi/include/kapi/memory/frame_allocator.hpp index fb6bc0d..8532a45 100644 --- a/kapi/include/kapi/memory/frame_allocator.hpp +++ b/kapi/include/kapi/memory/frame_allocator.hpp @@ -5,7 +5,9 @@ #include "kapi/memory/frame.hpp" +#include #include +#include namespace teachos::memory { @@ -27,12 +29,30 @@ namespace teachos::memory //! Allocate a frame of physical memory. //! //! @return An engaged std::optional iff. a new frame could be allocated, std::nullopt otherwise. - virtual auto allocate() noexcept -> std::optional = 0; + virtual auto allocate() noexcept -> std::optional + { + return allocate_many(1).transform([](auto result) { return result.first; }); + } + + //! Allocate multiple consecutive frames of physical memory. + //! + //! @param count The number of frames to allocate + //! @return an engaged optional iff. a block of consecutive frames could be allocated, std::nullopt otherwise. + virtual auto allocate_many(std::size_t count) noexcept -> std::optional> = 0; //! Release a frame of physical memory. //! //! @param frame A frame of physical memory, previously acquired by a call to the #allocate function. - virtual auto release(frame frame) -> void = 0; + virtual auto release(frame frame) -> void + { + return release_many({frame, 1}); + } + + //! Release a frame of physical memory. + //! + //! @param frame_set A set of frames of physical memory, previously acquired by a call to the #allocate_many + //! function. + virtual auto release_many(std::pair frame_set) -> void = 0; protected: frame_allocator() = default; -- cgit v1.2.3