aboutsummaryrefslogtreecommitdiff
path: root/kapi/include
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-03-18 17:18:37 +0100
committerFelix Morgner <felix.morgner@ost.ch>2026-03-18 17:18:37 +0100
commite7ccb96aecae7b231fb05818d7e45a767aebc31d (patch)
tree68f7a623018d025b3fb6d10ce49d022242cc14f2 /kapi/include
parent12c0586ee15cadfa178e6982dc0f76b047cb2df9 (diff)
downloadteachos-e7ccb96aecae7b231fb05818d7e45a767aebc31d.tar.xz
teachos-e7ccb96aecae7b231fb05818d7e45a767aebc31d.zip
kstd: introduce strong type for memory amounts
Diffstat (limited to 'kapi/include')
-rw-r--r--kapi/include/kapi/memory/address.hpp11
-rw-r--r--kapi/include/kapi/memory/chunk.hpp8
-rw-r--r--kapi/include/kapi/memory/layout.hpp6
3 files changed, 20 insertions, 5 deletions
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 <kstd/format>
+#include <kstd/units>
#include <bit>
#include <compare>
@@ -183,6 +184,16 @@ namespace kapi::memory
return static_cast<MaskType>(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 <kstd/units>
+
#include <compare>
#include <cstddef>
@@ -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<typename ChunkType, typename AddressType, std::size_t Size>
+ template<typename ChunkType, typename AddressType, kstd::units::bytes Size>
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 <kstd/units>
+
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.
//!