Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Move MC_report() functions as static functions where they're used
[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   /** Turn into a local pointer
38    *
39    (if the remote process is not, in fact, remote) */
40   T* local() { return (T*) address_; }
41
42   operator bool() const
43   {
44     return address_;
45   }
46   bool operator!() const
47   {
48     return !address_;
49   }
50   operator RemotePtr<void>() const
51   {
52     return RemotePtr<void>(address_);
53   }
54   RemotePtr<T> operator+(std::uint64_t n) const
55   {
56     return RemotePtr<T>(address_ + n * sizeof(T));
57   }
58   RemotePtr<T> operator-(std::uint64_t n) const
59   {
60     return RemotePtr<T>(address_ - n * sizeof(T));
61   }
62   RemotePtr<T>& operator+=(std::uint64_t n)
63   {
64     address_ += n * sizeof(T);
65     return *this;
66   }
67   RemotePtr<T>& operator-=(std::uint64_t n)
68   {
69     address_ -= n * sizeof(T);
70     return *this;
71   }
72 };
73
74 template<class X, class Y>
75 bool operator<(RemotePtr<X> const& x, RemotePtr<Y> const& y)
76 {
77   return x.address() < y.address();
78 }
79
80 template<class X, class Y>
81 bool operator>(RemotePtr<X> const& x, RemotePtr<Y> const& y)
82 {
83   return x.address() > y.address();
84 }
85
86 template<class X, class Y>
87 bool operator>=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
88 {
89   return x.address() >= y.address();
90 }
91
92 template<class X, class Y>
93 bool operator<=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
94 {
95   return x.address() <= y.address();
96 }
97
98 template<class X, class Y>
99 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>
105 bool operator!=(RemotePtr<X> const& x, RemotePtr<Y> const& y)
106 {
107   return x.address() != y.address();
108 }
109
110 template<class T> inline
111 RemotePtr<T> remote(T *p)
112 {
113   return RemotePtr<T>(p);
114 }
115
116 template<class T=void> inline
117 RemotePtr<T> remote(uint64_t p)
118 {
119   return RemotePtr<T>(p);
120 }
121
122 }
123 }
124
125 #endif