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
13 #include <type_traits>
15 #include "src/mc/mc_forward.hpp"
16 #include "src/mc/RemotePtr.hpp"
21 /** Process index used when no process is available
23 * The expected behaviour is that if a process index is needed it will fail.
25 const int ProcessIndexMissing = -1;
27 /** Process index used when we don't care about the process index
29 const int ProcessIndexDisabled = -2;
31 /** Constant used when any process will do.
33 * This is is index of the first process.
35 const int ProcessIndexAny = 0;
37 /** Options for read operations
39 * This is a set of flags managed with bitwise operators. Only the
40 * meaningful operations are defined: addition, conversions to/from
41 * integers are not allowed.
45 constexpr explicit ReadOptions(std::uint32_t value) : value_(value) {}
47 constexpr ReadOptions() : value_(0) {}
49 constexpr operator bool() const { return value_ != 0; }
50 constexpr bool operator!() const { return value_ == 0; }
52 constexpr ReadOptions operator|(ReadOptions const& that) const
54 return ReadOptions(value_ | that.value_);
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~() const
66 return ReadOptions(~value_);
69 ReadOptions& operator|=(ReadOptions const& that)
71 value_ |= that.value_;
74 ReadOptions& operator&=(ReadOptions const& that)
76 value_ &= that.value_;
79 ReadOptions& operator^=(ReadOptions const& that)
81 value_ &= that.value_;
85 /** Copy the data to the given buffer */
86 static constexpr ReadOptions none() { return ReadOptions(0); }
88 /** Allows to return a pointer to another buffer where the data is
89 * available instead of copying the data into the buffer
91 static constexpr ReadOptions lazy() { return ReadOptions(1); }
94 /** A value read from another process */
98 char buffer[sizeof(T)];
100 void* data() { return buffer; }
101 const void* data() const { return buffer; }
102 constexpr std::size_t size() const { return sizeof(T); }
105 /** A given state of a given process (abstract base class)
107 * Currently, this might either be:
109 * * the current state of an existing process;
117 AddressSpace(Process* process) : process_(process) {}
118 virtual ~AddressSpace();
120 simgrid::mc::Process* process() const { return process_; }
122 /** Read data from the address space
124 * @param buffer target buffer for the data
125 * @param size number of bytes
126 * @param address remote source address of the data
127 * @param process_index which process (used for SMPI privatization)
130 virtual const void* read_bytes(void* buffer, std::size_t size,
131 RemotePtr<void> address, int process_index = ProcessIndexAny,
132 ReadOptions options = ReadOptions::none()) const = 0;
134 /** Read a given data structure from the address space */
135 template<class T> inline
136 void read(T *buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny)
138 this->read_bytes(buffer, sizeof(T), ptr, process_index);
141 template<class T> inline
142 void read(Remote<T>& buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny)
144 this->read_bytes(buffer.data(), sizeof(T), ptr, process_index);
147 /** Read a given data structure from the address space */
148 template<class T> inline
149 T read(RemotePtr<T> ptr, int process_index = ProcessIndexMissing)
151 static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
153 return *(T*)this->read_bytes(&res, sizeof(T), ptr, process_index);