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 "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 whild avoiding to use native local pointers.
26 template<class T> class remote_ptr {
27 std::uint64_t address_;
29 remote_ptr() : address_(0) {}
30 remote_ptr(std::uint64_t address) : address_(address) {}
31 remote_ptr(T* address) : address_((std::uintptr_t)address) {}
32 std::uint64_t address() const { return address_; }
37 bool operator!() const
41 operator remote_ptr<void>() const
43 return remote_ptr<void>(address_);
45 remote_ptr<T> operator+(std::uint64_t n) const
47 return remote_ptr<T>(address_ + n * sizeof(T));
49 remote_ptr<T> operator-(std::uint64_t n) const
51 return remote_ptr<T>(address_ - n * sizeof(T));
53 remote_ptr<T>& operator+=(std::uint64_t n) const
55 address_ += n * sizeof(T);
58 remote_ptr<T>& operator-=(std::uint64_t n) const
60 address_ -= n * sizeof(T);
65 template<class X, class Y>
66 bool operator<(remote_ptr<X> const& x, remote_ptr<Y> const& y)
68 return x.address() < y.address();
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 T> inline
102 remote_ptr<T> remote(T *p)
104 return remote_ptr<T>(p);
107 template<class T=void> inline
108 remote_ptr<T> remote(uint64_t p)
110 return remote_ptr<T>(p);
113 /** Process index used when no process is available
115 * The expected behaviour is that if a process index is needed it will fail.
117 const int ProcessIndexMissing = -1;
119 /** Process index used when we don't care about the process index
121 const int ProcessIndexDisabled = -2;
123 /** Constant used when any process will do.
125 * This is is index of the first process.
127 const int ProcessIndexAny = 0;
135 /** Allows the `read_bytes` to return a pointer to another buffer
136 * where the data ins available instead of copying the data into the buffer
140 AddressSpace(Process* process) : process_(process) {}
141 virtual ~AddressSpace();
143 simgrid::mc::Process* process() { return process_; }
144 virtual const void* read_bytes(void* buffer, std::size_t size,
145 remote_ptr<void> address, int process_index = ProcessIndexAny,
146 ReadMode mode = Normal) const = 0;
148 template<class T> inline
149 void read(T *buffer, remote_ptr<T> ptr, int process_index = ProcessIndexAny)
151 this->read_bytes(buffer, sizeof(T), ptr, process_index);
154 template<class T> inline
155 T read(remote_ptr<T> ptr, int process_index = ProcessIndexMissing)
157 static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
159 return *(T*)this->read_bytes(&res, sizeof(T), ptr, process_index);