1 /* Copyright (c) 2008-2015. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #ifndef SIMGRID_MC_ADDRESS_SPACE_H
8 #define SIMGRID_MC_ADDRESS_SPACE_H
14 #include <type_traits>
19 #include "src/mc/mc_forward.hpp"
20 #include "src/mc/remote/RemotePtr.hpp"
25 /** Process index used when no process is available (SMPI privatization)
27 * The expected behavior is that if a process index is needed it will fail.
29 const int ProcessIndexMissing = -1;
31 /** Process index used when we don't care about the process index (SMPI privatization)
33 const int ProcessIndexDisabled = -2;
35 /** Constant used when any process will do (SMPI privatization)
37 * Note: This is is index of the first process.
39 const int ProcessIndexAny = 0;
41 /** Options for read operations
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.
49 constexpr explicit ReadOptions(std::uint32_t value) : value_(value) {}
51 constexpr ReadOptions() : value_(0) {}
53 explicit constexpr operator bool() const { return value_ != 0; }
54 constexpr bool operator!() const { return value_ == 0; }
56 constexpr ReadOptions operator|(ReadOptions const& that) const
58 return ReadOptions(value_ | that.value_);
60 constexpr ReadOptions operator&(ReadOptions const& that) const
62 return ReadOptions(value_ & that.value_);
64 constexpr ReadOptions operator^(ReadOptions const& that) const
66 return ReadOptions(value_ ^ that.value_);
68 constexpr ReadOptions operator~() const
70 return ReadOptions(~value_);
73 ReadOptions& operator|=(ReadOptions const& that)
75 value_ |= that.value_;
78 ReadOptions& operator&=(ReadOptions const& that)
80 value_ &= that.value_;
83 ReadOptions& operator^=(ReadOptions const& that)
85 value_ &= that.value_;
89 /** Copy the data to the given buffer */
90 static constexpr ReadOptions none() { return ReadOptions(0); }
92 /** Allows to return a pointer to another buffer where the data is
93 * available instead of copying the data into the buffer
95 static constexpr ReadOptions lazy() { return ReadOptions(1); }
98 /** A given state of a given process (abstract base class)
100 * Currently, this might either be:
102 * * the current state of an existing process;
106 * In order to support SMPI privatization, the can read the memory from the
107 * context of a given SMPI process: if specified, the code reads data from the
108 * correct SMPI privatization VMA.
112 RemoteClient* process_;
115 explicit AddressSpace(RemoteClient* process) : process_(process) {}
116 virtual ~AddressSpace() = default;
118 /** The process of this address space
120 * This is where we can get debug informations, memory layout, etc.
122 simgrid::mc::RemoteClient* process() const { return process_; }
124 /** Read data from the address space
126 * @param buffer target buffer for the data
127 * @param size number of bytes to read
128 * @param address remote source address of the data
129 * @param process_index which process (used for SMPI privatization)
132 virtual const void* read_bytes(void* buffer, std::size_t size,
133 RemotePtr<void> address, int process_index = ProcessIndexAny,
134 ReadOptions options = ReadOptions::none()) const = 0;
136 /** Read a given data structure from the address space */
137 template<class T> inline
138 void read(T *buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny) const
140 this->read_bytes(buffer, sizeof(T), ptr, process_index);
143 template<class T> inline
144 void read(Remote<T>& buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny) const
146 this->read_bytes(buffer.getBuffer(), sizeof(T), ptr, process_index);
149 /** Read a given data structure from the addres space
151 * This version returns by value.
153 template<class T> inline
154 Remote<T> read(RemotePtr<T> ptr, int process_index = ProcessIndexMissing) const
157 this->read_bytes(&res, sizeof(T), ptr, process_index);
161 /** Read a string of known size */
162 std::string read_string(RemotePtr<char> address, std::size_t len) const
166 this->read_bytes(&res[0], len, address);