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 template<class M, class T, class Enable = void>
21 struct pointer_to_data_member {};
22 template<class M, class T>
23 struct pointer_to_data_member<M,T,typename std::enable_if< std::is_union<M>::value || std::is_class<M>::value >::type> {
27 template<class M, class T>
28 using pointer_to_data_member_t = typename pointer_to_data_member<M,T>::type;
30 /** HACK, A value from another process
32 * This represents a value from another process:
34 * * constructor/destructor are disabled;
36 * * raw memory copy (std::memcpy) is used to copy Remote<T>;
38 * * raw memory comparison is used to compare them;
40 * * when T is a trivial type, Remote is convertible to a T.
42 * We currently only handle the case where the type has the same layout
43 * in the current process and in the target process: we don't handle
44 * cross-architecture (such as 32-bit/64-bit access).
55 std::memcpy(&buffer, &p, sizeof(buffer));
57 Remote(Remote const& that)
59 std::memcpy(&buffer, &that.buffer, sizeof(buffer));
61 Remote& operator=(Remote const& that)
63 std::memcpy(&buffer, &that.buffer, sizeof(buffer));
66 T* getBuffer() { return &buffer; }
67 const T* getBuffer() const { return &buffer; }
68 std::size_t getBufferSize() const { return sizeof(T); }
70 static_assert(std::is_trivial<T>::value, "Cannot convert non trivial type");
75 std::memset(static_cast<void*>(&buffer), 0, sizeof(T));
80 /** Pointer to a remote address-space (process, snapshot)
82 * With this we can clearly identify the expected type of an address in the
83 * remote process while avoiding to use native local pointers.
85 * Some operators (+/-) assume use the size of the underlying element. This
86 * only works if the target applications is using the same target: it won't
87 * work for example, when inspecting a 32 bit application from a 64 bit
90 * We do not actually store the target address space because we can
91 * always detect it in context. This way `RemotePtr` is as efficient
94 template<class T> class RemotePtr {
95 std::uint64_t address_;
97 RemotePtr() : address_(0) {}
98 RemotePtr(std::uint64_t address) : address_(address) {}
99 RemotePtr(T* address) : address_((std::uintptr_t)address) {}
100 RemotePtr(Remote<T*> p) : RemotePtr(*p.getBuffer()) {}
101 std::uint64_t address() const { return address_; }
103 /** Turn into a local pointer
105 (if the remote process is not, in fact, remote) */
106 T* local() const { return (T*) address_; }
108 operator bool() const
112 bool operator!() const
116 operator RemotePtr<void>() const
118 return RemotePtr<void>(address_);
120 RemotePtr<T> operator+(std::uint64_t n) const
122 return RemotePtr<T>(address_ + n * sizeof(T));
124 RemotePtr<T> operator-(std::uint64_t n) const
126 return RemotePtr<T>(address_ - n * sizeof(T));
128 RemotePtr<T>& operator+=(std::uint64_t n)
130 address_ += n * sizeof(T);
133 RemotePtr<T>& operator-=(std::uint64_t n)
135 address_ -= n * sizeof(T);
140 template<class X, class Y>
141 bool operator<(RemotePtr<X> const& x, RemotePtr<Y> const& y)
143 return x.address() < y.address();
146 template<class X, class Y>
147 bool operator>(RemotePtr<X> const& x, RemotePtr<Y> const& y)
149 return x.address() > y.address();
152 template<class X, class Y>
153 bool operator>=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
155 return x.address() >= y.address();
158 template<class X, class Y>
159 bool operator<=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
161 return x.address() <= y.address();
164 template<class X, class Y>
165 bool operator==(RemotePtr<X> const& x, RemotePtr<Y> const& y)
167 return x.address() == y.address();
170 template<class X, class Y>
171 bool operator!=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
173 return x.address() != y.address();
176 template<class T> inline
177 RemotePtr<T> remote(T *p)
179 return RemotePtr<T>(p);
182 template<class T=void> inline
183 RemotePtr<T> remote(uint64_t p)
185 return RemotePtr<T>(p);