1 /* Copyright (c) 2008-2014. 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 MC_ADDRESS_SPACE_H
8 #define MC_ADDRESS_SPACE_H
11 #include <type_traits>
17 #include "mc_forward.h"
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_(nullptr) {}
31 remote_ptr(std::uint64_t address) : address_(address) {}
32 remote_ptr(T* address) : address_((std::uint64_t)address) {}
33 std::uint64_t address() const { return address_; }
38 operator remote_ptr<void>() const
40 return remote_ptr<void>(address_);
42 remote_ptr<T> operator+(std::uint64_t n) const
44 return remote_ptr<T>(address_ + n * sizeof(T));
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 address_ += n * sizeof(T);
55 remote_ptr<T>& operator-=(std::uint64_t n) const
57 address_ -= n * sizeof(T);
62 template<class T> inline
63 remote_ptr<T> remote(T *p)
65 return remote_ptr<T>(p);
68 template<class T=void> inline
69 remote_ptr<T> remote(uint64_t p)
71 return remote_ptr<T>(p);
74 /** Process index used when no process is available
76 * The expected behaviour is that if a process index is needed it will fail.
78 const int ProcessIndexMissing = -1;
80 /** Process index used when we don't care about the process index
82 const int ProcessIndexDisabled = -2;
84 /** Constant used when any process will do.
86 * This is is index of the first process.
88 const int ProcessIndexAny = 0;
94 /** Allows the `read_bytes` to return a pointer to another buffer
95 * where the data ins available instead of copying the data into the buffer
99 virtual ~AddressSpace();
100 virtual const void* read_bytes(void* buffer, std::size_t size,
101 remote_ptr<void> address, int process_index = ProcessIndexAny,
102 ReadMode mode = Normal) const = 0;
104 template<class T> inline
105 void read(T *buffer, remote_ptr<T> ptr, int process_index = ProcessIndexAny)
107 this->read_bytes(buffer, sizeof(T), ptr, process_index);
110 template<class T> inline
111 T read(remote_ptr<T> ptr, int process_index = ProcessIndexMissing)
113 static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
115 return *(T*)this->read_bytes(&res, sizeof(T), ptr, process_index);