summaryrefslogtreecommitdiff
path: root/asl/status.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'asl/status.cpp')
-rw-r--r--asl/status.cpp24
1 files changed, 23 insertions, 1 deletions
diff --git a/asl/status.cpp b/asl/status.cpp
index ac0b29e..eb26111 100644
--- a/asl/status.cpp
+++ b/asl/status.cpp
@@ -1,5 +1,7 @@
#include "asl/status.hpp"
#include "asl/allocator.hpp"
+#include "asl/string.hpp"
+#include "asl/atomic.hpp"
using Allocator = asl::DefaultAllocator;
static Allocator g_allocator{};
@@ -9,8 +11,17 @@ namespace
struct StatusInternal
{
- asl::string_view msg;
+ // @Todo Use custom allocator
+ asl::string<> msg;
asl::status_code code;
+ asl::atomic<int32_t> ref_count;
+
+ constexpr StatusInternal(asl::string_view msg_, asl::status_code code_)
+ : msg{msg_}
+ , code{code_}
+ {
+ atomic_store(&ref_count, 1);
+ }
};
} // anonymous namespace
@@ -25,3 +36,14 @@ asl::status_code asl::status::code_internal() const
return reinterpret_cast<const StatusInternal*>(m_payload)->code;
}
+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);
+ }
+}
+