From e99eb52d9923606dd325862a5f2e649eee0b2d3e Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Fri, 24 Jul 2026 17:50:16 +0200 Subject: kapi: formally extract device registry --- kernel/kapi/devices/device_registry.cpp | 106 ++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 kernel/kapi/devices/device_registry.cpp (limited to 'kernel/kapi/devices/device_registry.cpp') diff --git a/kernel/kapi/devices/device_registry.cpp b/kernel/kapi/devices/device_registry.cpp new file mode 100644 index 00000000..aca0e7fe --- /dev/null +++ b/kernel/kapi/devices/device_registry.cpp @@ -0,0 +1,106 @@ +#include + +#include +#include + +#include +#include +#include +#include + +#include +#include +#include + +namespace kapi::devices +{ + + namespace + { + auto constinit instance = std::optional{}; + } + + auto device_registry::init() -> void + { + if (instance) + { + system::panic("[OS:DEV] Device registry has already been initialized"); + } + + instance = device_registry{}; + } + + auto device_registry::get() -> device_registry & + { + if (!instance) + { + system::panic("[OS:DEV] Device registry has not been initialized"); + } + + return *instance; + } + + auto device_registry::add(kstd::shared_ptr device) -> bool + { + if (!device) + { + return false; + } + + kstd::println("[OS:DEV] Registering device {}", device->name()); + + auto found = m_devices.find(device->name()); + if (found != m_devices.end()) + { + if (!found->second.expired()) + { + return false; + } + + found->second = device; + return true; + } + + return m_devices.emplace(device->name(), device).second; + } + + auto device_registry::remove(device &) -> bool + { + kstd::println("[OS:DEV] TODO: implement device deregistration"); + return false; + } + + auto device_registry::find(std::string_view name) const -> kstd::shared_ptr + { + auto found = m_devices.find(kstd::string{name}); + if (found == m_devices.end()) + { + return nullptr; + } + + return found->second.lock(); + } + + auto device_registry::all() const -> kstd::vector> + { + auto result = kstd::vector>{}; + + for (auto const & [name, weak_device] : m_devices) + { + if (auto device = weak_device.lock()) + { + result.push_back(std::move(device)); + } + } + + return result; + } +} // namespace kapi::devices + +namespace kapi::test_support::devices +{ + auto deinit_device_registry() -> void + { + kapi::devices::instance.reset(); + } +} // namespace kapi::test_support::devices \ No newline at end of file -- cgit v1.2.3