blob: 41c0257df672576061e5c94eaf97b36940eb7dd4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#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*);
void InitializeStatus(ApiRegistry*);
struct ApiEntry
{
gsl::owner<const ApiEntry*> next{};
IdName name;
void* impl;
ApiEntry(const IdName& name_, void* impl_) :
name{name_}, impl{impl_}
{}
};
class ApiRegistryImpl: public ApiRegistry
{
Allocator* m_allocator;
gsl::owner<const ApiEntry*> m_head{};
public:
explicit ApiRegistryImpl(Allocator* allocator) :
m_allocator{allocator}
{}
void Set(const IdName& name, void* impl) final
{
gsl::owner<ApiEntry*> entry = m_allocator->New<ApiEntry>(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();
gsl::owner<Allocator*> allocator = g_allocator_api->CreateChild(g_allocator_api->system, "API Registry");
gsl::owner<ApiRegistry*> api_registry = allocator->New<ApiRegistryImpl>(allocator);
api_registry->Set(g_allocator_api);
InitializeStatus(api_registry);
RegisterOsApi(api_registry);
RegisterLogApi(api_registry);
return api_registry;
}
} // namespace deimos
|