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>
16 #include "src/mc/mc_forward.hpp"
17 #include "src/mc/remote_ptr.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 given state of a given process (abstract base class)
97 * Currently, this might either be:
99 * * the current state of an existing process;
107 AddressSpace(Process* process) : process_(process) {}
108 virtual ~AddressSpace();
110 simgrid::mc::Process* process() const { return process_; }
112 /** Read data from the address space
114 * @param buffer target buffer for the data
115 * @param size number of bytes
116 * @param address remote source address of the data
117 * @param process_index which process (used for SMPI privatization)
120 virtual const void* read_bytes(void* buffer, std::size_t size,
121 remote_ptr<void> address, int process_index = ProcessIndexAny,
122 ReadOptions options = ReadOptions::none()) const = 0;
124 /** Read a given data structure from the address space */
125 template<class T> inline
126 void read(T *buffer, remote_ptr<T> ptr, int process_index = ProcessIndexAny)
128 this->read_bytes(buffer, sizeof(T), ptr, process_index);
131 /** Read a given data structure from the address space */
132 template<class T> inline
133 T read(remote_ptr<T> ptr, int process_index = ProcessIndexMissing)
135 static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
137 return *(T*)this->read_bytes(&res, sizeof(T), ptr, process_index);