Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ad428e71b4983d8832c2e85d9f9baf6874978108
[simgrid.git] / src / mc / sosp / Region.hpp
1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #ifndef SIMGRID_MC_SOSP_REGION_HPP
7 #define SIMGRID_MC_SOSP_REGION_HPP
8
9 #include "src/mc/remote/RemotePtr.hpp"
10 #include "src/mc/sosp/ChunkedData.hpp"
11
12 #include <memory>
13 #include <vector>
14
15 namespace simgrid::mc {
16
17 enum class RegionType { Heap = 1, Data = 2 };
18
19 /** A copy/snapshot of a given memory region, where identical pages are stored only once */
20 class Region {
21 public:
22   static const RegionType HeapRegion    = RegionType::Heap;
23   static const RegionType DataRegion    = RegionType::Data;
24
25 private:
26   RegionType region_type_;
27   simgrid::mc::ObjectInformation* object_info_ = nullptr;
28
29   /** @brief  Virtual address of the region in the simulated process */
30   void* start_addr_ = nullptr;
31
32   /** @brief Size of the data region in bytes */
33   std::size_t size_ = 0;
34
35   ChunkedData chunks_;
36
37 public:
38   Region(RegionType type, void* start_addr, size_t size);
39   Region(Region const&) = delete;
40   Region& operator=(Region const&) = delete;
41   Region(Region&& that)            = delete;
42   Region& operator=(Region&& that) = delete;
43
44   // Data
45
46   ChunkedData const& get_chunks() const { return chunks_; }
47
48   simgrid::mc::ObjectInformation* object_info() const { return object_info_; }
49   void object_info(simgrid::mc::ObjectInformation* info) { object_info_ = info; }
50
51   // Other getters
52
53   RemotePtr<void> start() const { return remote(start_addr_); }
54   RemotePtr<void> end() const { return remote((char*)start_addr_ + size_); }
55   std::size_t size() const { return size_; }
56   RegionType region_type() const { return region_type_; }
57
58   bool contain(RemotePtr<void> p) const { return p >= start() && p < end(); }
59
60   /** @brief Restore a region from a snapshot */
61   void restore() const;
62
63   /** @brief Read memory that was snapshotted in this region
64    *
65    *  @param target  Buffer to store contiguously the value if it spans over several pages
66    *  @param addr    Process (non-snapshot) address of the data
67    *  @param size    Size of the data to read in bytes
68    *  @return Pointer where the data is located (either target buffer or original location)
69    */
70   void* read(void* target, const void* addr, std::size_t size) const;
71 };
72
73 } // namespace simgrid::mc
74
75 int MC_snapshot_region_memcmp(const void* addr1, const simgrid::mc::Region* region1, const void* addr2,
76                               const simgrid::mc::Region* region2, std::size_t size);
77
78 static XBT_ALWAYS_INLINE void* MC_region_read_pointer(const simgrid::mc::Region* region, const void* addr)
79 {
80   void* res;
81   return *(void**)region->read(&res, addr, sizeof(void*));
82 }
83
84 #endif