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 /** Pointer to a remote address-space (process, snapshot)
17 * With this we can clearly identify the expected type of an address in the
18 * remote process while avoiding to use native local pointers.
20 * Some operators (+/-) assume use the size of the underlying element. This
21 * only works if the target applications is using the same target: it won't
22 * work for example, when inspecting a 32 bit application from a 64 bit
25 * We do not actually store the target address space because we can
26 * always detect it in context. This way `RemotePtr` is as efficient
29 template<class T> class RemotePtr {
30 std::uint64_t address_;
32 RemotePtr() : address_(0) {}
33 RemotePtr(std::uint64_t address) : address_(address) {}
34 RemotePtr(T* address) : address_((std::uintptr_t)address) {}
35 std::uint64_t address() const { return address_; }
37 /** Turn into a local pointer
39 (if the remote process is not, in fact, remote) */
40 T* local() { return (T*) address_; }
46 bool operator!() const
50 operator RemotePtr<void>() const
52 return RemotePtr<void>(address_);
54 RemotePtr<T> operator+(std::uint64_t n) const
56 return RemotePtr<T>(address_ + n * sizeof(T));
58 RemotePtr<T> operator-(std::uint64_t n) const
60 return RemotePtr<T>(address_ - n * sizeof(T));
62 RemotePtr<T>& operator+=(std::uint64_t n)
64 address_ += n * sizeof(T);
67 RemotePtr<T>& operator-=(std::uint64_t n)
69 address_ -= n * sizeof(T);
74 template<class X, class Y>
75 bool operator<(RemotePtr<X> const& x, RemotePtr<Y> const& y)
77 return x.address() < y.address();
80 template<class X, class Y>
81 bool operator>(RemotePtr<X> const& x, RemotePtr<Y> const& y)
83 return x.address() > y.address();
86 template<class X, class Y>
87 bool operator>=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
89 return x.address() >= y.address();
92 template<class X, class Y>
93 bool operator<=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
95 return x.address() <= y.address();
98 template<class X, class Y>
99 bool operator==(RemotePtr<X> const& x, RemotePtr<Y> const& y)
101 return x.address() == y.address();
104 template<class X, class Y>
105 bool operator!=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
107 return x.address() != y.address();
110 template<class T> inline
111 RemotePtr<T> remote(T *p)
113 return RemotePtr<T>(p);
116 template<class T=void> inline
117 RemotePtr<T> remote(uint64_t p)
119 return RemotePtr<T>(p);