From 74f080361e7736aca03d4028565df4f12bc47b74 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Sun, 27 Jun 2021 18:24:30 +0800 Subject: [PATCH] horrible_cast --- src/ipc.cpp | 8 ++++---- src/libipc/platform/detail.h | 12 ++++++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/ipc.cpp b/src/ipc.cpp index b3b0921..31c0a0e 100755 --- a/src/ipc.cpp +++ b/src/ipc.cpp @@ -32,8 +32,8 @@ namespace { -using msg_id_t = std::uint32_t; -using acc_t = std::atomic; +using msg_id_t = std::uint32_t; +using acc_t = std::atomic; template struct msg_t; @@ -505,8 +505,8 @@ static ipc::buff_t recv(ipc::handle_t h, std::size_t tm) { if (buf != nullptr) { if (recycled) { return ipc::buff_t{buf, msg_size, [](void* pmid, std::size_t size) { - release_storage(reinterpret_cast(pmid) - 1, size); - }, reinterpret_cast(buf_id + 1)}; + release_storage(ipc::detail::horrible_cast(pmid) - 1, size); + }, ipc::detail::horrible_cast(buf_id + 1)}; } else { return ipc::buff_t{buf, msg_size}; // no recycle } diff --git a/src/libipc/platform/detail.h b/src/libipc/platform/detail.h index fbf539a..141de6e 100755 --- a/src/libipc/platform/detail.h +++ b/src/libipc/platform/detail.h @@ -111,5 +111,17 @@ constexpr const T& (min)(const T& a, const T& b) { #endif/*__cplusplus < 201703L*/ +template +auto horrible_cast(U rhs) noexcept + -> typename std::enable_if::value + && std::is_trivially_copyable::value, T>::type { + union { + T t; + U u; + } r = {}; + r.u = rhs; + return r.t; +} + } // namespace detail } // namespace ipc