Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b49866528466fe418b8e08c6395dcd2a88ca04dd
[simgrid.git] / src / mc / AddressSpace.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_ADDRESS_SPACE_H
8 #define SIMGRID_MC_ADDRESS_SPACE_H
9
10 #include <cstdint>
11 #include <type_traits>
12
13 #include <xbt/misc.h>
14
15 #include <stdint.h>
16
17 #include "mc_forward.hpp"
18
19 namespace simgrid {
20 namespace mc {
21
22 /** Pointer to a remote address-space (process, snapshot)
23  *
24  *  With this we can clearly identify the expected type of an address in the
25  *  remote process whild avoiding to use native local pointers.
26  */
27 template<class T> class remote_ptr {
28   std::uint64_t address_;
29 public:
30   remote_ptr() : address_(0) {}
31   remote_ptr(std::uint64_t address) : address_(address) {}
32   remote_ptr(T* address) : address_((std::uintptr_t)address) {}
33   std::uint64_t address() const { return address_; }
34   operator bool() const
35   {
36     return address_;
37   }
38   bool operator!() const
39   {
40     return !address_;
41   }
42   operator remote_ptr<void>() const
43   {
44     return remote_ptr<void>(address_);
45   }
46   remote_ptr<T> operator+(std::uint64_t n) const
47   {
48     return remote_ptr<T>(address_ + n * sizeof(T));
49   }
50   remote_ptr<T> operator-(std::uint64_t n) const
51   {
52     return remote_ptr<T>(address_ - n * sizeof(T));
53   }
54   remote_ptr<T>& operator+=(std::uint64_t n) const
55   {
56     address_ += n * sizeof(T);
57     return *this;
58   }
59   remote_ptr<T>& operator-=(std::uint64_t n) const
60   {
61     address_ -= n * sizeof(T);
62     return *this;
63   }
64 };
65
66 template<class X, class Y>
67 bool operator<(remote_ptr<X> const& x, remote_ptr<Y> const& y)
68 {
69   return x.address() < y.address();
70 }
71
72 template<class X, class Y>
73 bool operator>(remote_ptr<X> const& x, remote_ptr<Y> const& y)
74 {
75   return x.address() > y.address();
76 }
77
78 template<class X, class Y>
79 bool operator>=(remote_ptr<X> const& x, remote_ptr<Y> const& y)
80 {
81   return x.address() >= y.address();
82 }
83
84 template<class X, class Y>
85 bool operator<=(remote_ptr<X> const& x, remote_ptr<Y> const& y)
86 {
87   return x.address() <= y.address();
88 }
89
90 template<class X, class Y>
91 bool operator==(remote_ptr<X> const& x, remote_ptr<Y> const& y)
92 {
93   return x.address() == y.address();
94 }
95
96 template<class X, class Y>
97 bool operator!=(remote_ptr<X> const& x, remote_ptr<Y> const& y)
98 {
99   return x.address() != y.address();
100 }
101
102 template<class T> inline
103 remote_ptr<T> remote(T *p)
104 {
105   return remote_ptr<T>(p);
106 }
107
108 template<class T=void> inline
109 remote_ptr<T> remote(uint64_t p)
110 {
111   return remote_ptr<T>(p);
112 }
113
114 /** Process index used when no process is available
115  *
116  *  The expected behaviour is that if a process index is needed it will fail.
117  * */
118 const int ProcessIndexMissing = -1;
119
120 /** Process index used when we don't care about the process index
121  * */
122 const int ProcessIndexDisabled = -2;
123
124 /** Constant used when any process will do.
125  *
126  *  This is is index of the first process.
127  */
128 const int ProcessIndexAny = 0;
129
130 class AddressSpace {
131 public:
132   enum ReadMode {
133     Normal,
134     /** Allows the `read_bytes` to return a pointer to another buffer
135      *  where the data ins available instead of copying the data into the buffer
136      */
137     Lazy
138   };
139   virtual ~AddressSpace();
140   virtual const void* read_bytes(void* buffer, std::size_t size,
141     remote_ptr<void> address, int process_index = ProcessIndexAny,
142     ReadMode mode = Normal) const = 0;
143
144   template<class T> inline
145   void read(T *buffer, remote_ptr<T> ptr, int process_index = ProcessIndexAny)
146   {
147     this->read_bytes(buffer, sizeof(T), ptr, process_index);
148   }
149
150   template<class T> inline
151   T read(remote_ptr<T> ptr, int process_index = ProcessIndexMissing)
152   {
153     static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
154     T res;
155     return *(T*)this->read_bytes(&res, sizeof(T), ptr, process_index);
156   }
157 };
158
159 }
160 }
161
162 #endif