Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
de935729877bae68ed5b555a3068ff81ad6722b9
[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  *  * raw memory comparison is used to compare them;
23  *
24  *  * when T is a trivial type, Remote is convertible to a T.
25  *
26  *  We currently only handle the case where the type has the same layout
27  *  in the current process and in the target process: we don't handle
28  *  cross-architecture (such as 32-bit/64-bit access).
29  */
30 template <class T> union Remote {
31 private:
32   T buffer;
33
34 public:
35   Remote() { /* Nothing to do */}
36   ~Remote() { /* Nothing to do */}
37   Remote(T const& p) { std::memcpy(static_cast<void*>(&buffer), static_cast<const void*>(&p), sizeof(buffer)); }
38   Remote(Remote const& that)
39   {
40     std::memcpy(static_cast<void*>(&buffer), static_cast<const void*>(&that.buffer), sizeof(buffer));
41   }
42   Remote& operator=(Remote const& that)
43   {
44     std::memcpy(static_cast<void*>(&buffer), static_cast<const void*>(&that.buffer), sizeof(buffer));
45     return *this;
46   }
47   T* getBuffer() { return &buffer; }
48   const T* getBuffer() const { return &buffer; }
49   std::size_t getBufferSize() const { return sizeof(T); }
50   operator T() const
51   {
52     static_assert(std::is_trivial<T>::value, "Cannot convert non trivial type");
53     return buffer;
54   }
55   void clear() { std::memset(static_cast<void*>(&buffer), 0, sizeof(T)); }
56 };
57
58 /** Pointer to a remote address-space (process, snapshot)
59  *
60  *  With this we can clearly identify the expected type of an address in the
61  *  remote process while avoiding to use native local pointers.
62  *
63  *  Some operators (+/-) assume use the size of the underlying element. This
64  *  only works if the target applications is using the same target: it won't
65  *  work for example, when inspecting a 32 bit application from a 64 bit
66  *  model-checker.
67  *
68  *  We do not actually store the target address space because we can
69  *  always detect it in context. This way `RemotePtr` is as efficient
70  *  as a `uint64_t`.
71  */
72 template <class T> class RemotePtr {
73   std::uint64_t address_;
74
75 public:
76   RemotePtr() : address_(0) {}
77   explicit RemotePtr(std::nullptr_t) : address_(0) {}
78   explicit RemotePtr(std::uint64_t address) : address_(address) {}
79   explicit RemotePtr(T* address) : address_((std::uintptr_t)address) {}
80   explicit RemotePtr(Remote<T*> p) : address_((std::uintptr_t)*p.getBuffer()) {}
81   std::uint64_t address() const { return address_; }
82
83   /** Turn into a local pointer
84    *
85    (if the remote process is not, in fact, remote) */
86   T* local() const { return (T*)address_; }
87
88   operator bool() const { return address_; }
89   bool operator!() const { return not address_; }
90   operator RemotePtr<void>() const { return RemotePtr<void>(address_); }
91   RemotePtr<T>& operator=(std::nullptr_t)
92   {
93     address_ = 0;
94     return *this;
95   }
96   RemotePtr<T> operator+(std::uint64_t n) const { return RemotePtr<T>(address_ + n * sizeof(T)); }
97   RemotePtr<T> operator-(std::uint64_t n) const { return RemotePtr<T>(address_ - n * sizeof(T)); }
98   RemotePtr<T>& operator+=(std::uint64_t n)
99   {
100     address_ += n * sizeof(T);
101     return *this;
102   }
103   RemotePtr<T>& operator-=(std::uint64_t n)
104   {
105     address_ -= n * sizeof(T);
106     return *this;
107   }
108 };
109
110 template <class X, class Y> bool operator<(RemotePtr<X> const& x, RemotePtr<Y> const& y)
111 {
112   return x.address() < y.address();
113 }
114
115 template <class X, class Y> bool operator>(RemotePtr<X> const& x, RemotePtr<Y> const& y)
116 {
117   return x.address() > y.address();
118 }
119
120 template <class X, class Y> bool operator>=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
121 {
122   return x.address() >= y.address();
123 }
124
125 template <class X, class Y> bool operator<=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
126 {
127   return x.address() <= y.address();
128 }
129
130 template <class X, class Y> bool operator==(RemotePtr<X> const& x, RemotePtr<Y> const& y)
131 {
132   return x.address() == y.address();
133 }
134
135 template <class X, class Y> bool operator!=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
136 {
137   return x.address() != y.address();
138 }
139
140 template <class T> inline RemotePtr<T> remote(T* p)
141 {
142   return RemotePtr<T>(p);
143 }
144
145 template <class T = void> inline RemotePtr<T> remote(uint64_t p)
146 {
147   return RemotePtr<T>(p);
148 }
149 }
150 }
151
152 #endif