Logo AND Algorithmique Numérique Distribuée

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