From e1229e05aba7554363b2aa9874bd383b5923ee8b Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Mon, 25 Mar 2024 23:28:26 +0100 Subject: Basic console API & API registry implementation --- deimos/core/BUILD | 2 ++ deimos/core/api_registry.cpp | 18 +++++++++++++++--- deimos/core/api_registry.h | 7 +++++++ deimos/core/base.h | 10 ++++++++++ deimos/core/id_name.h | 5 +++++ 5 files changed, 39 insertions(+), 3 deletions(-) diff --git a/deimos/core/BUILD b/deimos/core/BUILD index cb76f90..2f0dc2d 100644 --- a/deimos/core/BUILD +++ b/deimos/core/BUILD @@ -6,10 +6,12 @@ cc_library( "base.h", "hash.h", "id_name.h", + "os.h", ], srcs = [ "allocator.cpp", "api_registry.cpp", + "os_win32.cpp", ], visibility = ["//:__subpackages__"], ) diff --git a/deimos/core/api_registry.cpp b/deimos/core/api_registry.cpp index 873d72d..1489484 100644 --- a/deimos/core/api_registry.cpp +++ b/deimos/core/api_registry.cpp @@ -7,6 +7,7 @@ namespace deimos { AllocatorApi* BootstrapAllocatorApi(); +void RegisterOsApi(ApiRegistry*); struct ApiEntry { @@ -22,6 +23,7 @@ struct ApiEntry class ApiRegistryImpl: public ApiRegistry { Allocator* m_allocator; + const ApiEntry* m_head{}; public: explicit ApiRegistryImpl(Allocator* allocator) : @@ -30,9 +32,17 @@ public: void Set(const IdName& name, void* impl) final { - (void)name; - (void)impl; - (void)m_allocator; + 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; } }; @@ -45,6 +55,8 @@ ApiRegistry* InitializeGlobalApiRegistry() api_registry->Set(g_allocator_api); + RegisterOsApi(api_registry); + return api_registry; } diff --git a/deimos/core/api_registry.h b/deimos/core/api_registry.h index 2624935..f2f850a 100644 --- a/deimos/core/api_registry.h +++ b/deimos/core/api_registry.h @@ -15,8 +15,15 @@ public: virtual ~ApiRegistry() = default; + virtual void* Get(const IdName&) = 0; virtual void Set(const IdName&, void* impl) = 0; + template + Api* Get() + { + return (Api*)Get(Api::kApiName); + } + template void Set(Api* impl) { diff --git a/deimos/core/base.h b/deimos/core/base.h index ea14610..0669f07 100644 --- a/deimos/core/base.h +++ b/deimos/core/base.h @@ -46,6 +46,8 @@ struct uint128 { uint64 high; uint64 low; + + constexpr bool operator==(const uint128& other) const = default; }; struct SourceLocation @@ -96,5 +98,13 @@ constexpr T&& forward(deimos::RemoveReference&& t) noexcept // NOLINT return static_cast(t); } +template +constexpr T exchange(T& obj, U&& new_value) +{ + T old_value = std::move(obj); + obj = std::forward(new_value); + return old_value; +} + } // namespace std diff --git a/deimos/core/id_name.h b/deimos/core/id_name.h index 0fb793b..47e7708 100644 --- a/deimos/core/id_name.h +++ b/deimos/core/id_name.h @@ -15,6 +15,11 @@ struct IdName hash{MurmurHash3_x64_128(name_)}, name{name_} {} + + constexpr bool operator==(const IdName& other) const + { + return hash == other.hash; + } }; } // namespace deimos -- cgit