Skip to content

Commit 3490945

Browse files
committed
horrible_cast
1 parent 628914d commit 3490945

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/ipc.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131

3232
namespace {
3333

34-
using msg_id_t = std::uint32_t;
35-
using acc_t = std::atomic<msg_id_t>;
34+
using msg_id_t = std::uint32_t;
35+
using acc_t = std::atomic<msg_id_t>;
3636

3737
template <std::size_t DataSize, std::size_t AlignSize>
3838
struct msg_t;
@@ -492,8 +492,8 @@ static ipc::buff_t recv(ipc::handle_t h, std::size_t tm) {
492492
if (buf != nullptr) {
493493
if (recycled) {
494494
return ipc::buff_t{buf, msg_size, [](void* pmid, std::size_t size) {
495-
release_storage(reinterpret_cast<ipc::storage_id_t>(pmid) - 1, size);
496-
}, reinterpret_cast<void*>(buf_id + 1)};
495+
release_storage(ipc::detail::horrible_cast<ipc::storage_id_t>(pmid) - 1, size);
496+
}, ipc::detail::horrible_cast<void*>(buf_id + 1)};
497497
} else {
498498
return ipc::buff_t{buf, msg_size}; // no recycle
499499
}

src/libipc/platform/detail.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,17 @@ constexpr const T& (min)(const T& a, const T& b) {
111111

112112
#endif/*__cplusplus < 201703L*/
113113

114+
template <typename T, typename U>
115+
auto horrible_cast(U rhs) noexcept
116+
-> typename std::enable_if<std::is_trivially_copyable<T>::value
117+
&& std::is_trivially_copyable<U>::value, T>::type {
118+
union {
119+
T t;
120+
U u;
121+
} r = {};
122+
r.u = rhs;
123+
return r.t;
124+
}
125+
114126
} // namespace detail
115127
} // namespace ipc

0 commit comments

Comments
 (0)