aboutsummaryrefslogtreecommitdiff
path: root/kapi
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-03-16 10:38:54 +0100
committerFelix Morgner <felix.morgner@ost.ch>2026-03-16 10:38:54 +0100
commit80ae75bf039820ecb332ae1ab86ef6ce4e2675e4 (patch)
tree1bb344657e2516f175c7ce0045f491c8ff811de1 /kapi
parentd9a653e66d3e7ce3a93219626b281d727e51e2a9 (diff)
downloadteachos-80ae75bf039820ecb332ae1ab86ef6ce4e2675e4.tar.xz
teachos-80ae75bf039820ecb332ae1ab86ef6ce4e2675e4.zip
kapi/memory: support additional address arithmetic
Diffstat (limited to 'kapi')
-rw-r--r--kapi/include/kapi/memory/address.hpp104
-rw-r--r--kapi/include/kapi/memory/chunk.hpp2
2 files changed, 98 insertions, 8 deletions
diff --git a/kapi/include/kapi/memory/address.hpp b/kapi/include/kapi/memory/address.hpp
index e5cf50b..3bef358 100644
--- a/kapi/include/kapi/memory/address.hpp
+++ b/kapi/include/kapi/memory/address.hpp
@@ -50,30 +50,120 @@ namespace kapi::memory
{}
//! Convert this address into a C++ pointer.
- explicit operator std::byte *() const noexcept
+ //!
+ //! @tparam T The type of the object this address should refer to.
+ //! @return This address as a typed pointer to the given type.
+ template<typename ObjectType>
+ explicit operator ObjectType *() const noexcept
{
- return std::bit_cast<std::byte *>(m_value);
+ return std::bit_cast<ObjectType *>(m_value);
}
//! Create a new address n beyond this one.
//!
- //! @param n The amount to increase the address by.
+ //! @param n The amount to add to this address.
//! @return A new address, n further than this one.
- constexpr auto operator+(std::uintptr_t n) const noexcept -> address
+ [[nodiscard]] constexpr auto operator+(std::ptrdiff_t n) const noexcept -> address
{
return address{m_value + n};
}
- //! Increase this address by a given amount.
+ //! Increment this address by a given amount.
//!
- //! @param n The amount to increase the address by.
+ //! @param n The amount to Increment the address by.
//! @return A reference to this address.
- constexpr auto operator+=(std::uintptr_t n) noexcept -> address &
+ constexpr auto operator+=(std::ptrdiff_t n) noexcept -> address &
{
m_value += n;
return *this;
}
+ //! Increment this address by one.
+ //!
+ //! @return A reference to this address.
+ constexpr auto operator++() noexcept -> address &
+ {
+ return (*this += 1);
+ }
+
+ //! Increment this address by one.
+ //!
+ //! @return A copy of this address before the increment.
+ constexpr auto operator++(int) noexcept -> address
+ {
+ auto copy = *this;
+ ++*this;
+ return copy;
+ }
+
+ //! Create a new address n bytes before this one
+ //!
+ //! @param n The amount to subtract from this address
+ //! @return A nre address, @p n ahead of this one
+ [[nodiscard]] constexpr auto operator-(std::ptrdiff_t n) noexcept -> address
+ {
+ return address{m_value - n};
+ }
+
+ //! Decrement this address by a given amount.
+ //!
+ //! @param n The amount to Decrement the address by.
+ //! @return A reference to this address.
+ constexpr auto operator-=(std::ptrdiff_t n) noexcept -> address &
+ {
+ m_value -= n;
+ return *this;
+ }
+
+ //! Decrement this address by one.
+ //!
+ //! @return A reference to this address.
+ constexpr auto operator--() noexcept -> address &
+ {
+ return (*this -= 1);
+ }
+
+ //! Decrement this address by one.
+ //!
+ //! @return A copy of this address before the decrement.
+ constexpr auto operator--(int) noexcept -> address
+ {
+ auto copy = *this;
+ --*this;
+ return copy;
+ }
+
+ //! Calculate the distance between this address and another one
+ //!
+ //! @param other The address to calculate the distance to.
+ //! @return The distance between this address and the given one.
+ [[nodiscard]] constexpr auto operator-(address const & other) noexcept -> std::ptrdiff_t
+ {
+ return m_value - other.m_value;
+ }
+
+ //! Extract the lower bits of the address
+ //!
+ //! @note The only meaningful values for @p n are powers of two.
+ //!
+ //! @param n The divisor to use for extraction.
+ //! @return The lower bits of the address as defined by the divisor.
+ constexpr auto operator%(std::size_t n) const noexcept -> std::uintptr_t
+ {
+ return m_value % n;
+ }
+
+ //! Extract the upper bits of the address
+ //!
+ //! @note The only meaningful values for @p n are powers of two.
+ //!
+ //! @param n The divisor to use for extraction.
+ //! @return The upper bits of the address as defined by the divisor.
+ constexpr auto operator/(std::size_t n) const noexcept -> std::uintptr_t
+ {
+ return m_value / n;
+ }
+
//! Shift this address n bits to the right.
//!
//! @param n The width of the shift.
diff --git a/kapi/include/kapi/memory/chunk.hpp b/kapi/include/kapi/memory/chunk.hpp
index 3d90326..4529535 100644
--- a/kapi/include/kapi/memory/chunk.hpp
+++ b/kapi/include/kapi/memory/chunk.hpp
@@ -30,7 +30,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.raw() / size};
+ return ChunkType{address / size};
}
//! Get the start address of the chunk referenced by this handle.