Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines with new year.
[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 //FIXME: assert turned off because smpi:Request is not seen as "trivial".
53 //    static_assert(std::is_trivial<T>::value, "Cannot convert non trivial type");
54     return buffer;
55   }
56   void clear() { std::memset(static_cast<void*>(&buffer), 0, sizeof(T)); }
57 };
58
59 /** Pointer to a remote address-space (process, snapshot)
60  *
61  *  With this we can clearly identify the expected type of an address in the
62  *  remote process while avoiding to use native local pointers.
63  *
64  *  Some operators (+/-) assume use the size of the underlying element. This
65  *  only works if the target applications is using the same target: it won't
66  *  work for example, when inspecting a 32 bit application from a 64 bit
67  *  model-checker.
68  *
69  *  We do not actually store the target address space because we can
70  *  always detect it in context. This way `RemotePtr` is as efficient
71  *  as a `uint64_t`.
72  */
73 template <class T> class RemotePtr {
74   std::uint64_t address_;
75
76 public:
77   RemotePtr() : address_(0) {}
78   explicit RemotePtr(std::nullptr_t) : address_(0) {}
79   explicit RemotePtr(std::uint64_t address) : address_(address) {}
80   explicit RemotePtr(T* address) : address_((std::uintptr_t)address) {}
81   explicit RemotePtr(Remote<T*> p) : address_((std::uintptr_t)*p.getBuffer()) {}
82   std::uint64_t address() const { return address_; }
83
84   /** Turn into a local pointer
85    *
86    (if the remote process is not, in fact, remote) */
87   T* local() const { return (T*)address_; }
88
89   operator bool() const { return address_; }
90   bool operator!() const { return not address_; }
91   operator RemotePtr<void>() const { return RemotePtr<void>(address_); }
92   RemotePtr<T>& operator=(std::nullptr_t)
93   {
94     address_ = 0;
95     return *this;
96   }
97   RemotePtr<T> operator+(std::uint64_t n) const { return RemotePtr<T>(address_ + n * sizeof(T)); }
98   RemotePtr<T> operator-(std::uint64_t n) const { return RemotePtr<T>(address_ - n * sizeof(T)); }
99   RemotePtr<T>& operator+=(std::uint64_t n)
100   {
101     address_ += n * sizeof(T);
102     return *this;
103   }
104   RemotePtr<T>& operator-=(std::uint64_t n)
105   {
106     address_ -= n * sizeof(T);
107     return *this;
108   }
109 };
110
111 template <class X, class Y> bool operator<(RemotePtr<X> const& x, RemotePtr<Y> const& y)
112 {
113   return x.address() < y.address();
114 }
115
116 template <class X, class Y> bool operator>(RemotePtr<X> const& x, RemotePtr<Y> const& y)
117 {
118   return x.address() > y.address();
119 }
120
121 template <class X, class Y> bool operator>=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
122 {
123   return x.address() >= y.address();
124 }
125
126 template <class X, class Y> bool operator<=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
127 {
128   return x.address() <= y.address();
129 }
130
131 template <class X, class Y> bool operator==(RemotePtr<X> const& x, RemotePtr<Y> const& y)
132 {
133   return x.address() == y.address();
134 }
135
136 template <class X, class Y> bool operator!=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
137 {
138   return x.address() != y.address();
139 }
140
141 template <class T> inline RemotePtr<T> remote(T* p)
142 {
143   return RemotePtr<T>(p);
144 }
145
146 template <class T = void> inline RemotePtr<T> remote(uint64_t p)
147 {
148   return RemotePtr<T>(p);
149 }
150 }
151 }
152
153 #endif