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
12 #include <type_traits>
14 #include "src/mc/mc_forward.hpp"
15 #include "src/mc/RemotePtr.hpp"
20 /** Process index used when no process is available
22 * The expected behaviour is that if a process index is needed it will fail.
24 const int ProcessIndexMissing = -1;
26 /** Process index used when we don't care about the process index
28 const int ProcessIndexDisabled = -2;
30 /** Constant used when any process will do.
32 * This is is index of the first process.
34 const int ProcessIndexAny = 0;
36 /** Options for read operations
38 * This is a set of flags managed with bitwise operators. Only the
39 * meaningful operations are defined: addition, conversions to/from
40 * integers are not allowed.
44 constexpr explicit ReadOptions(std::uint32_t value) : value_(value) {}
46 constexpr ReadOptions() : value_(0) {}
48 constexpr operator bool() const { return value_ != 0; }
49 constexpr bool operator!() const { return value_ == 0; }
51 constexpr ReadOptions operator|(ReadOptions const& that) const
53 return ReadOptions(value_ | that.value_);
55 constexpr ReadOptions operator&(ReadOptions const& that) const
57 return ReadOptions(value_ & that.value_);
59 constexpr ReadOptions operator^(ReadOptions const& that) const
61 return ReadOptions(value_ ^ that.value_);
63 constexpr ReadOptions operator~() const
65 return ReadOptions(~value_);
68 ReadOptions& operator|=(ReadOptions const& that)
70 value_ |= that.value_;
73 ReadOptions& operator&=(ReadOptions const& that)
75 value_ &= that.value_;
78 ReadOptions& operator^=(ReadOptions const& that)
80 value_ &= that.value_;
84 /** Copy the data to the given buffer */
85 static constexpr ReadOptions none() { return ReadOptions(0); }
87 /** Allows to return a pointer to another buffer where the data is
88 * available instead of copying the data into the buffer
90 static constexpr ReadOptions lazy() { return ReadOptions(1); }
93 /** A given state of a given process (abstract base class)
95 * Currently, this might either be:
97 * * the current state of an existing process;
105 AddressSpace(Process* process) : process_(process) {}
106 virtual ~AddressSpace();
108 simgrid::mc::Process* process() const { return process_; }
110 /** Read data from the address space
112 * @param buffer target buffer for the data
113 * @param size number of bytes
114 * @param address remote source address of the data
115 * @param process_index which process (used for SMPI privatization)
118 virtual const void* read_bytes(void* buffer, std::size_t size,
119 RemotePtr<void> address, int process_index = ProcessIndexAny,
120 ReadOptions options = ReadOptions::none()) const = 0;
122 /** Read a given data structure from the address space */
123 template<class T> inline
124 void read(T *buffer, RemotePtr<T> ptr, int process_index = ProcessIndexAny)
126 this->read_bytes(buffer, sizeof(T), ptr, process_index);
129 /** Read a given data structure from the address space */
130 template<class T> inline
131 T read(RemotePtr<T> ptr, int process_index = ProcessIndexMissing)
133 static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
135 return *(T*)this->read_bytes(&res, sizeof(T), ptr, process_index);