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"
21 /** Pointer to a remote address-space (process, snapshot)
23 * With this we can clearly identify the expected type of an address in the
24 * remote process while avoiding to use native local pointers.
26 * Some operators (+/-) assume use the size of the underlying element. This
27 * only works if the target applications is using the same target: it won't
28 * work for example, when inspecting a 32 bit application from a 64 bit
31 template<class T> class remote_ptr {
32 std::uint64_t address_;
34 remote_ptr() : address_(0) {}
35 remote_ptr(std::uint64_t address) : address_(address) {}
36 remote_ptr(T* address) : address_((std::uintptr_t)address) {}
37 std::uint64_t address() const { return address_; }
43 bool operator!() const
47 operator remote_ptr<void>() const
49 return remote_ptr<void>(address_);
51 remote_ptr<T> operator+(std::uint64_t n) const
53 return remote_ptr<T>(address_ + n * sizeof(T));
55 remote_ptr<T> operator-(std::uint64_t n) const
57 return remote_ptr<T>(address_ - n * sizeof(T));
59 remote_ptr<T>& operator+=(std::uint64_t n)
61 address_ += n * sizeof(T);
64 remote_ptr<T>& operator-=(std::uint64_t n)
66 address_ -= n * sizeof(T);
71 template<class X, class Y>
72 bool operator<(remote_ptr<X> const& x, remote_ptr<Y> const& y)
74 return x.address() < y.address();
77 template<class X, class Y>
78 bool operator>(remote_ptr<X> const& x, remote_ptr<Y> const& y)
80 return x.address() > y.address();
83 template<class X, class Y>
84 bool operator>=(remote_ptr<X> const& x, remote_ptr<Y> const& y)
86 return x.address() >= y.address();
89 template<class X, class Y>
90 bool operator<=(remote_ptr<X> const& x, remote_ptr<Y> const& y)
92 return x.address() <= y.address();
95 template<class X, class Y>
96 bool operator==(remote_ptr<X> const& x, remote_ptr<Y> const& y)
98 return x.address() == y.address();
101 template<class X, class Y>
102 bool operator!=(remote_ptr<X> const& x, remote_ptr<Y> const& y)
104 return x.address() != y.address();
107 template<class T> inline
108 remote_ptr<T> remote(T *p)
110 return remote_ptr<T>(p);
113 template<class T=void> inline
114 remote_ptr<T> remote(uint64_t p)
116 return remote_ptr<T>(p);
119 /** Process index used when no process is available
121 * The expected behaviour is that if a process index is needed it will fail.
123 const int ProcessIndexMissing = -1;
125 /** Process index used when we don't care about the process index
127 const int ProcessIndexDisabled = -2;
129 /** Constant used when any process will do.
131 * This is is index of the first process.
133 const int ProcessIndexAny = 0;
135 /** Options for read operations
137 * This is a set of flags managed with bitwise operators. Only the
138 * meaningful operations are defined: addition, conversions to/from
139 * integers are not allowed.
142 std::uint32_t value_;
143 constexpr explicit ReadOptions(std::uint32_t value) : value_(value) {}
145 constexpr ReadOptions() : value_(0) {}
147 constexpr operator bool() const { return value_ != 0; }
148 constexpr operator!() const { return value_ == 0; }
150 constexpr ReadOptions operator|(ReadOptions const& that) const
152 return ReadOptions(value_ | that.value_);
154 constexpr ReadOptions operator&(ReadOptions const& that) const
156 return ReadOptions(value_ & that.value_);
158 constexpr ReadOptions operator^(ReadOptions const& that) const
160 return ReadOptions(value_ ^ that.value_);
162 constexpr ReadOptions operator~() const
164 return ReadOptions(~value_);
167 ReadOptions& operator|=(ReadOptions const& that)
169 value_ |= that.value_;
172 ReadOptions& operator&=(ReadOptions const& that)
174 value_ &= that.value_;
177 ReadOptions& operator^=(ReadOptions const& that)
179 value_ &= that.value_;
183 /** Copy the data to the given buffer */
184 static constexpr ReadOptions none() { return ReadOptions(0); }
186 /** Allows to return a pointer to another buffer where the data is
187 * available instead of copying the data into the buffer
189 static constexpr ReadOptions lazy() { return ReadOptions(1); }
192 /** A given state of a given process (abstract base class)
194 * Currently, this might either be:
196 * * the current state of an existing process;
204 AddressSpace(Process* process) : process_(process) {}
205 virtual ~AddressSpace();
207 simgrid::mc::Process* process() const { return process_; }
209 /** Read data from the address space
211 * @param buffer target buffer for the data
212 * @param size number of bytes
213 * @param address remote source address of the data
214 * @param process_index which process (used for SMPI privatization)
217 virtual const void* read_bytes(void* buffer, std::size_t size,
218 remote_ptr<void> address, int process_index = ProcessIndexAny,
219 ReadOptions options = ReadOptions::none()) const = 0;
221 /** Read a given data structure from the address space */
222 template<class T> inline
223 void read(T *buffer, remote_ptr<T> ptr, int process_index = ProcessIndexAny)
225 this->read_bytes(buffer, sizeof(T), ptr, process_index);
228 /** Read a given data structure from the address space */
229 template<class T> inline
230 T read(remote_ptr<T> ptr, int process_index = ProcessIndexMissing)
232 static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
234 return *(T*)this->read_bytes(&res, sizeof(T), ptr, process_index);