From 5449acc193f96d2ffaeb224aad5cea16bf753cdb Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 10 Sep 2026 08:53:57 +0200 Subject: x86_64: replace raw pointers with observer_ptr --- kapi/kapi/memory.hpp | 8 ++++++-- kapi/kapi/memory/address.hpp | 24 +++++++++++++++++++++--- kapi/kapi/memory/page_mapper.hpp | 5 +++-- 3 files changed, 30 insertions(+), 7 deletions(-) (limited to 'kapi') 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 // IWYU pragma: export #include // IWYU pragma: export +#include + #include #include #include @@ -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; //! 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; //! 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 #include +#include #include #include @@ -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(pointer)} + explicit address(std::byte const * pointer) noexcept + : m_value{reinterpret_cast(pointer)} + {} + + //! Construct an address representing the given pointer value. + //! + //! @param pointer The pointer value to initialize this address with. + explicit address(kstd::observer_ptr pointer) noexcept + : m_value{reinterpret_cast(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 explicit operator ObjectType *() const noexcept @@ -61,6 +69,16 @@ namespace kapi::memory return std::bit_cast(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 + explicit operator kstd::observer_ptr() const noexcept + { + return kstd::make_observer(reinterpret_cast(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 #include +#include #include #include @@ -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 = 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 - [[nodiscard]] auto map_as(page page, frame frame, flags flags) -> T * + [[nodiscard]] auto map_as(page page, frame frame, flags flags) -> kstd::observer_ptr { return std::bit_cast(map(page, frame, flags)); } -- cgit v1.2.3