From 4884b594330dea41d04d776d14a5cb18d9a354bc Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Tue, 18 Mar 2025 22:31:59 +0100 Subject: Make status implementation more correct wrt type punning --- asl/base/utility.hpp | 7 +++++++ asl/containers/buffer.hpp | 4 +++- asl/types/status.cpp | 21 ++++++++------------- asl/types/status.hpp | 8 +++++--- 4 files changed, 23 insertions(+), 17 deletions(-) (limited to 'asl') diff --git a/asl/base/utility.hpp b/asl/base/utility.hpp index 206a5b1..85c873d 100644 --- a/asl/base/utility.hpp +++ b/asl/base/utility.hpp @@ -35,6 +35,13 @@ template return static_cast(x); } +template +[[nodiscard]] constexpr T* launder(T* ptr) noexcept // NOLINT + requires (!asl::is_func && !asl::is_void) +{ + return __builtin_launder(ptr); +} + } // namespace std namespace asl diff --git a/asl/containers/buffer.hpp b/asl/containers/buffer.hpp index c3d865f..f5ba975 100644 --- a/asl/containers/buffer.hpp +++ b/asl/containers/buffer.hpp @@ -110,7 +110,9 @@ private: constexpr void set_size_inline(isize_t new_size) { ASL_ASSERT(new_size >= 0 && new_size <= kInlineCapacity); - const size_t size_encoded = (load_size_encoded() & size_t{0x00ff'ffff'ffff'ffff}) | (bit_cast(new_size) << 56U); + const size_t size_encoded = + (load_size_encoded() & size_t{0x00ff'ffff'ffff'ffff}) + | (bit_cast(new_size) << 56U); store_size_encoded(size_encoded); } diff --git a/asl/types/status.cpp b/asl/types/status.cpp index 43f3b9e..246d017 100644 --- a/asl/types/status.cpp +++ b/asl/types/status.cpp @@ -15,7 +15,7 @@ using Allocator = asl::DefaultAllocator; // NOLINTNEXTLINE(*-non-const-global-variables) static Allocator g_allocator{}; -namespace +namespace asl { struct StatusInternal @@ -33,7 +33,7 @@ struct StatusInternal } }; -} // anonymous namespace +} // namespace asl asl::status::status(status_code code, string_view msg) : m_payload{alloc_new(g_allocator, msg, code)} @@ -49,34 +49,29 @@ asl::status::status(status_code code, string_view fmt, span(m_payload)->code; + return m_payload->code; } asl::string_view asl::status::message_internal() const { ASL_ASSERT(!is_inline()); - // NOLINTNEXTLINE(*-reinterpret-cast) - return reinterpret_cast(m_payload)->msg; + return m_payload->msg; } void asl::status::ref() { ASL_ASSERT(!is_inline()); - // NOLINTNEXTLINE(*-reinterpret-cast) - auto* internal = reinterpret_cast(m_payload); - atomic_fetch_increment(&internal->ref_count, memory_order::relaxed); + atomic_fetch_increment(&m_payload->ref_count, memory_order::relaxed); } void asl::status::unref() { ASL_ASSERT(!is_inline()); - // NOLINTNEXTLINE(*-reinterpret-cast) - auto* internal = reinterpret_cast(m_payload); - if (atomic_fetch_decrement(&internal->ref_count, memory_order::release) == 1) + if (atomic_fetch_decrement(&m_payload->ref_count, memory_order::release) == 1) { atomic_fence(memory_order::acquire); - alloc_delete(g_allocator, internal); + alloc_delete(g_allocator, m_payload); + m_payload = nullptr; } } diff --git a/asl/types/status.hpp b/asl/types/status.hpp index df96cd8..de95670 100644 --- a/asl/types/status.hpp +++ b/asl/types/status.hpp @@ -22,15 +22,17 @@ enum class status_code : uint8_t invalid_argument = 4, }; +struct StatusInternal; + class status { - void* m_payload{}; + StatusInternal* m_payload{}; - static constexpr void* status_to_payload(status_code code) + static constexpr StatusInternal* status_to_payload(status_code code) { return code == status_code::ok ? nullptr - : bit_cast((static_cast(code) << 1) | 1); + : bit_cast((static_cast(code) << 1) | 1); } static constexpr status_code payload_to_status(void* payload) -- cgit