From b99b79d8080cc1491f96069677d9ce0b7775a4f0 Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Wed, 9 Sep 2026 23:02:20 +0200 Subject: chore: replace some naked pointers The coding guidelines explicitly prohibit the use of "naked"/C-style pointers. However, there were some prominent examples in the kapi and the core kernel source. This changeset replaces them with the appropriate smart pointer types. --- kapi/kapi/boot_modules/device.hpp | 4 +++- kapi/kapi/devices.hpp | 4 ++-- kapi/kapi/devices/bus.hpp | 2 +- kapi/kapi/devices/device.hpp | 16 ++++++++-------- kapi/kapi/devices/driver.hpp | 12 +++++++----- kapi/kapi/devices/facet_registry.hpp | 30 +++++++++++++++--------------- 6 files changed, 36 insertions(+), 32 deletions(-) (limited to 'kapi') diff --git a/kapi/kapi/boot_modules/device.hpp b/kapi/kapi/boot_modules/device.hpp index 4bdd34e4..7d3e3d6e 100644 --- a/kapi/kapi/boot_modules/device.hpp +++ b/kapi/kapi/boot_modules/device.hpp @@ -6,6 +6,8 @@ #include #include +#include + #include #include #include @@ -54,7 +56,7 @@ namespace kapi::boot_modules [[nodiscard]] auto module() const -> struct module const & override; protected: - auto query_facet(kapi::capabilities::facet_id facet) -> void * override; + auto query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr override; private: struct module m_module; diff --git a/kapi/kapi/devices.hpp b/kapi/kapi/devices.hpp index c4bda9b9..c7de1830 100644 --- a/kapi/kapi/devices.hpp +++ b/kapi/kapi/devices.hpp @@ -58,8 +58,8 @@ namespace kapi::devices //! @param name A stable name for the device. //! @param implementation The implementation of the facet for the device. template - [[nodiscard]] auto publish_facet(kstd::shared_ptr device, kstd::string name, Facet * implementation) - -> kstd::result + [[nodiscard]] auto publish_facet(kstd::shared_ptr device, kstd::string name, + kstd::observer_ptr implementation) -> kstd::result { return facet_registry::get().publish(device, std::move(name), implementation); } diff --git a/kapi/kapi/devices/bus.hpp b/kapi/kapi/devices/bus.hpp index aaca462a..476216c7 100644 --- a/kapi/kapi/devices/bus.hpp +++ b/kapi/kapi/devices/bus.hpp @@ -59,7 +59,7 @@ namespace kapi::devices protected: //! All busses have the "bus" facet. - auto query_facet(kapi::capabilities::facet_id facet) -> void * override; + auto query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr override; private: auto do_remove_child(device & child) -> void; diff --git a/kapi/kapi/devices/device.hpp b/kapi/kapi/devices/device.hpp index 86566bff..c3a33daf 100644 --- a/kapi/kapi/devices/device.hpp +++ b/kapi/kapi/devices/device.hpp @@ -56,22 +56,22 @@ namespace kapi::devices //! //! @param id The id of the desired facet. //! @return An opaque pointer to the facet implementation if this device supports it, nullptr otherwise. - [[nodiscard]] auto facet(kapi::capabilities::facet_id id) noexcept -> void *; + [[nodiscard]] auto facet(kapi::capabilities::facet_id id) noexcept -> kstd::observer_ptr; //! Get a specific facet of this device, if it supports it. //! //! @param id The id of the desired facet. //! @return A opaque pointer to the facet implementation if this device supports it, nullptr otherwise. - [[nodiscard]] auto facet(kapi::capabilities::facet_id id) const noexcept -> void const *; + [[nodiscard]] auto facet(kapi::capabilities::facet_id id) const noexcept -> kstd::observer_ptr; //! Get a specific facet of this device, if it supports it. //! //! @tparam FacetType The type of the desired facet. //! @return A typed pointer to the facet implementation if this device supports it, nullptr otherwise. template - [[nodiscard]] auto facet() -> FacetType * + [[nodiscard]] auto facet() -> kstd::observer_ptr { - return static_cast(facet(FacetType::id)); + return kstd::observer_ptr{static_cast(facet(FacetType::id).get())}; } //! Get a specific facet of this device, if it supports it. @@ -79,9 +79,9 @@ namespace kapi::devices //! @tparam FacetType The type of the desired facet. //! @return A typed pointer to the facet implementation if this device supports it, nullptr otherwise. template - [[nodiscard]] auto facet() const noexcept -> FacetType const * + [[nodiscard]] auto facet() const noexcept -> kstd::observer_ptr { - return static_cast(facet(FacetType::id)); + return kstd::observer_ptr{static_cast(facet(FacetType::id).get())}; } //! Check if this device has a specific facet. @@ -119,7 +119,7 @@ namespace kapi::devices auto set_state(enum state state) -> void; //! Get the driver, if any, currently bound to this device. - [[nodiscard]] auto bound_driver() const noexcept -> driver *; + [[nodiscard]] auto bound_driver() const noexcept -> kstd::shared_ptr; //! Bind this device to the given driver. auto bind_driver(kstd::weak_ptr driver) -> void; @@ -159,7 +159,7 @@ namespace kapi::devices auto set_resources(kstd::vector resources) -> void; protected: - auto virtual query_facet(kapi::capabilities::facet_id facet) -> void *; + auto virtual query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr; private: //! Busses need to be able to register themselves as a device's parent. diff --git a/kapi/kapi/devices/driver.hpp b/kapi/kapi/devices/driver.hpp index b46091fe..8c4d5e97 100644 --- a/kapi/kapi/devices/driver.hpp +++ b/kapi/kapi/devices/driver.hpp @@ -5,6 +5,7 @@ #include +#include #include #include @@ -66,9 +67,9 @@ namespace kapi::devices //! @return A pointer to this drivers implementation of the requested facet, nullptr if this driver does not //! support the requested facet. template - [[nodiscard]] auto facet() -> FacetType * + [[nodiscard]] auto facet() -> kstd::observer_ptr { - return static_cast(query_facet(FacetType::id)); + return kstd::observer_ptr{static_cast(query_facet(FacetType::id))}; } //! Retrieve this drivers implementation of a given capability facet. @@ -76,9 +77,10 @@ namespace kapi::devices //! @return A pointer to this drivers implementation of the requested facet, nullptr if this driver does not //! support the requested facet. template - [[nodiscard]] auto facet() const -> FacetType const * + [[nodiscard]] auto facet() const -> kstd::observer_ptr { - return static_cast(const_cast(this)->query_facet(FacetType::id)); + return kstd::observer_ptr{ + static_cast(const_cast(this)->query_facet(FacetType::id).get())}; } //! Check if this driver implements a given capability facet. @@ -107,7 +109,7 @@ namespace kapi::devices protected: //! Return a pointer to the implementation of the given facet if this driver supports it. - virtual auto query_facet(kapi::capabilities::facet_id facet) -> void *; + virtual auto query_facet(kapi::capabilities::facet_id facet) -> kstd::observer_ptr; }; //! @} diff --git a/kapi/kapi/devices/facet_registry.hpp b/kapi/kapi/devices/facet_registry.hpp index 3c405923..5e0367c1 100644 --- a/kapi/kapi/devices/facet_registry.hpp +++ b/kapi/kapi/devices/facet_registry.hpp @@ -51,7 +51,7 @@ namespace kapi::devices //! @param facet The id of the implemented facet. //! @param implementation A pointer to the actual implementation of the facet for the given device. constexpr entry(kstd::shared_ptr device, kstd::string name, kapi::capabilities::facet_id facet, - void * implementation) + kstd::observer_ptr implementation) : m_device{device} , m_name{name} , m_id{facet} @@ -86,7 +86,7 @@ namespace kapi::devices //! Get the facet of the device-facet-implementation tuple described by this entry. //! //! @return An untyped pointer to the facet implementation. - [[nodiscard]] constexpr auto untyped_facet() const noexcept -> void * + [[nodiscard]] constexpr auto untyped_facet() const noexcept -> kstd::observer_ptr { return m_facet; } @@ -95,11 +95,11 @@ namespace kapi::devices //! //! @return A typed pointer to the facet if the facet id matches, nullptr otherwise. template - [[nodiscard]] constexpr auto facet() noexcept -> FacetType * + [[nodiscard]] constexpr auto facet() noexcept -> kstd::observer_ptr { if (m_id == FacetType::id) { - return static_cast(untyped_facet()); + return kstd::observer_ptr{static_cast(untyped_facet().get())}; } return nullptr; } @@ -108,11 +108,11 @@ namespace kapi::devices //! //! @return A typed pointer to the facet if the facet id matches, nullptr otherwise. template - [[nodiscard]] constexpr auto facet() const noexcept -> FacetType const * + [[nodiscard]] constexpr auto facet() const noexcept -> kstd::observer_ptr { if (m_id == FacetType::id) { - return static_cast(untyped_facet()); + return kstd::observer_ptr{static_cast(untyped_facet().get())}; } return nullptr; } @@ -121,7 +121,7 @@ namespace kapi::devices kstd::weak_ptr m_device; kstd::string m_name; kapi::capabilities::facet_id m_id; - void * m_facet; + kstd::observer_ptr m_facet; }; //! Construct an empty facet registry. @@ -172,7 +172,7 @@ namespace kapi::devices //! @param name A stable name for the device. //! @param facet The implementation of the facet for the device. template - [[nodiscard]] auto publish(kstd::shared_ptr device, kstd::string name, FacetType * facet) + [[nodiscard]] auto publish(kstd::shared_ptr device, kstd::string name, kstd::observer_ptr facet) -> kstd::result { return do_publish(device, std::move(name), FacetType::id, facet); @@ -198,22 +198,22 @@ namespace kapi::devices //! //! @param id The id of the facet to look for. //! @param name The stable name of the device. - [[nodiscard]] auto resolve(kapi::capabilities::facet_id id, std::string_view name) -> void *; + [[nodiscard]] auto resolve(kapi::capabilities::facet_id id, std::string_view name) -> kstd::observer_ptr; //! Attempt to resolve a facet for a device. //! //! @param id The id of the facet to look for. //! @param device The device. - [[nodiscard]] auto resolve(kapi::capabilities::facet_id id, device & device) -> void *; + [[nodiscard]] auto resolve(kapi::capabilities::facet_id id, device & device) -> kstd::observer_ptr; //! Attempt to resolve a facet for a device by name. //! //! @tparam FacetType The facet to look for. //! @param name The stable name of the device. template - [[nodiscard]] auto resolve(std::string_view name) -> FacetType * + [[nodiscard]] auto resolve(std::string_view name) -> kstd::observer_ptr { - return static_cast(resolve(FacetType::id, name)); + return kstd::observer_ptr{static_cast(resolve(FacetType::id, name).get())}; } //! Attempt to resolve a facet for a device. @@ -221,9 +221,9 @@ namespace kapi::devices //! @tparam FacetType The facet to look for. //! @param device The device. template - [[nodiscard]] auto resolve(device & device) -> FacetType * + [[nodiscard]] auto resolve(device & device) -> kstd::observer_ptr { - return static_cast(resolve(FacetType::id, device)); + return kstd::observer_ptr{static_cast(resolve(FacetType::id, device).get())}; } //! Subscribe to facet publish/withdraw notifications. @@ -256,7 +256,7 @@ namespace kapi::devices //! @param id The id of the facet to be published for the device. //! @param facet The facet of the device. [[nodiscard]] auto do_publish(kstd::shared_ptr device, kstd::string name, kapi::capabilities::facet_id id, - void * facet) -> kstd::result; + kstd::observer_ptr facet) -> kstd::result; //! Notify all subscribed observers about a new facet having been published for a device. //! -- cgit v1.2.3