blob: c081f334b3b34127f324d0a55b9d6347d730d5f0 (
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
|
#include "asl/status.hpp"
#include "asl/allocator.hpp"
#include "asl/string.hpp"
#include "asl/atomic.hpp"
// @Todo Use custom allocator
using Allocator = asl::DefaultAllocator;
static Allocator g_allocator{};
namespace
{
struct StatusInternal
{
asl::string<Allocator> msg;
asl::status_code code;
asl::atomic<int32_t> ref_count;
constexpr StatusInternal(asl::string_view msg_, asl::status_code code_)
: msg{msg_, g_allocator}
, code{code_}
{
atomic_store(&ref_count, 1);
}
};
} // anonymous namespace
asl::status::status(status_code code, string_view msg)
: m_payload{alloc_new<StatusInternal>(g_allocator, msg, code)}
{}
asl::status_code asl::status::code_internal() const
{
ASL_ASSERT(!is_inline());
return reinterpret_cast<const StatusInternal*>(m_payload)->code;
}
asl::string_view asl::status::message_internal() const
{
ASL_ASSERT(!is_inline());
return reinterpret_cast<const StatusInternal*>(m_payload)->msg;
}
void asl::status::ref()
{
ASL_ASSERT(!is_inline());
auto* internal = reinterpret_cast<StatusInternal*>(m_payload);
atomic_fetch_increment(&internal->ref_count, memory_order::relaxed);
}
void asl::status::unref()
{
ASL_ASSERT(!is_inline());
auto* internal = reinterpret_cast<StatusInternal*>(m_payload);
if (atomic_fetch_decrement(&internal->ref_count, memory_order::release) == 1)
{
atomic_fence(memory_order::acquire);
alloc_delete(g_allocator, internal);
}
}
|