diff options
Diffstat (limited to 'kernel/kapi/devices')
| -rw-r--r-- | kernel/kapi/devices/bus.cpp | 20 | ||||
| -rw-r--r-- | kernel/kapi/devices/device.cpp | 9 |
2 files changed, 18 insertions, 11 deletions
diff --git a/kernel/kapi/devices/bus.cpp b/kernel/kapi/devices/bus.cpp index d9f3f991..179a04a8 100644 --- a/kernel/kapi/devices/bus.cpp +++ b/kernel/kapi/devices/bus.cpp @@ -9,6 +9,7 @@ #include <kstd/vector.hpp> #include <algorithm> +#include <span> #include <utility> namespace kapi::devices @@ -41,29 +42,30 @@ namespace kapi::devices return child_status; } - auto bus::add_child(kstd::unique_ptr<device> child) -> void + auto bus::add_child(kstd::shared_ptr<device> child) -> void { - auto observer = m_observers.emplace_back(child.get()); - child->set_parent(kstd::make_observer(this)); - m_devices.push_back(std::move(child)); - if (!kapi::devices::register_device(*observer)) + child->set_parent(kstd::static_pointer_cast<bus>(shared_from_this())); + + if (!kapi::devices::register_device(child)) { kapi::system::panic("[OS:DEV] Failed to register child device"); } + auto & attached = m_devices.emplace_back(std::move(child)); + if (m_initialized.test()) { - kstd::println("[OS:DEV] Initializing child device {}@{}", observer->name(), name()); - if (!observer->init()) + kstd::println("[OS:DEV] Initializing child device {}@{}", attached->name(), name()); + if (!attached->init()) { kapi::system::panic("[OS:DEV] Failed to initialize child device"); } } } - [[nodiscard]] auto bus::children() const -> kstd::vector<kstd::observer_ptr<device>> const & + [[nodiscard]] auto bus::children() const -> std::span<kstd::shared_ptr<device> const> { - return m_observers; + return {m_devices.data(), m_devices.size()}; } auto bus::enumerate() -> bool diff --git a/kernel/kapi/devices/device.cpp b/kernel/kapi/devices/device.cpp index 0d00d8fd..540d37b7 100644 --- a/kernel/kapi/devices/device.cpp +++ b/kernel/kapi/devices/device.cpp @@ -12,12 +12,17 @@ namespace kapi::devices : m_name(name) {} - [[nodiscard]] auto device::name() const -> kstd::string const & + auto device::name() const -> kstd::string const & { return m_name; } - auto device::set_parent(kstd::observer_ptr<bus> parent) -> void + auto device::parent() const -> kstd::shared_ptr<bus> + { + return m_parent.lock(); + } + + auto device::set_parent(kstd::weak_ptr<bus> parent) -> void { m_parent = parent; } |
