Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Move the stack as field of SafetyChecker and CommDetChecker
[simgrid.git] / src / mc / RemotePtr.hpp
1 /* Copyright (c) 2008-2015. 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 <cstdint>
11
12 namespace simgrid {
13 namespace mc {
14
15 /** Pointer to a remote address-space (process, snapshot)
16  *
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.
19  *
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
23  *  model-checker.
24  *
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
27  *  as a `uint64_t`.
28  */
29 template<class T> class RemotePtr {
30   std::uint64_t address_;
31 public:
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_; }
36
37   operator bool() const
38   {
39     return address_;
40   }
41   bool operator!() const
42   {
43     return !address_;
44   }
45   operator RemotePtr<void>() const
46   {
47     return RemotePtr<void>(address_);
48   }
49   RemotePtr<T> operator+(std::uint64_t n) const
50   {
51     return RemotePtr<T>(address_ + n * sizeof(T));
52   }
53   RemotePtr<T> operator-(std::uint64_t n) const
54   {
55     return RemotePtr<T>(address_ - n * sizeof(T));
56   }
57   RemotePtr<T>& operator+=(std::uint64_t n)
58   {
59     address_ += n * sizeof(T);
60     return *this;
61   }
62   RemotePtr<T>& operator-=(std::uint64_t n)
63   {
64     address_ -= n * sizeof(T);
65     return *this;
66   }
67 };
68
69 template<class X, class Y>
70 bool operator<(RemotePtr<X> const& x, RemotePtr<Y> const& y)
71 {
72   return x.address() < y.address();
73 }
74
75 template<class X, class Y>
76 bool operator>(RemotePtr<X> const& x, RemotePtr<Y> const& y)
77 {
78   return x.address() > y.address();
79 }
80
81 template<class X, class Y>
82 bool operator>=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
83 {
84   return x.address() >= y.address();
85 }
86
87 template<class X, class Y>
88 bool operator<=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
89 {
90   return x.address() <= y.address();
91 }
92
93 template<class X, class Y>
94 bool operator==(RemotePtr<X> const& x, RemotePtr<Y> const& y)
95 {
96   return x.address() == y.address();
97 }
98
99 template<class X, class Y>
100 bool operator!=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
101 {
102   return x.address() != y.address();
103 }
104
105 template<class T> inline
106 RemotePtr<T> remote(T *p)
107 {
108   return RemotePtr<T>(p);
109 }
110
111 template<class T=void> inline
112 RemotePtr<T> remote(uint64_t p)
113 {
114   return RemotePtr<T>(p);
115 }
116
117 }
118 }
119
120 #endif