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
11 #include <type_traits>
17 #include "mc_forward.hpp"
22 /** Pointer to a remote address-space (process, snapshot)
24 * With this we can clearly identify the expected type of an address in the
25 * remote process whild avoiding to use native local pointers.
27 template<class T> class remote_ptr {
28 std::uint64_t address_;
30 remote_ptr() : address_(0) {}
31 remote_ptr(std::uint64_t address) : address_(address) {}
32 remote_ptr(T* address) : address_((std::uintptr_t)address) {}
33 std::uint64_t address() const { return address_; }
38 bool operator!() const
42 operator remote_ptr<void>() const
44 return remote_ptr<void>(address_);
46 remote_ptr<T> operator+(std::uint64_t n) const
48 return remote_ptr<T>(address_ + n * sizeof(T));
50 remote_ptr<T> operator-(std::uint64_t n) const
52 return remote_ptr<T>(address_ - n * sizeof(T));
54 remote_ptr<T>& operator+=(std::uint64_t n) const
56 address_ += n * sizeof(T);
59 remote_ptr<T>& operator-=(std::uint64_t n) const
61 address_ -= n * sizeof(T);
66 template<class X, class Y>
67 bool operator<(remote_ptr<X> const& x, remote_ptr<Y> const& y)
69 return x.address() < y.address();
72 template<class X, class Y>
73 bool operator>(remote_ptr<X> const& x, remote_ptr<Y> const& y)
75 return x.address() > y.address();
78 template<class X, class Y>
79 bool operator>=(remote_ptr<X> const& x, remote_ptr<Y> const& y)
81 return x.address() >= y.address();
84 template<class X, class Y>
85 bool operator<=(remote_ptr<X> const& x, remote_ptr<Y> const& y)
87 return x.address() <= y.address();
90 template<class X, class Y>
91 bool operator==(remote_ptr<X> const& x, remote_ptr<Y> const& y)
93 return x.address() == y.address();
96 template<class X, class Y>
97 bool operator!=(remote_ptr<X> const& x, remote_ptr<Y> const& y)
99 return x.address() != y.address();
102 template<class T> inline
103 remote_ptr<T> remote(T *p)
105 return remote_ptr<T>(p);
108 template<class T=void> inline
109 remote_ptr<T> remote(uint64_t p)
111 return remote_ptr<T>(p);
114 /** Process index used when no process is available
116 * The expected behaviour is that if a process index is needed it will fail.
118 const int ProcessIndexMissing = -1;
120 /** Process index used when we don't care about the process index
122 const int ProcessIndexDisabled = -2;
124 /** Constant used when any process will do.
126 * This is is index of the first process.
128 const int ProcessIndexAny = 0;
134 /** Allows the `read_bytes` to return a pointer to another buffer
135 * where the data ins available instead of copying the data into the buffer
139 virtual ~AddressSpace();
140 virtual const void* read_bytes(void* buffer, std::size_t size,
141 remote_ptr<void> address, int process_index = ProcessIndexAny,
142 ReadMode mode = Normal) const = 0;
144 template<class T> inline
145 void read(T *buffer, remote_ptr<T> ptr, int process_index = ProcessIndexAny)
147 this->read_bytes(buffer, sizeof(T), ptr, process_index);
150 template<class T> inline
151 T read(remote_ptr<T> ptr, int process_index = ProcessIndexMissing)
153 static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
155 return *(T*)this->read_bytes(&res, sizeof(T), ptr, process_index);