diff options
| author | Felix Morgner <felix.morgner@ost.ch> | 2026-07-23 01:38:56 +0200 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@ost.ch> | 2026-07-23 01:38:56 +0200 |
| commit | 526709a1e86d55dd88e2dbdbc471795efce9046b (patch) | |
| tree | 04d9de462f506f983d3a991179eccfd68300cc14 /kernel | |
| parent | 0361a4912affe1594def1aa24e387fe5440afaee (diff) | |
| download | kernel-526709a1e86d55dd88e2dbdbc471795efce9046b.tar.xz kernel-526709a1e86d55dd88e2dbdbc471795efce9046b.zip | |
kapi: bootstrap owning device tree
Diffstat (limited to 'kernel')
| -rw-r--r-- | kernel/kapi/devices.cpp | 56 | ||||
| -rw-r--r-- | kernel/kapi/devices/bus.cpp | 20 | ||||
| -rw-r--r-- | kernel/kapi/devices/device.cpp | 9 |
3 files changed, 54 insertions, 31 deletions
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 <kstd/print.hpp> #include <kstd/string.hpp> -#include <optional> #include <string_view> #include <utility> @@ -18,37 +17,55 @@ namespace kapi::devices { namespace { - auto constinit root_bus = std::optional<kernel::devices::root_bus>{}; - auto constinit device_tree = kstd::flat_map<kstd::string, kstd::observer_ptr<device>>{}; + auto constinit root_bus = kstd::shared_ptr<kernel::devices::root_bus>{}; + auto constinit device_tree = kstd::flat_map<kstd::string, kstd::weak_ptr<device>>{}; } // 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<kernel::devices::root_bus>(); + register_device(root_bus); + root_bus->init(); } - auto get_root_bus() -> bus & + auto get_root_bus() -> kstd::shared_ptr<bus> { - 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> 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<device> + auto find_device(std::string_view name) -> kstd::shared_ptr<device> { - 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 <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; } |
