Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix a doc error about actors (Tutorial_algorithms)
[simgrid.git] / src / mc / AddressSpace.hpp
1 /* Copyright (c) 2008-2019. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_ADDRESS_SPACE_H
7 #define SIMGRID_MC_ADDRESS_SPACE_H
8
9 #include "src/mc/mc_forward.hpp"
10 #include "src/mc/remote/RemotePtr.hpp"
11
12 namespace simgrid {
13 namespace mc {
14
15 /** Options for read operations
16  *
17  *  This is a set of flags managed with bitwise operators. Only the
18  *  meaningful operations are defined: addition, conversions to/from
19  *  integers are not allowed.
20  */
21 class ReadOptions {
22   std::uint32_t value_;
23   constexpr explicit ReadOptions(std::uint32_t value) : value_(value) {}
24 public:
25   constexpr ReadOptions() : value_(0) {}
26
27   explicit constexpr operator bool() const { return value_ != 0; }
28   constexpr bool operator!() const { return value_ == 0; }
29
30   constexpr ReadOptions operator|(ReadOptions const& that) const
31   {
32     return ReadOptions(value_ | that.value_);
33   }
34   constexpr ReadOptions operator&(ReadOptions const& that) const
35   {
36     return ReadOptions(value_ & that.value_);
37   }
38   constexpr ReadOptions operator^(ReadOptions const& that) const
39   {
40     return ReadOptions(value_ ^ that.value_);
41   }
42   constexpr ReadOptions operator~() const
43   {
44     return ReadOptions(~value_);
45   }
46
47   ReadOptions& operator|=(ReadOptions const& that)
48   {
49     value_ |= that.value_;
50     return *this;
51   }
52   ReadOptions& operator&=(ReadOptions const& that)
53   {
54     value_ &= that.value_;
55     return *this;
56   }
57   ReadOptions& operator^=(ReadOptions const& that)
58   {
59     value_ &= that.value_;
60     return *this;
61   }
62
63   /** Copy the data to the given buffer */
64   static constexpr ReadOptions none() { return ReadOptions(0); }
65
66   /** Allows to return a pointer to another buffer where the data is
67    *  available instead of copying the data into the buffer
68    */
69   static constexpr ReadOptions lazy() { return ReadOptions(1); }
70 };
71
72 /** A given state of a given process (abstract base class)
73  *
74  *  Currently, this might either be:
75  *
76  *  * the current state of an existing process;
77  *
78  *  * a snapshot.
79  *
80  *  In order to support SMPI privatization, the can read the memory from the
81  *  context of a given SMPI process: if specified, the code reads data from the
82  *  correct SMPI privatization VMA.
83  */
84 class AddressSpace {
85 private:
86   RemoteClient* process_;
87
88 public:
89   explicit AddressSpace(RemoteClient* process) : process_(process) {}
90   virtual ~AddressSpace() = default;
91
92   /** The process of this address space
93    *
94    *  This is where we can get debug informations, memory layout, etc.
95    */
96   simgrid::mc::RemoteClient* process() const { return process_; }
97
98   /** Read data from the address space
99    *
100    *  @param buffer        target buffer for the data
101    *  @param size          number of bytes to read
102    *  @param address       remote source address of the data
103    *  @param options
104    */
105   virtual void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address,
106                            ReadOptions options = ReadOptions::none()) const = 0;
107
108   /** Read a given data structure from the address space */
109   template <class T> inline void read(T* buffer, RemotePtr<T> ptr) const { this->read_bytes(buffer, sizeof(T), ptr); }
110
111   template <class T> inline void read(Remote<T>& buffer, RemotePtr<T> ptr) const
112   {
113     this->read_bytes(buffer.get_buffer(), sizeof(T), ptr);
114   }
115
116   /** Read a given data structure from the address space
117    *
118    *  This version returns by value.
119    */
120   template <class T> inline Remote<T> read(RemotePtr<T> ptr) const
121   {
122     Remote<T> res;
123     this->read_bytes(&res, sizeof(T), ptr);
124     return res;
125   }
126
127   /** Read a string of known size */
128   std::string read_string(RemotePtr<char> address, std::size_t len) const
129   {
130     std::string res;
131     res.resize(len);
132     this->read_bytes(&res[0], len, address);
133     return res;
134   }
135
136 };
137
138 }
139 }
140
141 #endif