Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Clang needs pointers to be explicitely casted here.
[simgrid.git] / src / mc / remote / RemotePtr.hpp
1 /* Copyright (c) 2008-2017. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #ifndef SIMGRID_MC_REMOTE_PTR_HPP
8 #define SIMGRID_MC_REMOTE_PTR_HPP
9
10 #include <cstddef>
11 #include <cstdint>
12 #include <cstring>
13
14 #include <stdexcept>
15 #include <type_traits>
16
17 namespace simgrid {
18 namespace mc {
19
20 /** HACK, A value from another process
21  *
22  *  This represents a value from another process:
23  *
24  *  * constructor/destructor are disabled;
25  *
26  *  * raw memory copy (std::memcpy) is used to copy Remote<T>;
27  *
28  *  * raw memory comparison is used to compare them;
29  *
30  *  * when T is a trivial type, Remote is convertible to a T.
31  *
32  *  We currently only handle the case where the type has the same layout
33  *  in the current process and in the target process: we don't handle
34  *  cross-architecture (such as 32-bit/64-bit access).
35  */
36 template <class T> union Remote {
37 private:
38   T buffer;
39
40 public:
41   Remote() { /* Nothing to do */}
42   ~Remote() { /* Nothing to do */}
43   Remote(T const& p) { std::memcpy(static_cast<void*>(&buffer), static_cast<const void*>(&p), sizeof(buffer)); }
44   Remote(Remote const& that)
45   {
46     std::memcpy(static_cast<void*>(&buffer), static_cast<const void*>(&that.buffer), sizeof(buffer));
47   }
48   Remote& operator=(Remote const& that)
49   {
50     std::memcpy(static_cast<void*>(&buffer), static_cast<const void*>(&that.buffer), sizeof(buffer));
51     return *this;
52   }
53   T* getBuffer() { return &buffer; }
54   const T* getBuffer() const { return &buffer; }
55   std::size_t getBufferSize() const { return sizeof(T); }
56   operator T() const
57   {
58 //FIXME: assert turned off because smpi:Request is not seen as "trivial".
59 //    static_assert(std::is_trivial<T>::value, "Cannot convert non trivial type");
60     return buffer;
61   }
62   void clear() { std::memset(static_cast<void*>(&buffer), 0, sizeof(T)); }
63 };
64
65 /** Pointer to a remote address-space (process, snapshot)
66  *
67  *  With this we can clearly identify the expected type of an address in the
68  *  remote process while avoiding to use native local pointers.
69  *
70  *  Some operators (+/-) assume use the size of the underlying element. This
71  *  only works if the target applications is using the same target: it won't
72  *  work for example, when inspecting a 32 bit application from a 64 bit
73  *  model-checker.
74  *
75  *  We do not actually store the target address space because we can
76  *  always detect it in context. This way `RemotePtr` is as efficient
77  *  as a `uint64_t`.
78  */
79 template <class T> class RemotePtr {
80   std::uint64_t address_;
81
82 public:
83   RemotePtr() : address_(0) {}
84   RemotePtr(std::uint64_t address) : address_(address) {}
85   RemotePtr(T* address) : address_((std::uintptr_t)address) {}
86   RemotePtr(Remote<T*> p) : RemotePtr(*p.getBuffer()) {}
87   std::uint64_t address() const { return address_; }
88
89   /** Turn into a local pointer
90    *
91    (if the remote process is not, in fact, remote) */
92   T* local() const { return (T*)address_; }
93
94   operator bool() const { return address_; }
95   bool operator!() const { return not address_; }
96   operator RemotePtr<void>() const { return RemotePtr<void>(address_); }
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