Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
5da8005458453fc74b7443c0c2c0fa9c93672e97
[simgrid.git] / src / mc / remote / RemotePtr.hpp
1 /* Copyright (c) 2008-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_REMOTE_PTR_HPP
7 #define SIMGRID_MC_REMOTE_PTR_HPP
8
9 #include "src/simix/smx_private.hpp"
10
11 namespace simgrid {
12 namespace mc {
13
14 /** HACK, A value from another process
15  *
16  *  This represents a value from another process:
17  *
18  *  * constructor/destructor are disabled;
19  *
20  *  * raw memory copy (std::memcpy) is used to copy Remote<T>;
21  *
22  *  * when T is a trivial type, Remote is convertible to a T.
23  *
24  *  We currently only handle the case where the type has the same layout
25  *  in the current process and in the target process: we don't handle
26  *  cross-architecture (such as 32-bit/64-bit access).
27  */
28 template <class T> class Remote {
29 private:
30   typename std::aligned_storage<sizeof(T), alignof(T)>::type buffer;
31
32 public:
33   Remote() = default;
34   explicit Remote(T const& p) { std::memcpy(&buffer, &p, sizeof buffer); }
35
36   T* get_buffer() { return reinterpret_cast<T*>(&buffer); }
37   const T* get_buffer() const { return reinterpret_cast<const T*>(&buffer); }
38   std::size_t get_buffer_size() const { return sizeof(T); }
39   operator T() const
40   {
41     static_assert(std::is_trivial<T>::value, "Cannot convert non trivial type");
42     return *get_buffer();
43   }
44   void clear() { std::memset(&buffer, 0, sizeof buffer); }
45 };
46
47 /** Pointer to a remote address-space (process, snapshot)
48  *
49  *  With this we can clearly identify the expected type of an address in the
50  *  remote process while avoiding to use native local pointers.
51  *
52  *  Some operators (+/-) assume use the size of the underlying element. This
53  *  only works if the target applications is using the same target: it won't
54  *  work for example, when inspecting a 32 bit application from a 64 bit
55  *  model-checker.
56  *
57  *  We do not actually store the target address space because we can
58  *  always detect it in context. This way `RemotePtr` is as efficient
59  *  as a `uint64_t`.
60  */
61 template <class T> class RemotePtr {
62   std::uint64_t address_;
63
64 public:
65   RemotePtr() : address_(0) {}
66   explicit RemotePtr(std::nullptr_t) : address_(0) {}
67   explicit RemotePtr(std::uint64_t address) : address_(address) {}
68   explicit RemotePtr(T* address) : address_((std::uintptr_t)address) {}
69   explicit RemotePtr(Remote<T*> p) : address_((std::uintptr_t)*p.get_buffer()) {}
70   std::uint64_t address() const { return address_; }
71
72   /** Turn into a local pointer
73    *
74    (if the remote process is not, in fact, remote) */
75   T* local() const { return (T*)address_; }
76
77   operator bool() const { return address_; }
78   bool operator!() const { return not address_; }
79   operator RemotePtr<void>() const { return RemotePtr<void>(address_); }
80   RemotePtr<T>& operator=(std::nullptr_t)
81   {
82     address_ = 0;
83     return *this;
84   }
85   RemotePtr<T> operator+(std::uint64_t n) const { return RemotePtr<T>(address_ + n * sizeof(T)); }
86   RemotePtr<T> operator-(std::uint64_t n) const { return RemotePtr<T>(address_ - n * sizeof(T)); }
87   RemotePtr<T>& operator+=(std::uint64_t n)
88   {
89     address_ += n * sizeof(T);
90     return *this;
91   }
92   RemotePtr<T>& operator-=(std::uint64_t n)
93   {
94     address_ -= n * sizeof(T);
95     return *this;
96   }
97 };
98
99 template <class X, class Y> bool operator<(RemotePtr<X> const& x, RemotePtr<Y> const& y)
100 {
101   return x.address() < y.address();
102 }
103
104 template <class X, class Y> bool operator>(RemotePtr<X> const& x, RemotePtr<Y> const& y)
105 {
106   return x.address() > y.address();
107 }
108
109 template <class X, class Y> bool operator>=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
110 {
111   return x.address() >= y.address();
112 }
113
114 template <class X, class Y> bool operator<=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
115 {
116   return x.address() <= y.address();
117 }
118
119 template <class X, class Y> bool operator==(RemotePtr<X> const& x, RemotePtr<Y> const& y)
120 {
121   return x.address() == y.address();
122 }
123
124 template <class X, class Y> bool operator!=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
125 {
126   return x.address() != y.address();
127 }
128
129 template <class T> inline RemotePtr<T> remote(T* p)
130 {
131   return RemotePtr<T>(p);
132 }
133
134 template <class T = void> inline RemotePtr<T> remote(uint64_t p)
135 {
136   return RemotePtr<T>(p);
137 }
138 }
139 }
140
141 #endif