mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-07 01:06:45 +08:00
horrible_cast
This commit is contained in:
parent
7c063f8c17
commit
74f080361e
@ -32,8 +32,8 @@
|
|||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
using msg_id_t = std::uint32_t;
|
using msg_id_t = std::uint32_t;
|
||||||
using acc_t = std::atomic<msg_id_t>;
|
using acc_t = std::atomic<msg_id_t>;
|
||||||
|
|
||||||
template <std::size_t DataSize, std::size_t AlignSize>
|
template <std::size_t DataSize, std::size_t AlignSize>
|
||||||
struct msg_t;
|
struct msg_t;
|
||||||
@ -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
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user