1 /* Copyright (c) 2008-2015. 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 {
43 Remote(T& p) { std::memcpy(&buffer, &p, sizeof(buffer)); }
44 Remote(Remote const& that) { std::memcpy(&buffer, &that.buffer, sizeof(buffer)); }
45 Remote& operator=(Remote const& that)
47 std::memcpy(&buffer, &that.buffer, sizeof(buffer));
50 T* getBuffer() { return &buffer; }
51 const T* getBuffer() const { return &buffer; }
52 std::size_t getBufferSize() const { return sizeof(T); }
55 static_assert(std::is_trivial<T>::value, "Cannot convert non trivial type");
58 void clear() { std::memset(static_cast<void*>(&buffer), 0, sizeof(T)); }
61 /** Pointer to a remote address-space (process, snapshot)
63 * With this we can clearly identify the expected type of an address in the
64 * remote process while avoiding to use native local pointers.
66 * Some operators (+/-) assume use the size of the underlying element. This
67 * only works if the target applications is using the same target: it won't
68 * work for example, when inspecting a 32 bit application from a 64 bit
71 * We do not actually store the target address space because we can
72 * always detect it in context. This way `RemotePtr` is as efficient
75 template <class T> class RemotePtr {
76 std::uint64_t address_;
79 RemotePtr() : address_(0) {}
80 RemotePtr(std::uint64_t address) : address_(address) {}
81 RemotePtr(T* address) : address_((std::uintptr_t)address) {}
82 RemotePtr(Remote<T*> p) : RemotePtr(*p.getBuffer()) {}
83 std::uint64_t address() const { return address_; }
85 /** Turn into a local pointer
87 (if the remote process is not, in fact, remote) */
88 T* local() const { return (T*)address_; }
90 operator bool() const { return address_; }
91 bool operator!() const { return !address_; }
92 operator RemotePtr<void>() const { return RemotePtr<void>(address_); }
93 RemotePtr<T> operator+(std::uint64_t n) const { return RemotePtr<T>(address_ + n * sizeof(T)); }
94 RemotePtr<T> operator-(std::uint64_t n) const { return RemotePtr<T>(address_ - n * sizeof(T)); }
95 RemotePtr<T>& operator+=(std::uint64_t n)
97 address_ += n * sizeof(T);
100 RemotePtr<T>& operator-=(std::uint64_t n)
102 address_ -= n * sizeof(T);
107 template <class X, class Y> bool operator<(RemotePtr<X> const& x, RemotePtr<Y> const& y)
109 return x.address() < y.address();
112 template <class X, class Y> bool operator>(RemotePtr<X> const& x, RemotePtr<Y> const& y)
114 return x.address() > y.address();
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 T> inline RemotePtr<T> remote(T* p)
139 return RemotePtr<T>(p);
142 template <class T = void> inline RemotePtr<T> remote(uint64_t p)
144 return RemotePtr<T>(p);