Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / src / mc / AddressSpace.hpp
1 /* Copyright (c) 2008-2023. 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::mc {
13
14 /** Options for read operations
15  *
16  *  This is a set of flags managed with bitwise operators. Only the
17  *  meaningful operations are defined: addition, conversions to/from
18  *  integers are not allowed.
19  */
20 class ReadOptions {
21   std::uint32_t value_ = 0;
22   constexpr explicit ReadOptions(std::uint32_t value) : value_(value) {}
23
24 public:
25   constexpr ReadOptions() = default;
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 class AddressSpace {
81 private:
82   RemoteProcessMemory* remote_process_memory_;
83
84 public:
85   explicit AddressSpace(RemoteProcessMemory* process) : remote_process_memory_(process) {}
86   virtual ~AddressSpace() = default;
87
88   /** The process of this address space
89    *
90    *  This is where we can get debug information, memory layout, etc.
91    */
92   simgrid::mc::RemoteProcessMemory* get_remote_process_memory() const { return remote_process_memory_; }
93
94   /** Read data from the address space
95    *
96    *  @param buffer        target buffer for the data
97    *  @param size          number of bytes to read
98    *  @param address       remote source address of the data
99    *  @param options
100    */
101   virtual void* read_bytes(void* buffer, std::size_t size, RemotePtr<void> address,
102                            ReadOptions options = ReadOptions::none()) const = 0;
103
104   /** Read a given data structure from the address space */
105   template <class T> inline void read(T* buffer, RemotePtr<T> ptr) const { this->read_bytes(buffer, sizeof(T), ptr); }
106
107   template <class T> inline void read(Remote<T>& buffer, RemotePtr<T> ptr) const
108   {
109     this->read_bytes(buffer.get_buffer(), sizeof(T), ptr);
110   }
111
112   /** Read a given data structure from the address space
113    *
114    *  This version returns by value.
115    */
116   template <class T> inline Remote<T> read(RemotePtr<T> ptr) const
117   {
118     Remote<T> res;
119     this->read_bytes(&res, sizeof(T), ptr);
120     return res;
121   }
122
123   /** Read a string of known size */
124   std::string read_string(RemotePtr<char> address, std::size_t len) const
125   {
126     std::string res;
127     res.resize(len);
128     this->read_bytes(&res[0], len, address);
129     return res;
130   }
131 };
132
133 } // namespace simgrid::mc
134
135 #endif