Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Cosmetics: fix "Malformed whitespace in C++" spotted by codefactor.io.
[simgrid.git] / src / mc / AddressSpace.hpp
1 /* Copyright (c) 2008-2020. 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_ = 0;
23   constexpr explicit ReadOptions(std::uint32_t value) : value_(value) {}
24
25 public:
26   constexpr ReadOptions() {}
27
28   explicit constexpr operator bool() const { return value_ != 0; }
29   constexpr bool operator!() const { return value_ == 0; }
30
31   constexpr ReadOptions operator|(ReadOptions const& that) const
32   {
33     return ReadOptions(value_ | that.value_);
34   }
35   constexpr ReadOptions operator&(ReadOptions const& that) const
36   {
37     return ReadOptions(value_ & that.value_);
38   }
39   constexpr ReadOptions operator^(ReadOptions const& that) const
40   {
41     return ReadOptions(value_ ^ that.value_);
42   }
43   constexpr ReadOptions operator~() const
44   {
45     return ReadOptions(~value_);
46   }
47
48   ReadOptions& operator|=(ReadOptions const& that)
49   {
50     value_ |= that.value_;
51     return *this;
52   }
53   ReadOptions& operator&=(ReadOptions const& that)
54   {
55     value_ &= that.value_;
56     return *this;
57   }
58   ReadOptions& operator^=(ReadOptions const& that)
59   {
60     value_ &= that.value_;
61     return *this;
62   }
63
64   /** Copy the data to the given buffer */
65   static constexpr ReadOptions none() { return ReadOptions(0); }
66
67   /** Allows to return a pointer to another buffer where the data is
68    *  available instead of copying the data into the buffer
69    */
70   static constexpr ReadOptions lazy() { return ReadOptions(1); }
71 };
72
73 /** A given state of a given process (abstract base class)
74  *
75  *  Currently, this might either be:
76  *
77  *  * the current state of an existing process;
78  *
79  *  * a snapshot.
80  *
81  *  In order to support SMPI privatization, the can read the memory from the
82  *  context of a given SMPI process: if specified, the code reads data from the
83  *  correct SMPI privatization VMA.
84  */
85 class AddressSpace {
86 private:
87   RemoteClient* process_;
88
89 public:
90   explicit AddressSpace(RemoteClient* process) : process_(process) {}
91   virtual ~AddressSpace() = default;
92
93   /** The process of this address space
94    *
95    *  This is where we can get debug informations, memory layout, etc.
96    */
97   simgrid::mc::RemoteClient* process() const { return process_; }
98
99   /** Read data from the address space
100    *
101    *  @param buffer        target buffer for the data
102    *  @param size          number of bytes to read
103    *  @param address       remote source address of the data
104    *  @param options
105    */
106   virtual void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address,
107                            ReadOptions options = ReadOptions::none()) const = 0;
108
109   /** Read a given data structure from the address space */
110   template <class T> inline void read(T* buffer, RemotePtr<T> ptr) const { this->read_bytes(buffer, sizeof(T), ptr); }
111
112   template <class T> inline void read(Remote<T>& buffer, RemotePtr<T> ptr) const
113   {
114     this->read_bytes(buffer.get_buffer(), sizeof(T), ptr);
115   }
116
117   /** Read a given data structure from the address space
118    *
119    *  This version returns by value.
120    */
121   template <class T> inline Remote<T> read(RemotePtr<T> ptr) const
122   {
123     Remote<T> res;
124     this->read_bytes(&res, sizeof(T), ptr);
125     return res;
126   }
127
128   /** Read a string of known size */
129   std::string read_string(RemotePtr<char> address, std::size_t len) const
130   {
131     std::string res;
132     res.resize(len);
133     this->read_bytes(&res[0], len, address);
134     return res;
135   }
136 };
137
138 }
139 }
140
141 #endif