1 /* Copyright (c) 2008-2018. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 #ifndef SIMGRID_MC_REMOTE_PTR_HPP
8 #define SIMGRID_MC_REMOTE_PTR_HPP
15 #include <type_traits>
20 /** HACK, A value from another process
22 * This represents a value from another process:
24 * * constructor/destructor are disabled;
26 * * raw memory copy (std::memcpy) is used to copy Remote<T>;
28 * * raw memory comparison is used to compare them;
30 * * when T is a trivial type, Remote is convertible to a T.
32 * We currently only handle the case where the type has the same layout
33 * in the current process and in the target process: we don't handle
34 * cross-architecture (such as 32-bit/64-bit access).
36 template <class T> union Remote {
41 Remote() { /* Nothing to do */}
42 ~Remote() { /* Nothing to do */}
43 Remote(T const& p) { std::memcpy(static_cast<void*>(&buffer), static_cast<const void*>(&p), sizeof(buffer)); }
44 Remote(Remote const& that)
46 std::memcpy(static_cast<void*>(&buffer), static_cast<const void*>(&that.buffer), sizeof(buffer));
48 Remote& operator=(Remote const& that)
50 std::memcpy(static_cast<void*>(&buffer), static_cast<const void*>(&that.buffer), sizeof(buffer));
53 T* getBuffer() { return &buffer; }
54 const T* getBuffer() const { return &buffer; }
55 std::size_t getBufferSize() const { return sizeof(T); }
58 //FIXME: assert turned off because smpi:Request is not seen as "trivial".
59 // static_assert(std::is_trivial<T>::value, "Cannot convert non trivial type");
62 void clear() { std::memset(static_cast<void*>(&buffer), 0, sizeof(T)); }
65 /** Pointer to a remote address-space (process, snapshot)
67 * With this we can clearly identify the expected type of an address in the
68 * remote process while avoiding to use native local pointers.
70 * Some operators (+/-) assume use the size of the underlying element. This
71 * only works if the target applications is using the same target: it won't
72 * work for example, when inspecting a 32 bit application from a 64 bit
75 * We do not actually store the target address space because we can
76 * always detect it in context. This way `RemotePtr` is as efficient
79 template <class T> class RemotePtr {
80 std::uint64_t address_;
83 RemotePtr() : address_(0) {}
84 explicit RemotePtr(std::nullptr_t) : address_(0) {}
85 explicit RemotePtr(std::uint64_t address) : address_(address) {}
86 explicit RemotePtr(T* address) : address_((std::uintptr_t)address) {}
87 explicit RemotePtr(Remote<T*> p) : address_((std::uintptr_t)*p.getBuffer()) {}
88 std::uint64_t address() const { return address_; }
90 /** Turn into a local pointer
92 (if the remote process is not, in fact, remote) */
93 T* local() const { return (T*)address_; }
95 operator bool() const { return address_; }
96 bool operator!() const { return not address_; }
97 operator RemotePtr<void>() const { return RemotePtr<void>(address_); }
98 RemotePtr<T>& operator=(std::nullptr_t)
103 RemotePtr<T> operator+(std::uint64_t n) const { return RemotePtr<T>(address_ + n * sizeof(T)); }
104 RemotePtr<T> operator-(std::uint64_t n) const { return RemotePtr<T>(address_ - n * sizeof(T)); }
105 RemotePtr<T>& operator+=(std::uint64_t n)
107 address_ += n * sizeof(T);
110 RemotePtr<T>& operator-=(std::uint64_t n)
112 address_ -= n * sizeof(T);
117 template <class X, class Y> bool operator<(RemotePtr<X> const& x, RemotePtr<Y> const& y)
119 return x.address() < y.address();
122 template <class X, class Y> bool operator>(RemotePtr<X> const& x, RemotePtr<Y> const& y)
124 return x.address() > y.address();
127 template <class X, class Y> bool operator>=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
129 return x.address() >= y.address();
132 template <class X, class Y> bool operator<=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
134 return x.address() <= y.address();
137 template <class X, class Y> bool operator==(RemotePtr<X> const& x, RemotePtr<Y> const& y)
139 return x.address() == y.address();
142 template <class X, class Y> bool operator!=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
144 return x.address() != y.address();
147 template <class T> inline RemotePtr<T> remote(T* p)
149 return RemotePtr<T>(p);
152 template <class T = void> inline RemotePtr<T> remote(uint64_t p)
154 return RemotePtr<T>(p);