horrible_cast

This commit is contained in:
mutouyun 2021-06-27 18:24:30 +08:00
parent 7c063f8c17
commit 74f080361e
2 changed files with 16 additions and 4 deletions

View File

@ -505,8 +505,8 @@ static ipc::buff_t recv(ipc::handle_t h, std::size_t tm) {
if (buf != nullptr) { if (buf != nullptr) {
if (recycled) { if (recycled) {
return ipc::buff_t{buf, msg_size, [](void* pmid, std::size_t size) { return ipc::buff_t{buf, msg_size, [](void* pmid, std::size_t size) {
release_storage(reinterpret_cast<ipc::storage_id_t>(pmid) - 1, size); release_storage(ipc::detail::horrible_cast<ipc::storage_id_t>(pmid) - 1, size);
}, reinterpret_cast<void*>(buf_id + 1)}; }, ipc::detail::horrible_cast<void*>(buf_id + 1)};
} else { } else {
return ipc::buff_t{buf, msg_size}; // no recycle return ipc::buff_t{buf, msg_size}; // no recycle
} }

View File

@ -111,5 +111,17 @@ constexpr const T& (min)(const T& a, const T& b) {
#endif/*__cplusplus < 201703L*/ #endif/*__cplusplus < 201703L*/
template <typename T, typename U>
auto horrible_cast(U rhs) noexcept
-> typename std::enable_if<std::is_trivially_copyable<T>::value
&& std::is_trivially_copyable<U>::value, T>::type {
union {
T t;
U u;
} r = {};
r.u = rhs;
return r.t;
}
} // namespace detail } // namespace detail
} // namespace ipc } // namespace ipc