diff --git a/include/def.h b/include/def.h index 2a17ecc..9571382 100644 --- a/include/def.h +++ b/include/def.h @@ -46,8 +46,7 @@ using uint_t = typename uint::type; enum : std::size_t { invalid_value = (std::numeric_limits::max)(), - data_length = 16, - name_length = 64 + data_length = 64 }; enum class relat { // multiplicity of the relationship diff --git a/src/ipc.cpp b/src/ipc.cpp index e04ef9b..adc8961 100644 --- a/src/ipc.cpp +++ b/src/ipc.cpp @@ -18,7 +18,7 @@ namespace { using namespace ipc; -using data_t = byte_t[data_length]; + using msg_id_t = std::size_t; inline auto acc_of_msg() { @@ -26,19 +26,30 @@ inline auto acc_of_msg() { return static_cast*>(g_shm.get()); } -template -struct detail_impl { +template = 201703L + std::size_t AlignSize = (std::min)(DataSize, alignof(std::size_t))> +# else /*__cplusplus < 201703L*/ + std::size_t AlignSize = (DataSize < alignof(std::size_t)) ? DataSize : alignof(std::size_t)> +# endif/*__cplusplus < 201703L*/ +struct msg_t; -#pragma pack(1) -struct msg_t { +template +struct msg_t<0, AlignSize> { void* que_; msg_id_t id_; int remain_; - data_t data_; }; -#pragma pack() -using queue_t = ipc::queue; +template +struct msg_t : msg_t<0, AlignSize> { + alignas(AlignSize) byte_t data_[DataSize]; +}; + +template +struct detail_impl { + +using queue_t = ipc::queue, Policy>; constexpr static void* head_of(queue_t* que) { return static_cast(que->elems()); @@ -131,12 +142,11 @@ static bool send(ipc::handle_t h, void const * data, std::size_t size) { } // calc a new message id, start with 1 auto msg_id = acc_of_msg()->fetch_add(1, std::memory_order_relaxed) + 1; - // push message fragment, one fragment size is data_length + // push message fragment int offset = 0; for (int i = 0; i < static_cast(size / data_length); ++i, offset += data_length) { - msg_t msg { - que, msg_id, - static_cast(size) - offset - static_cast(data_length), {} + msg_t msg { + { que, msg_id, static_cast(size) - offset - static_cast(data_length) }, {} }; std::memcpy(msg.data_, static_cast(data) + offset, data_length); if (!que->push(msg)) return false; @@ -144,9 +154,8 @@ static bool send(ipc::handle_t h, void const * data, std::size_t size) { // if remain > 0, this is the last message fragment int remain = static_cast(size) - offset; if (remain > 0) { - msg_t msg { - que, msg_id, - remain - static_cast(data_length), {} + msg_t msg { + { que, msg_id, remain - static_cast(data_length) }, {} }; std::memcpy(msg.data_, static_cast(data) + offset, static_cast(remain)); diff --git a/src/queue.h b/src/queue.h index 4d67051..7042769 100644 --- a/src/queue.h +++ b/src/queue.h @@ -240,7 +240,9 @@ public: return base_t::template push(std::forward

(params)...); } - T pop() { return base_t::template pop(); } + T pop() { + return base_t::template pop(); + } }; } // namespace ipc