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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#include "deimos/core/status.h"
#include "deimos/core/api_registry.h"
#include "deimos/core/allocator.h"
#include "deimos/core/atomic.h"
static deimos::AllocatorApi* allocator_api;
namespace deimos
{
StringView StatusCodeToString(StatusCode code)
{
switch (code)
{
case StatusCode::kOk: return StringView("OK");
case StatusCode::kUnknown: return StringView("Unknown error");
case StatusCode::kInvalidArgument: return StringView("Invalid argument");
case StatusCode::kUnimplemented: return StringView("Unimplemented");
case StatusCode::kInternal: return StringView("Internal error");
}
}
struct StatusRep
{
Atomic<int32_t> ref_count;
StatusCode code{};
StringView message;
};
Status::Status(StatusCode code, StringView message)
{
if (code == StatusCode::kOk || message.empty())
{
m_rep = CodeToRep(code);
}
else
{
void* ptr = allocator_api->system->Allocate((int64_t)sizeof(StatusRep) + message.size() + 1);
auto* rep = new(ptr) StatusRep;
char* message_ptr = (char*)ptr + sizeof(StatusRep); // NOLINT
MemoryCopy(message_ptr, message.data(), message.size());
message_ptr[message.size()] = '\0'; // NOLINT
AtomicStore(&rep->ref_count, 1);
rep->code = code;
rep->message = StringView(message_ptr, message.size());
m_rep = std::bit_cast<uintptr_t>(rep);
Ensures((m_rep & 1U) == 0);
}
}
void Status::Ref() const
{
if (!IsInline())
{
auto* rep = std::bit_cast<StatusRep*>(m_rep);
Expects(AtomicLoad(&rep->ref_count) > 0);
AtomicFetchIncrement(&rep->ref_count);
}
}
StatusCode Status::RepCode() const
{
Expects(!IsInline());
return std::bit_cast<StatusRep*>(m_rep)->code;
}
void Status::Unref() const
{
if (!IsInline())
{
auto* rep = std::bit_cast<StatusRep*>(m_rep);
Expects(AtomicLoad(&rep->ref_count) > 0);
if (AtomicFetchDecrement(&rep->ref_count, MemoryOrder::kRelease) == 1)
{
AtomicFence(MemoryOrder::kAcquire);
deimos_StaticAssert(std::is_trivially_destructible_v<StatusRep>);
allocator_api->system->Free(
rep, (int64_t)sizeof(StatusRep) + rep->message.size() + 1);
}
}
}
void DeimosFormat(IWriter* writer, const Status& status)
{
if (status.IsInline())
{
Format(writer, "[$]", StatusCodeToString(status.code()));
}
else
{
auto* rep = std::bit_cast<StatusRep*>(status.m_rep);
Format(writer, "[$: $]", StatusCodeToString(rep->code), rep->message);
}
}
void InitializeStatus(ApiRegistry* api_registry)
{
allocator_api = api_registry->Get<AllocatorApi>();
}
} // namespace deimos
|