Skip to content

Commit 74f0803

Browse files
committed
horrible_cast
1 parent 7c063f8 commit 74f0803

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
@@ -32,8 +32,8 @@
3232

3333
namespace {
3434

35-
using msg_id_t = std::uint32_t;
36-
using acc_t = std::atomic<msg_id_t>;
35+
using msg_id_t = std::uint32_t;
36+
using acc_t = std::atomic<msg_id_t>;
3737

3838
template <std::size_t DataSize, std::size_t AlignSize>
3939
struct msg_t;
@@ -505,8 +505,8 @@ static ipc::buff_t recv(ipc::handle_t h, std::size_t tm) {
505505
if (buf != nullptr) {
506506
if (recycled) {
507507
return ipc::buff_t{buf, msg_size, [](void* pmid, std::size_t size) {
508-
release_storage(reinterpret_cast<ipc::storage_id_t>(pmid) - 1, size);
509-
}, reinterpret_cast<void*>(buf_id + 1)};
508+
release_storage(ipc::detail::horrible_cast<ipc::storage_id_t>(pmid) - 1, size);
509+
}, ipc::detail::horrible_cast<void*>(buf_id + 1)};
510510
} else {
511511
return ipc::buff_t{buf, msg_size}; // no recycle
512512
}

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)