Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Make C++ classes out of addres_space, process, snapshot
[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 // Compatibility stuff, will be removed:
20 #define MC_ADDRESS_SPACE_READ_FLAGS_NONE ::simgrid::mc::AddressSpace::Normal
21 #define MC_ADDRESS_SPACE_READ_FLAGS_LAZY ::simgrid::mc::AddressSpace::Lazy
22
23 // Compatibility stuff, will be removed:
24 #define MC_PROCESS_INDEX_MISSING ::simgrid::mc::ProcessIndexMissing
25 #define MC_PROCESS_INDEX_DISABLED ::simgrid::mc::ProcessIndexDisabled
26 #define MC_PROCESS_INDEX_ANY ::simgrid::mc::ProcessIndexAny
27
28 namespace simgrid {
29 namespace mc {
30
31 /** Process index used when no process is available
32  *
33  *  The expected behaviour is that if a process index is needed it will fail.
34  * */
35 const int ProcessIndexMissing = -1;
36
37 /** Process index used when we don't care about the process index
38  * */
39 const int ProcessIndexDisabled = -2;
40
41 /** Constant used when any process will do.
42  *
43  *  This is is index of the first process.
44  */
45 const int ProcessIndexAny = 0;
46
47 class AddressSpace {
48 public:
49   enum ReadMode {
50     Normal,
51     /** Allows the `read_bytes` to return a pointer to another buffer
52      *  where the data ins available instead of copying the data into the buffer
53      */
54     Lazy
55   };
56   virtual ~AddressSpace();
57   virtual const void* read_bytes(void* buffer, std::size_t size,
58     std::uint64_t address, int process_index = ProcessIndexAny,
59     ReadMode mode = Normal) = 0;
60   template<class T>
61   T read(uint64_t address, int process_index = ProcessIndexMissing)
62   {
63     static_assert(std::is_trivial<T>::value, "Cannot read a non-trivial type");
64     T res;
65     this->read_bytes(&res, sizeof(T), address, process_index);
66     return res;
67   }
68 };
69
70 }
71 }
72
73 // Deprecated compatibility wrapper:
74 static inline
75 const void* MC_address_space_read(
76   mc_address_space_t address_space, simgrid::mc::AddressSpace::ReadMode mode,
77   void* target, const void* addr, size_t size,
78   int process_index)
79 {
80   return address_space->read_bytes(target, size, (std::uint64_t) addr,
81     process_index, mode);
82 }
83
84 #endif