aboutsummaryrefslogtreecommitdiff
path: root/kapi
diff options
context:
space:
mode:
authorFelix Morgner <felix.morgner@ost.ch>2026-07-23 01:38:56 +0200
committerFelix Morgner <felix.morgner@ost.ch>2026-07-23 01:38:56 +0200
commit526709a1e86d55dd88e2dbdbc471795efce9046b (patch)
tree04d9de462f506f983d3a991179eccfd68300cc14 /kapi
parent0361a4912affe1594def1aa24e387fe5440afaee (diff)
downloadkernel-526709a1e86d55dd88e2dbdbc471795efce9046b.tar.xz
kernel-526709a1e86d55dd88e2dbdbc471795efce9046b.zip
kapi: bootstrap owning device tree
Diffstat (limited to 'kapi')
-rw-r--r--kapi/kapi/devices.hpp4
-rw-r--r--kapi/kapi/devices/bus.hpp8
-rw-r--r--kapi/kapi/devices/device.hpp11
-rw-r--r--kapi/kapi/devices/manager.hpp6
4 files changed, 17 insertions, 12 deletions
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<bus>;
//! 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 <kstd/vector.hpp>
#include <atomic>
+#include <span>
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<device> child) -> void;
+ auto add_child(kstd::shared_ptr<device> child) -> void;
- [[nodiscard]] auto children() const -> kstd::vector<kstd::observer_ptr<device>> const &;
+ [[nodiscard]] auto children() const -> std::span<kstd::shared_ptr<device> const>;
protected:
//! Enumerate the devices on the bus.
@@ -47,8 +48,7 @@ namespace kapi::devices
auto virtual enumerate() -> bool;
private:
- kstd::vector<kstd::unique_ptr<device>> m_devices;
- kstd::vector<kstd::observer_ptr<device>> m_observers;
+ kstd::vector<kstd::shared_ptr<device>> 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<device>
{
/**
* @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<struct bus>;
+
protected:
auto virtual query_interface(interface interface) -> void *;
private:
friend struct bus;
- auto set_parent(kstd::observer_ptr<struct bus> parent) -> void;
+ auto set_parent(kstd::weak_ptr<struct bus> parent) -> void;
kstd::string m_name;
- kstd::observer_ptr<bus> m_parent;
+ kstd::weak_ptr<bus> 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> 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<device>;
+ //! @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<device>;
//! @}