From 526709a1e86d55dd88e2dbdbc471795efce9046b Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Thu, 23 Jul 2026 01:38:56 +0200 Subject: kapi: bootstrap owning device tree --- kapi/kapi/devices.hpp | 4 ++-- kapi/kapi/devices/bus.hpp | 8 ++++---- kapi/kapi/devices/device.hpp | 11 ++++++++--- kapi/kapi/devices/manager.hpp | 6 +++--- 4 files changed, 17 insertions(+), 12 deletions(-) (limited to 'kapi') diff --git a/kapi/kapi/devices.hpp b/kapi/kapi/devices.hpp index e86e9ee7..5c3d1b63 100644 --- a/kapi/kapi/devices.hpp +++ b/kapi/kapi/devices.hpp @@ -29,8 +29,8 @@ namespace kapi::devices //! //! @warning This function will panic if the root bus has not been initialized. //! - //! @return a reference to the root bus. - auto get_root_bus() -> bus &; + //! @return a shared pointer to the root bus. + auto get_root_bus() -> kstd::shared_ptr; //! Publish the fact that a given device implements a given interface. //! diff --git a/kapi/kapi/devices/bus.hpp b/kapi/kapi/devices/bus.hpp index 1f433efc..6356e166 100644 --- a/kapi/kapi/devices/bus.hpp +++ b/kapi/kapi/devices/bus.hpp @@ -11,6 +11,7 @@ #include #include +#include namespace kapi::devices { @@ -36,9 +37,9 @@ namespace kapi::devices //! Whenever a device is attached to a bus, the bus takes sole ownership of the device. //! //! @param child The child device to attach. - auto add_child(kstd::unique_ptr child) -> void; + auto add_child(kstd::shared_ptr child) -> void; - [[nodiscard]] auto children() const -> kstd::vector> const &; + [[nodiscard]] auto children() const -> std::span const>; protected: //! Enumerate the devices on the bus. @@ -47,8 +48,7 @@ namespace kapi::devices auto virtual enumerate() -> bool; private: - kstd::vector> m_devices; - kstd::vector> m_observers; + kstd::vector> m_devices; std::atomic_flag m_init_was_called{}; std::atomic_flag m_initialized{}; }; diff --git a/kapi/kapi/devices/device.hpp b/kapi/kapi/devices/device.hpp index d72a6bbd..6ffd052d 100644 --- a/kapi/kapi/devices/device.hpp +++ b/kapi/kapi/devices/device.hpp @@ -17,7 +17,7 @@ namespace kapi::devices /** * @brief Base device identified by a name. */ - struct device + struct device : kstd::enable_shared_from_this { /** * @brief Create a device identified by @p name. @@ -60,16 +60,21 @@ namespace kapi::devices */ [[nodiscard]] auto name() const -> kstd::string const &; + //! Get the parent bus of this device. + //! + //! @return a shared pointer to the parent bus, or nullptr if this device has no parent. + [[nodiscard]] auto parent() const -> kstd::shared_ptr; + protected: auto virtual query_interface(interface interface) -> void *; private: friend struct bus; - auto set_parent(kstd::observer_ptr parent) -> void; + auto set_parent(kstd::weak_ptr parent) -> void; kstd::string m_name; - kstd::observer_ptr m_parent; + kstd::weak_ptr m_parent; }; //! @} diff --git a/kapi/kapi/devices/manager.hpp b/kapi/kapi/devices/manager.hpp index 25b22510..5b620b27 100644 --- a/kapi/kapi/devices/manager.hpp +++ b/kapi/kapi/devices/manager.hpp @@ -19,7 +19,7 @@ namespace kapi::devices //! //! @param device The device to register. //! @return true if the device was registered successfully, false otherwise. - auto register_device(device & device) -> bool; + auto register_device(kstd::shared_ptr device) -> bool; //! Unregister a device from the kernel's device manager. //! @@ -30,8 +30,8 @@ namespace kapi::devices //! Find a device by its name. //! //! @param name the name of the device. - //! @return a pointer to the device iff. the device was found, nullptr otherwise. - auto find_device(std::string_view name) -> kstd::observer_ptr; + //! @return a shared pointer to the device iff. the device was found and is still alive, nullptr otherwise. + auto find_device(std::string_view name) -> kstd::shared_ptr; //! @} -- cgit v1.2.3