Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
05665fb4d63eb064ef0683ea16177be7372df131
[simgrid.git] / src / mc / AddressSpace.hpp
1 /* Copyright (c) 2008-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
7 #ifndef MC_ADDRESS_SPACE_H
8 #define MC_ADDRESS_SPACE_H
9
10 #include <cstdint>
11 #include <type_traits>
12
13 #include <xbt/misc.h>
14
15 #include <stdint.h>
16
17 #include "mc_forward.h"
18
19 namespace simgrid {
20 namespace mc {
21
22 /** Pointer to a remote address-space (process, snapshot)
23  *
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.
26  */
27 template<class T> class remote_ptr {
28   std::uint64_t address_;
29 public:
30   remote_ptr() : address_(0) {}
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_; }
34   operator bool() const
35   {
36     return address_;
37   }
38   operator remote_ptr<void>() const
39   {
40     return remote_ptr<void>(address_);
41   }
42   remote_ptr<T> operator+(std::uint64_t n) const
43   {
44     return remote_ptr<T>(address_ + n * sizeof(T));
45   }
46   remote_ptr<T> operator-(std::uint64_t n) const
47   {
48     return remote_ptr<T>(address_ - n * sizeof(T));
49   }
50   remote_ptr<T>& operator+=(std::uint64_t n) const
51   {
52     address_ += n * sizeof(T);
53     return *this;
54   }
55   remote_ptr<T>& operator-=(std::uint64_t n) const
56   {
57     address_ -= n * sizeof(T);
58     return *this;
59   }
60 };
61
62 template<class T> inline
63 remote_ptr<T> remote(T *p)
64 {
65   return remote_ptr<T>(p);
66 }
67
68 template<class T=void> inline
69 remote_ptr<T> remote(uint64_t p)
70 {
71   return remote_ptr<T>(p);
72 }
73
74 /** Process index used when no process is available
75  *
76  *  The expected behaviour is that if a process index is needed it will fail.
77  * */
78 const int ProcessIndexMissing = -1;
79
80 /** Process index used when we don't care about the process index
81  * */
82 const int ProcessIndexDisabled = -2;
83
84 /** Constant used when any process will do.
85  *
86  *  This is is index of the first process.
87  */
88 const int ProcessIndexAny = 0;
89
90 class AddressSpace {
91 public:
92   enum ReadMode {
93     Normal,
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
96      */
97     Lazy
98   };
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;
103
104   template<class T> inline
105   void read(T *buffer, remote_ptr<T> ptr, int process_index = ProcessIndexAny)
106   {
107     this->read_bytes(buffer, sizeof(T), ptr, process_index);
108   }
109
110   template<class T> inline
111   T read(remote_ptr<T> ptr, int process_index = ProcessIndexMissing)
112   {
113     static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
114     T res;
115     return *(T*)this->read_bytes(&res, sizeof(T), ptr, process_index);
116   }
117 };
118
119 }
120 }
121
122 #endif