Logo AND Algorithmique Numérique Distribuée

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