#include "deimos/core/api_registry.h" #include "deimos/core/allocator.h" static deimos::AllocatorApi* g_allocator_api; namespace deimos { AllocatorApi* BootstrapAllocatorApi(); void RegisterOsApi(ApiRegistry*); void RegisterLogApi(ApiRegistry*); struct ApiEntry { const ApiEntry* next{}; IdName name; void* impl; ApiEntry(const IdName& name_, void* impl_) : name{name_}, impl{impl_} {} }; class ApiRegistryImpl: public ApiRegistry { Allocator* m_allocator; const ApiEntry* m_head{}; public: explicit ApiRegistryImpl(Allocator* allocator) : m_allocator{allocator} {} void Set(const IdName& name, void* impl) final { auto* entry = m_allocator->New(name, impl); entry->next = std::exchange(m_head, entry); } void* Get(const IdName& name) final { for (const ApiEntry* it = m_head; it != nullptr; it = it->next) { if (it->name == name) { return it->impl; } } return nullptr; } }; ApiRegistry* InitializeGlobalApiRegistry() { g_allocator_api = BootstrapAllocatorApi(); Allocator* allocator = g_allocator_api->CreateChild(g_allocator_api->system, "API Registry"); ApiRegistry* api_registry = allocator->New(allocator); api_registry->Set(g_allocator_api); RegisterOsApi(api_registry); RegisterLogApi(api_registry); return api_registry; } } // namespace deimos