From 7069d2b15a2de8a73639b24d9016f8d964645523 Mon Sep 17 00:00:00 2001 From: Arnaud Giersch Date: Mon, 6 May 2019 11:41:39 +0200 Subject: [PATCH] Use aligned storage and remove usage of union. I'm not sure about potential UB, but it should not be worse than before. --- src/mc/remote/RemotePtr.hpp | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/src/mc/remote/RemotePtr.hpp b/src/mc/remote/RemotePtr.hpp index de93572987..6b367fdab7 100644 --- a/src/mc/remote/RemotePtr.hpp +++ b/src/mc/remote/RemotePtr.hpp @@ -7,6 +7,7 @@ #define SIMGRID_MC_REMOTE_PTR_HPP #include "src/simix/smx_private.hpp" +#include namespace simgrid { namespace mc { @@ -19,40 +20,29 @@ namespace mc { * * * raw memory copy (std::memcpy) is used to copy Remote; * - * * raw memory comparison is used to compare them; - * * * when T is a trivial type, Remote is convertible to a T. * * We currently only handle the case where the type has the same layout * in the current process and in the target process: we don't handle * cross-architecture (such as 32-bit/64-bit access). */ -template union Remote { +template class Remote { private: - T buffer; + typename std::aligned_storage::type buffer; public: - Remote() { /* Nothing to do */} - ~Remote() { /* Nothing to do */} - Remote(T const& p) { std::memcpy(static_cast(&buffer), static_cast(&p), sizeof(buffer)); } - Remote(Remote const& that) - { - std::memcpy(static_cast(&buffer), static_cast(&that.buffer), sizeof(buffer)); - } - Remote& operator=(Remote const& that) - { - std::memcpy(static_cast(&buffer), static_cast(&that.buffer), sizeof(buffer)); - return *this; - } - T* getBuffer() { return &buffer; } - const T* getBuffer() const { return &buffer; } + Remote() = default; + Remote(T const& p) { std::memcpy(&buffer, &p, sizeof buffer); } + + T* getBuffer() { return reinterpret_cast(&buffer); } + const T* getBuffer() const { return reinterpret_cast(&buffer); } std::size_t getBufferSize() const { return sizeof(T); } operator T() const { static_assert(std::is_trivial::value, "Cannot convert non trivial type"); - return buffer; + return *getBuffer(); } - void clear() { std::memset(static_cast(&buffer), 0, sizeof(T)); } + void clear() { std::memset(&buffer, 0, sizeof buffer); } }; /** Pointer to a remote address-space (process, snapshot) -- 2.20.1