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 --- kernel/kapi/devices.cpp | 56 +++++++++++++++++++++++++++--------------- kernel/kapi/devices/bus.cpp | 20 ++++++++------- kernel/kapi/devices/device.cpp | 9 +++++-- 3 files changed, 54 insertions(+), 31 deletions(-) (limited to 'kernel/kapi') diff --git a/kernel/kapi/devices.cpp b/kernel/kapi/devices.cpp index 6da503d9..8f3011fe 100644 --- a/kernel/kapi/devices.cpp +++ b/kernel/kapi/devices.cpp @@ -10,7 +10,6 @@ #include #include -#include #include #include @@ -18,37 +17,55 @@ namespace kapi::devices { namespace { - auto constinit root_bus = std::optional{}; - auto constinit device_tree = kstd::flat_map>{}; + auto constinit root_bus = kstd::shared_ptr{}; + auto constinit device_tree = kstd::flat_map>{}; } // namespace auto init() -> void { - if (root_bus.has_value()) + if (root_bus) { kapi::system::panic("[OS:DEV] The device subsystem has already been initialized"); } interface_registry::init(); - auto & bus = root_bus.emplace(); - register_device(bus); - bus.init(); + root_bus = kstd::make_shared(); + register_device(root_bus); + root_bus->init(); } - auto get_root_bus() -> bus & + auto get_root_bus() -> kstd::shared_ptr { - if (!root_bus.has_value()) + if (!root_bus) { kapi::system::panic("[OS:DEV] Root bus not initialized!"); } - return *root_bus; + return root_bus; } - auto register_device(device & device) -> bool + auto register_device(kstd::shared_ptr device) -> bool { - kstd::println("[OS:DEV] Registering device {}", device.name()); - return device_tree.emplace(device.name(), &device).second; + if (!device) + { + return false; + } + + kstd::println("[OS:DEV] Registering device {}", device->name()); + + auto found = device_tree.find(device->name()); + if (found != device_tree.end()) + { + if (!found->second.expired()) + { + return false; + } + + found->second = device; + return true; + } + + return device_tree.emplace(device->name(), device).second; } auto unregister_device(device &) -> bool @@ -57,16 +74,15 @@ namespace kapi::devices return false; } - auto find_device(std::string_view name) -> kstd::observer_ptr + auto find_device(std::string_view name) -> kstd::shared_ptr { - for (auto const & [key, value] : device_tree) + auto found = device_tree.find(kstd::string{name}); + if (found == device_tree.end()) { - if (value->name() == name) - { - return value; - } + return nullptr; } - return nullptr; + + return found->second.lock(); } } // namespace kapi::devices 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 #include +#include #include namespace kapi::devices @@ -41,29 +42,30 @@ namespace kapi::devices return child_status; } - auto bus::add_child(kstd::unique_ptr child) -> void + auto bus::add_child(kstd::shared_ptr 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(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> const & + [[nodiscard]] auto bus::children() const -> std::span 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 parent) -> void + auto device::parent() const -> kstd::shared_ptr + { + return m_parent.lock(); + } + + auto device::set_parent(kstd::weak_ptr parent) -> void { m_parent = parent; } -- cgit v1.2.3