From e7ccb96aecae7b231fb05818d7e45a767aebc31d Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 18 Mar 2026 17:18:37 +0100 Subject: kstd: introduce strong type for memory amounts --- kapi/include/kapi/memory/address.hpp | 11 +++++++++++ kapi/include/kapi/memory/chunk.hpp | 8 +++++--- kapi/include/kapi/memory/layout.hpp | 6 ++++-- 3 files changed, 20 insertions(+), 5 deletions(-) (limited to 'kapi') diff --git a/kapi/include/kapi/memory/address.hpp b/kapi/include/kapi/memory/address.hpp index 69fc7b9..587aeac 100644 --- a/kapi/include/kapi/memory/address.hpp +++ b/kapi/include/kapi/memory/address.hpp @@ -4,6 +4,7 @@ // IWYU pragma: private, include "kapi/memory.hpp" #include +#include #include #include @@ -183,6 +184,16 @@ namespace kapi::memory return static_cast(m_value & mask); } + constexpr auto operator+(kstd::units::bytes n) const noexcept -> address + { + return address{m_value + n.value}; + } + + constexpr auto operator+=(kstd::units::bytes n) noexcept -> address & + { + return *this = *this + n; + } + //! Check if this address is equal to another one. constexpr auto operator==(address const &) const noexcept -> bool = default; diff --git a/kapi/include/kapi/memory/chunk.hpp b/kapi/include/kapi/memory/chunk.hpp index 4529535..a046221 100644 --- a/kapi/include/kapi/memory/chunk.hpp +++ b/kapi/include/kapi/memory/chunk.hpp @@ -3,6 +3,8 @@ // IWYU pragma: private, include "kapi/memory.hpp" +#include + #include #include @@ -15,7 +17,7 @@ namespace kapi::memory //! @tparam ChunkType The CRTP type of the deriving class //! @tparam AddressType The type of addresses used to index this chunk //! @tparam Size The size of this chunk. - template + template struct chunk { //! The type of addresses used to index this chunk @@ -30,7 +32,7 @@ namespace kapi::memory //! @return A handle to a chunk containing the given address. constexpr auto static containing(address_type address) noexcept -> ChunkType { - return ChunkType{address / size}; + return ChunkType{address / size.value}; } //! Get the start address of the chunk referenced by this handle. @@ -38,7 +40,7 @@ namespace kapi::memory //! @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}; + return address_type{(m_number * size).value}; } //! Get the number of the chunk referenced by this handle. diff --git a/kapi/include/kapi/memory/layout.hpp b/kapi/include/kapi/memory/layout.hpp index d4153d8..157f41e 100644 --- a/kapi/include/kapi/memory/layout.hpp +++ b/kapi/include/kapi/memory/layout.hpp @@ -5,18 +5,20 @@ #include "kapi/memory/address.hpp" +#include + namespace kapi::memory { //! The size of a single page of virtual memory. //! //! Platforms that use different sizes of pages are expected to emulate 4 KiB pages towards the kernel. - constexpr auto page_size = 4096uz; + constexpr auto page_size = kstd::units::KiB(4); //! The size of a single frame of physical memory. //! //! Platforms that use different sizes of frames are expected to emulate 4 KiB pages towards the kernel. - constexpr auto frame_size = 4096uz; + constexpr auto frame_size = kstd::units::KiB(4); //! The linear base address of the higher-half direct map. //! -- cgit v1.2.3