aboutsummaryrefslogtreecommitdiff
path: root/kapi
diff options
context:
space:
mode:
Diffstat (limited to 'kapi')
-rw-r--r--kapi/kapi/memory.hpp8
-rw-r--r--kapi/kapi/memory/address.hpp24
-rw-r--r--kapi/kapi/memory/page_mapper.hpp5
3 files changed, 30 insertions, 7 deletions
diff --git a/kapi/kapi/memory.hpp b/kapi/kapi/memory.hpp
index 8ad8d6ec..4b14b553 100644
--- a/kapi/kapi/memory.hpp
+++ b/kapi/kapi/memory.hpp
@@ -9,6 +9,8 @@
#include <kapi/memory/page.hpp> // IWYU pragma: export
#include <kapi/memory/page_mapper.hpp> // IWYU pragma: export
+#include <kstd/memory.hpp>
+
#include <cstddef>
#include <optional>
#include <utility>
@@ -72,7 +74,8 @@ namespace kapi::memory
//! @param frame The frame to map the page into.
//! @param flags The flags to apply to this mapping.
//! @return A pointer to the first byte of the mapped page.
- auto map(page page, frame frame, page_mapper::flags flags = page_mapper::flags::empty) -> std::byte *;
+ auto map(page page, frame frame, page_mapper::flags flags = page_mapper::flags::empty)
+ -> kstd::observer_ptr<std::byte>;
//! Unmap a page.
//!
@@ -103,7 +106,8 @@ namespace kapi::memory
//! @param region The region to map.
//! @param hw_base The base of the hardware region.
//! @param flags The flags to apply.
- auto map_mmio_region(mmio_region region, physical_address hw_base, page_mapper::flags flags = {}) -> std::byte *;
+ auto map_mmio_region(mmio_region region, physical_address hw_base, page_mapper::flags flags = {})
+ -> kstd::observer_ptr<std::byte>;
//! Release a Memory-mapped I/O region.
//!
diff --git a/kapi/kapi/memory/address.hpp b/kapi/kapi/memory/address.hpp
index 5b84a381..0fab06e0 100644
--- a/kapi/kapi/memory/address.hpp
+++ b/kapi/kapi/memory/address.hpp
@@ -4,6 +4,7 @@
// IWYU pragma: private, include <kapi/memory.hpp>
#include <kstd/format.hpp>
+#include <kstd/memory.hpp>
#include <kstd/units.hpp>
#include <bit>
@@ -47,13 +48,20 @@ namespace kapi::memory
//! Construct an address representing the given pointer value.
//!
//! @param pointer The pointer value to initialize this address with.
- explicit address(std::byte * pointer) noexcept
- : m_value{std::bit_cast<std::uintptr_t>(pointer)}
+ explicit address(std::byte const * pointer) noexcept
+ : m_value{reinterpret_cast<std::uintptr_t>(pointer)}
+ {}
+
+ //! Construct an address representing the given pointer value.
+ //!
+ //! @param pointer The pointer value to initialize this address with.
+ explicit address(kstd::observer_ptr<std::byte const> pointer) noexcept
+ : m_value{reinterpret_cast<std::uintptr_t>(pointer.get())}
{}
//! Convert this address into a C++ pointer.
//!
- //! @tparam T The type of the object this address should refer to.
+ //! @tparam ObjectType The type of the object the pointer should refer to.
//! @return This address as a typed pointer to the given type.
template<typename ObjectType>
explicit operator ObjectType *() const noexcept
@@ -61,6 +69,16 @@ namespace kapi::memory
return std::bit_cast<ObjectType *>(m_value);
}
+ //! Convert this address into an observer pointer.
+ //!
+ //! @tparam ObjectType The type of the object the pointer should refer to.
+ //! @return An observer pointer pointing to the memory location represented by this address.
+ template<typename ObjectType>
+ explicit operator kstd::observer_ptr<ObjectType>() const noexcept
+ {
+ return kstd::make_observer(reinterpret_cast<ObjectType *>(m_value));
+ }
+
//! Create a new address n beyond this one.
//!
//! @param n The amount to add to this address.
diff --git a/kapi/kapi/memory/page_mapper.hpp b/kapi/kapi/memory/page_mapper.hpp
index 3deb4702..a310e209 100644
--- a/kapi/kapi/memory/page_mapper.hpp
+++ b/kapi/kapi/memory/page_mapper.hpp
@@ -7,6 +7,7 @@
#include <kapi/memory/page.hpp>
#include <kstd/bitfield_enum.hpp>
+#include <kstd/memory.hpp>
#include <cstddef>
#include <cstdint>
@@ -45,7 +46,7 @@ namespace kapi::memory
//! @param frame The frame to map the page into.
//! @param flags The flags to map the page with.
//! @return A pointer to the first byte of mapped page.
- virtual auto map(page page, frame frame, flags flags) -> std::byte * = 0;
+ virtual auto map(page page, frame frame, flags flags) -> kstd::observer_ptr<std::byte> = 0;
//! Unmap the given page.
//!
@@ -70,7 +71,7 @@ namespace kapi::memory
//! @param flags The flags to map the page with.
//! @return A pointer to the first T in the page.
template<typename T>
- [[nodiscard]] auto map_as(page page, frame frame, flags flags) -> T *
+ [[nodiscard]] auto map_as(page page, frame frame, flags flags) -> kstd::observer_ptr<T>
{
return std::bit_cast<T *>(map(page, frame, flags));
}