Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] DeXFTification of Synchro: use std::list<> instead of xbt_fifo_t
[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 <cassert>
11 #include <cstddef>
12 #include <cstdint>
13 #include <cstring>
14 #include <type_traits>
15
16 #include <string>
17 #include <vector>
18
19 #include "src/mc/mc_forward.hpp"
20 #include "src/mc/RemotePtr.hpp"
21
22 namespace simgrid {
23 namespace mc {
24
25 /** Process index used when no process is available
26  *
27  *  The expected behaviour is that if a process index is needed it will fail.
28  * */
29 const int ProcessIndexMissing = -1;
30
31 /** Process index used when we don't care about the process index
32  * */
33 const int ProcessIndexDisabled = -2;
34
35 /** Constant used when any process will do.
36  *
37  *  This is is index of the first process.
38  */
39 const int ProcessIndexAny = 0;
40
41 /** Options for read operations
42  *
43  *  This is a set of flags managed with bitwise operators. Only the
44  *  meaningful operations are defined: addition, conversions to/from
45  *  integers are not allowed.
46  */
47 class ReadOptions {
48   std::uint32_t value_;
49   constexpr explicit ReadOptions(std::uint32_t value) : value_(value) {}
50 public:
51   constexpr ReadOptions() : value_(0) {}
52
53   constexpr operator bool() const { return value_ != 0; }
54   constexpr bool operator!() const { return value_ == 0; }
55
56   constexpr ReadOptions operator|(ReadOptions const& that) const
57   {
58     return ReadOptions(value_ | that.value_);
59   }
60   constexpr ReadOptions operator&(ReadOptions const& that) const
61   {
62     return ReadOptions(value_ & that.value_);
63   }
64   constexpr ReadOptions operator^(ReadOptions const& that) const
65   {
66     return ReadOptions(value_ ^ that.value_);
67   }
68   constexpr ReadOptions operator~() const
69   {
70     return ReadOptions(~value_);
71   }
72
73   ReadOptions& operator|=(ReadOptions const& that)
74   {
75     value_ |= that.value_;
76     return *this;
77   }
78   ReadOptions& operator&=(ReadOptions const& that)
79   {
80     value_ &= that.value_;
81     return *this;
82   }
83   ReadOptions& operator^=(ReadOptions const& that)
84   {
85     value_ &= that.value_;
86     return *this;
87   }
88
89   /** Copy the data to the given buffer */
90   static constexpr ReadOptions none() { return ReadOptions(0); }
91
92   /** Allows to return a pointer to another buffer where the data is
93    *  available instead of copying the data into the buffer
94    */
95   static constexpr ReadOptions lazy() { return ReadOptions(1); }
96 };
97
98 /** HACK, A value from another process
99  *
100  *  This represents a value from another process:
101  *
102  *  * constructor/destructor are disabled;
103  *
104  *  * raw memory copy (std::memcpy) is used to copy Remote<T>;
105  *
106  *  * raw memory comparison is used to compare them;
107  *
108  *  * when T is a trivial type, Remote is convertible to a T.
109  *
110  *  We currently only handle the case where the type has the same layout
111  *  in the current process and in the target process: we don't handle
112  *  cross-architecture (such as 32-bit/64-bit access).
113  */
114 template<class T>
115 union Remote {
116 private:
117   T buffer;
118 public:
119   Remote() {}
120   ~Remote() {}
121   Remote(Remote const& that)
122   {
123     std::memcpy(&buffer, &that.buffer, sizeof(buffer));
124   }
125   Remote& operator=(Remote const& that)
126   {
127     std::memcpy(&buffer, &that.buffer, sizeof(buffer));
128     return *this;
129   }
130   T*       getBuffer() { return &buffer; }
131   const T* getBuffer() const { return &buffer; }
132   std::size_t getBufferSize() const { return sizeof(T); }
133   operator T() const {
134     static_assert(std::is_trivial<T>::value, "Cannot convert non trivial type");
135     return buffer;
136   }
137   void clear()
138   {
139     std::memset(static_cast<void*>(&buffer), 0, sizeof(T));
140   }
141 };
142
143 /** A given state of a given process (abstract base class)
144  *
145  *  Currently, this might either be:
146  *
147  *  * the current state of an existing process;
148  *
149  *  * a snapshot.
150  */
151 class AddressSpace {
152 private:
153   Process* process_;
154 public:
155   AddressSpace(Process* process) : process_(process) {}
156   virtual ~AddressSpace();
157
158   simgrid::mc::Process* process() const { return process_; }
159
160   /** Read data from the address space
161    *
162    *  @param buffer        target buffer for the data
163    *  @param size          number of bytes
164    *  @param address       remote source address of the data
165    *  @param process_index which process (used for SMPI privatization)
166    *  @param options
167    */
168   virtual const void* read_bytes(void* buffer, std::size_t size,
169     RemotePtr<void> address, int process_index = ProcessIndexAny,
170     ReadOptions options = ReadOptions::none()) const = 0;
171
172   /** Read a given data structure from the address space */
173   template<class T> inline
174   void read(T *buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny) const
175   {
176     this->read_bytes(buffer, sizeof(T), ptr, process_index);
177   }
178
179   template<class T> inline
180   void read(Remote<T>& buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny) const
181   {
182     this->read_bytes(buffer.getBuffer(), sizeof(T), ptr, process_index);
183   }
184
185   /** Read a given data structure from the address space */
186   template<class T> inline
187   Remote<T> read(RemotePtr<T> ptr, int process_index = ProcessIndexMissing) const
188   {
189     Remote<T> res;
190     this->read_bytes(&res, sizeof(T), ptr, process_index);
191     return res;
192   }
193
194   std::string read_string(RemotePtr<char> address, std::size_t len) const
195   {
196     // TODO, use std::vector with .data() in C++17 to avoid useless copies
197     std::vector<char> buffer(len);
198     buffer[len] = '\0';
199     this->read_bytes(buffer.data(), len, address);
200     return std::string(buffer.data(), buffer.size());
201   }
202
203 };
204
205 }
206 }
207
208 #endif