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>
16 #include "src/mc/mc_forward.hpp"
17 #include "src/mc/RemotePtr.hpp"
22 /** Process index used when no process is available
24 * The expected behaviour is that if a process index is needed it will fail.
26 const int ProcessIndexMissing = -1;
28 /** Process index used when we don't care about the process index
30 const int ProcessIndexDisabled = -2;
32 /** Constant used when any process will do.
34 * This is is index of the first process.
36 const int ProcessIndexAny = 0;
38 /** Options for read operations
40 * This is a set of flags managed with bitwise operators. Only the
41 * meaningful operations are defined: addition, conversions to/from
42 * integers are not allowed.
46 constexpr explicit ReadOptions(std::uint32_t value) : value_(value) {}
48 constexpr ReadOptions() : value_(0) {}
50 constexpr operator bool() const { return value_ != 0; }
51 constexpr bool operator!() const { return value_ == 0; }
53 constexpr ReadOptions operator|(ReadOptions const& that) const
55 return ReadOptions(value_ | that.value_);
57 constexpr ReadOptions operator&(ReadOptions const& that) const
59 return ReadOptions(value_ & that.value_);
61 constexpr ReadOptions operator^(ReadOptions const& that) const
63 return ReadOptions(value_ ^ that.value_);
65 constexpr ReadOptions operator~() const
67 return ReadOptions(~value_);
70 ReadOptions& operator|=(ReadOptions const& that)
72 value_ |= that.value_;
75 ReadOptions& operator&=(ReadOptions const& that)
77 value_ &= that.value_;
80 ReadOptions& operator^=(ReadOptions const& that)
82 value_ &= that.value_;
86 /** Copy the data to the given buffer */
87 static constexpr ReadOptions none() { return ReadOptions(0); }
89 /** Allows to return a pointer to another buffer where the data is
90 * available instead of copying the data into the buffer
92 static constexpr ReadOptions lazy() { return ReadOptions(1); }
95 /** A value from another process
97 * This represents a value from another process:
99 * * constructor/destructor are disabled;
101 * * raw memory copy (std::memcpy) is used to copy Remote<T>;
103 * * raw memory comparison is used to compare them;
105 * * when T is a trivial type, Remote is convertible to a T.
107 * We currently only handle the case where the type has the same layout
108 * in the current process and in the target process: we don't handle
109 * cross-architecture (such as 32-bit/64-bit access).
114 // If we use a union, it won't work with abstract types:
115 char buffer[sizeof(T)];
117 // HACK, some code currently cast this to T* which is **not** legal.
118 void* data() { return buffer; }
119 const void* data() const { return buffer; }
120 constexpr std::size_t size() const { return sizeof(T); }
122 static_assert(std::is_trivial<T>::value, "Cannot convert non trivial type");
124 std::memcpy(&res, buffer, sizeof(T));
130 std::memcpy(&x, buffer, sizeof(T));
132 Remote& operator=(T const& x)
134 std::memcpy(&x, buffer, sizeof(T));
138 /** A given state of a given process (abstract base class)
140 * Currently, this might either be:
142 * * the current state of an existing process;
150 AddressSpace(Process* process) : process_(process) {}
151 virtual ~AddressSpace();
153 simgrid::mc::Process* process() const { return process_; }
155 /** Read data from the address space
157 * @param buffer target buffer for the data
158 * @param size number of bytes
159 * @param address remote source address of the data
160 * @param process_index which process (used for SMPI privatization)
163 virtual const void* read_bytes(void* buffer, std::size_t size,
164 RemotePtr<void> address, int process_index = ProcessIndexAny,
165 ReadOptions options = ReadOptions::none()) const = 0;
167 /** Read a given data structure from the address space */
168 template<class T> inline
169 void read(T *buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny)
171 this->read_bytes(buffer, sizeof(T), ptr, process_index);
174 template<class T> inline
175 void read(Remote<T>& buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny)
177 this->read_bytes(buffer.data(), sizeof(T), ptr, process_index);
180 /** Read a given data structure from the address space */
181 template<class T> inline
182 Remote<T> read(RemotePtr<T> ptr, int process_index = ProcessIndexMissing)
185 this->read_bytes(&res, sizeof(T), ptr, process_index);