aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2025-12-15 16:07:12 +0100
committerFelix Morgner <felix.morgner@ost.ch>2025-12-15 16:07:12 +0100
commite9de1e44a21f4b6d31814ac994b26860d0f6b232 (patch)
treeec4b4a5e275f105d64f61541cbd055b7ab5c0fec
parent40804526a58ddf2cc0df0750550c8dcfa7b7c57c (diff)
downloadteachos-e9de1e44a21f4b6d31814ac994b26860d0f6b232.tar.xz
teachos-e9de1e44a21f4b6d31814ac994b26860d0f6b232.zip
kapi/memory: extract common base of page and frame
-rw-r--r--kapi/include/kapi/memory.hpp1
-rw-r--r--kapi/include/kapi/memory/chunk.hpp104
-rw-r--r--kapi/include/kapi/memory/frame.hpp89
-rw-r--r--kapi/include/kapi/memory/page.hpp69
4 files changed, 134 insertions, 129 deletions
diff --git a/kapi/include/kapi/memory.hpp b/kapi/include/kapi/memory.hpp
index 2f45611..bed8dbc 100644
--- a/kapi/include/kapi/memory.hpp
+++ b/kapi/include/kapi/memory.hpp
@@ -2,6 +2,7 @@
#define TEACHOS_KAPI_MEMORY_HPP
#include "kapi/memory/address.hpp" // IWYU pragma: export
+#include "kapi/memory/chunk.hpp" // IWYU pragma: export
#include "kapi/memory/frame.hpp" // IWYU pragma: export
#include "kapi/memory/frame_allocator.hpp" // IWYU pragma: export
#include "kapi/memory/page.hpp" // IWYU pragma: export
diff --git a/kapi/include/kapi/memory/chunk.hpp b/kapi/include/kapi/memory/chunk.hpp
new file mode 100644
index 0000000..fde2e36
--- /dev/null
+++ b/kapi/include/kapi/memory/chunk.hpp
@@ -0,0 +1,104 @@
+#ifndef TEACHOS_KAPI_MEMORY_CHUNK_HPP
+#define TEACHOS_KAPI_MEMORY_CHUNK_HPP
+
+// IWYU pragma: private, include "kapi/memory.hpp"
+
+#include <compare>
+#include <cstddef>
+
+namespace teachos::memory
+{
+
+ //! @qualifier kernel-defined
+ //! A fixed-size unit of memory, indexed by a number.
+ //!
+ //! @tparam AddressType The type of addresses used to index this chunk
+ //! @tparam Size The size of this chunk.
+ template<typename AddressType, std::size_t Size>
+ struct chunk
+ {
+ //! The type of addresses used to index this chunk
+ using address_type = AddressType;
+
+ //! The size of this chunk
+ constexpr auto static size = Size;
+
+ //! Construct a handle referencing the first chunk of the respective address space.
+ constexpr chunk() noexcept = default;
+
+ //! Construct a handle referencing the chunk of memory with the given number.
+ explicit constexpr chunk(std::size_t number) noexcept
+ : m_number{number}
+ {}
+
+ //! Construct a new chunk handle for the chunk containing the given address.
+ //!
+ //! @param address An address contained by the desired chunk.
+ //! @return A handle to a chunk containing the given address.
+ constexpr auto static containing(address_type address) noexcept -> chunk
+ {
+ return chunk{address.raw() / size};
+ }
+
+ //! Get the start address of the chunk referenced by this handle.
+ //!
+ //! @return The address of the first byte contained by the chunk referenced by this handle.
+ [[nodiscard]] constexpr auto start_address() const noexcept -> address_type
+ {
+ return address_type{m_number * size};
+ }
+
+ //! Get the number of the chunk referenced by this handle.
+ //!
+ //! @return The zero-based number of the chunk referenced by this handle.
+ [[nodiscard]] constexpr auto number() const noexcept -> std::size_t
+ {
+ return m_number;
+ }
+
+ //! Get a handle n chunks after the one referenced by this handle.
+ //!
+ //! @param n The positive offset to this chunk.
+ //! @return A handle referencing the chunk n chunks after the one referenced by this handle.
+ constexpr auto operator+(std::size_t n) const noexcept -> chunk
+ {
+ return chunk{m_number + n};
+ }
+
+ //! Let this handle reference the next chunk after the currently reference one.
+ //!
+ //! @return A handle referencing the same chunk as this handle did before the operation.
+ constexpr auto operator++(int) noexcept -> chunk
+ {
+ auto copy = *this;
+ ++*this;
+ return copy;
+ }
+
+ //! Let this handle reference the next chunk after the currently reference one.
+ //!
+ //! @return A reference to this handle.
+ constexpr auto operator++() noexcept -> chunk &
+ {
+ ++m_number;
+ return *this;
+ }
+
+ //! Check if this chunk handle reference the same chunk as another one.
+ //!
+ //! @param other Another chunk handle.
+ constexpr auto operator==(chunk const & other) const noexcept -> bool = default;
+
+ //! Compare the number of the chunk referenced by this handle to the one reference by another one.
+ //!
+ //! @param other Another chunk handle.
+ constexpr auto operator<=>(chunk const & other) const noexcept -> std::strong_ordering = default;
+
+ private:
+ //! The number of the currently referenced chunk.
+ std::size_t m_number{};
+ };
+
+} // namespace teachos::memory
+
+#endif \ No newline at end of file
diff --git a/kapi/include/kapi/memory/frame.hpp b/kapi/include/kapi/memory/frame.hpp
index c8bcb92..49f3bb0 100644
--- a/kapi/include/kapi/memory/frame.hpp
+++ b/kapi/include/kapi/memory/frame.hpp
@@ -4,93 +4,34 @@
// IWYU pragma: private, include "kapi/memory.hpp"
#include "kapi/memory/address.hpp"
-
-#include <compare>
-#include <cstddef>
+#include "kapi/memory/chunk.hpp"
namespace teachos::memory
{
+ //! @qualifier kernel-defined
//! A handle to a frame of physical memory.
- struct frame
+ //!
+ //! @note Contrary to the address types, this type is modeled using inheritance to support future extensions.
+ struct frame : chunk<physical_address, PLATFORM_FRAME_SIZE>
{
- //! The platform-dependent size of a single frame of memory.
- constexpr auto static size = PLATFORM_FRAME_SIZE;
-
- //! Construct a handle referencing the first frame of physical memory.
- constexpr frame() noexcept = default;
-
- //! Construct a handle referencing the frame of memory with the given frame number.
- explicit constexpr frame(std::size_t number) noexcept
- : m_number{number}
- {}
+ using chunk::chunk;
- //! Construct a new frame handle for the frame containing the given address.
+ //! @copydoc chunk::containing
//!
- //! @param address A physical address contained by the desired frame.
- //! @return A handle to a frame containing the given address.
+ //! @note This factory shadows the base factory to aid in type deduction.
constexpr auto static containing(physical_address address) noexcept -> frame
{
- return frame{address.raw() / size};
- }
-
- //! Get the start address of the frame referenced by this handle.
- //!
- //! @return The address of the first byte contained by the frame referenced by this handle.
- [[nodiscard]] constexpr auto start_address() const noexcept -> physical_address
- {
- return physical_address{m_number * size};
- }
-
- //! Get the number of the frame referenced by this handle.
- //!
- //! @return The zero-based number of the frame referenced by this handle.
- [[nodiscard]] constexpr auto number() const noexcept -> std::size_t
- {
- return m_number;
- }
-
- //! Get a handle n frames after the one referenced by this handle.
- //!
- //! @param n The positive offset to this frame.
- //! @return A handle referencing the frame n frames after the one referenced by this handle.
- constexpr auto operator+(std::size_t n) const noexcept -> frame
- {
- return frame{m_number + n};
- }
-
- //! Let this handle reference the next frame after the currently reference one.
- //!
- //! @return A handle referencing the same frame as this handle did before the operation.
- constexpr auto operator++(int) noexcept -> frame
- {
- auto copy = *this;
- ++*this;
- return copy;
- }
-
- //! Let this handle reference the next frame after the currently reference one.
- //!
- //! @return A reference to this handle.
- constexpr auto operator++() noexcept -> frame &
- {
- ++m_number;
- return *this;
+ return frame{chunk::containing(address)};
}
- //! Check if this frame handle reference the same frame as another one.
+ //! Convert a base chunk into a page.
//!
- //! @param other Another frame handle.
- constexpr auto operator==(frame const & other) const noexcept -> bool = default;
-
- //! Compare the number of the frame referenced by this handle to the one reference by another one.
- //!
- //! @param other Another frame handle.
- constexpr auto operator<=>(frame const & other) const noexcept -> std::strong_ordering = default;
-
- private:
- //! The number of the currently referenced frame.
- std::size_t m_number{};
+ //! This constructor allows for conversion from chunk<physical_address, PLATFORM_FRAME_SIZE> to a frame for
+ //! convenience. It is deliberately not explicit.
+ constexpr frame(chunk other)
+ : chunk{other}
+ {}
};
} // namespace teachos::memory
diff --git a/kapi/include/kapi/memory/page.hpp b/kapi/include/kapi/memory/page.hpp
index 0b7083a..57f4f09 100644
--- a/kapi/include/kapi/memory/page.hpp
+++ b/kapi/include/kapi/memory/page.hpp
@@ -4,75 +4,34 @@
// IWYU pragma: private, include "kapi/memory.hpp"
#include "kapi/memory/address.hpp"
-
-#include <compare>
-#include <cstddef>
+#include "kapi/memory/chunk.hpp"
namespace teachos::memory
{
//! @qualifier kernel-defined
//! A handle to a page of virtual memory.
- struct page
+ //!
+ //! @note Contrary to the address types, this type is modeled using inheritance to support future extensions.
+ struct page : chunk<linear_address, PLATFORM_PAGE_SIZE>
{
- //! The platform-dependent size of a single page of memory.
- constexpr auto static size = PLATFORM_FRAME_SIZE;
-
- //! Construct a handle referencing the first page of virtual memory.
- constexpr page() = default;
+ using chunk::chunk;
- //! Construct a handle referencing the page of virtual memory with the given page number.
- explicit constexpr page(std::size_t number)
- : m_number{number}
- {}
-
- //! Construct a new page handle for the page containing the given address.
+ //! @copydoc chunk::containing
//!
- //! @param address A linear address contained by the desired page.
- //! @return A handle to a page containing the given address.
+ //! @note This factory shadows the base factory to aid in type deduction.
constexpr auto static containing(linear_address address) noexcept -> page
{
- return page{address.raw() / size};
- }
-
- //! Get the start address of the page referenced by this handle.
- //!
- //! @return The address of the first byte contained by the page referenced by this handle.
- [[nodiscard]] constexpr auto start_address() const noexcept -> linear_address
- {
- return linear_address{m_number * size};
- }
-
- //! Get the number of the page referenced by this handle.
- //!
- //! @return The zero-based number of the page referenced by this handle.
- [[nodiscard]] constexpr auto number() const noexcept -> std::size_t
- {
- return m_number;
- }
-
- //! Get a handle n pages after the one referenced by this handle.
- //!
- //! @param n The positive offset to this page.
- //! @return A handle referencing the page n pages after the one referenced by this handle.
- constexpr auto operator+(std::size_t n) const noexcept -> page
- {
- return page{m_number + n};
+ return page{chunk::containing(address)};
}
- //! Check if this page handle reference the same frame as another one.
- //!
- //! @param other Another page handle.
- constexpr auto operator==(page const & other) const noexcept -> bool = default;
-
- //! Compare the number of the page referenced by this handle to the one reference by another one.
+ //! Convert a base chunk into a page.
//!
- //! @param other Another page handle.
- constexpr auto operator<=>(page const & other) const noexcept -> std::strong_ordering = default;
-
- private:
- //! The number of the currently referenced page.
- std::size_t m_number{};
+ //! This constructor allows for conversion from chunk<linear_address, PLATFORM_PAGE_SIZE> to a page for convenience.
+ //! It is deliberately not explicit.
+ constexpr page(chunk other)
+ : chunk{other}
+ {}
};
} // namespace teachos::memory