Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Cleanup/documentation for simgrid::mc::Frame
[simgrid.git] / src / mc / AddressSpace.hpp
1 /* Copyright (c) 2008-2015. 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 SIMGRID_MC_ADDRESS_SPACE_H
8 #define SIMGRID_MC_ADDRESS_SPACE_H
9
10 #include <cstddef>
11 #include <cstdint>
12 #include <type_traits>
13
14 #include <xbt/misc.h>
15
16 #include "src/mc/mc_forward.hpp"
17 #include "src/mc/remote_ptr.hpp"
18
19 namespace simgrid {
20 namespace mc {
21
22 /** Process index used when no process is available
23  *
24  *  The expected behaviour is that if a process index is needed it will fail.
25  * */
26 const int ProcessIndexMissing = -1;
27
28 /** Process index used when we don't care about the process index
29  * */
30 const int ProcessIndexDisabled = -2;
31
32 /** Constant used when any process will do.
33  *
34  *  This is is index of the first process.
35  */
36 const int ProcessIndexAny = 0;
37
38 /** Options for read operations
39  *
40  *  This is a set of flags managed with bitwise operators. Only the
41  *  meaningful operations are defined: addition, conversions to/from
42  *  integers are not allowed.
43  */
44 class ReadOptions {
45   std::uint32_t value_;
46   constexpr explicit ReadOptions(std::uint32_t value) : value_(value) {}
47 public:
48   constexpr ReadOptions() : value_(0) {}
49
50   constexpr operator bool() const { return value_ != 0; }
51   constexpr bool operator!() const { return value_ == 0; }
52
53   constexpr ReadOptions operator|(ReadOptions const& that) const
54   {
55     return ReadOptions(value_ | that.value_);
56   }
57   constexpr ReadOptions operator&(ReadOptions const& that) const
58   {
59     return ReadOptions(value_ & that.value_);
60   }
61   constexpr ReadOptions operator^(ReadOptions const& that) const
62   {
63     return ReadOptions(value_ ^ that.value_);
64   }
65   constexpr ReadOptions operator~() const
66   {
67     return ReadOptions(~value_);
68   }
69
70   ReadOptions& operator|=(ReadOptions const& that)
71   {
72     value_ |= that.value_;
73     return *this;
74   }
75   ReadOptions& operator&=(ReadOptions const& that)
76   {
77     value_ &= that.value_;
78     return *this;
79   }
80   ReadOptions& operator^=(ReadOptions const& that)
81   {
82     value_ &= that.value_;
83     return *this;
84   }
85
86   /** Copy the data to the given buffer */
87   static constexpr ReadOptions none() { return ReadOptions(0); }
88
89   /** Allows to return a pointer to another buffer where the data is
90    *  available instead of copying the data into the buffer
91    */
92   static constexpr ReadOptions lazy() { return ReadOptions(1); }
93 };
94
95 /** A given state of a given process (abstract base class)
96  *
97  *  Currently, this might either be:
98  *
99  *  * the current state of an existing process;
100  *
101  *  * a snapshot.
102  */
103 class AddressSpace {
104 private:
105   Process* process_;
106 public:
107   AddressSpace(Process* process) : process_(process) {}
108   virtual ~AddressSpace();
109
110   simgrid::mc::Process* process() const { return process_; }
111
112   /** Read data from the address space
113    *
114    *  @param buffer        target buffer for the data
115    *  @param size          number of bytes
116    *  @param address       remote source address of the data
117    *  @param process_index which process (used for SMPI privatization)
118    *  @param options
119    */
120   virtual const void* read_bytes(void* buffer, std::size_t size,
121     remote_ptr<void> address, int process_index = ProcessIndexAny,
122     ReadOptions options = ReadOptions::none()) const = 0;
123
124   /** Read a given data structure from the address space */
125   template<class T> inline
126   void read(T *buffer, remote_ptr<T> ptr, int process_index = ProcessIndexAny)
127   {
128     this->read_bytes(buffer, sizeof(T), ptr, process_index);
129   }
130
131   /** Read a given data structure from the address space */
132   template<class T> inline
133   T read(remote_ptr<T> ptr, int process_index = ProcessIndexMissing)
134   {
135     static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
136     T res;
137     return *(T*)this->read_bytes(&res, sizeof(T), ptr, process_index);
138   }
139 };
140
141 }
142 }
143
144 #endif