Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
mc: Also remove the process_index
[simgrid.git] / src / mc / sosp / ChunkedData.hpp
1 /* Copyright (c) 2014-2019. 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_CHUNKED_DATA_HPP
7 #define SIMGRID_MC_CHUNKED_DATA_HPP
8
9 #include <vector>
10
11 #include "src/mc/mc_forward.hpp"
12 #include "src/mc/sosp/PageStore.hpp"
13
14 namespace simgrid {
15 namespace mc {
16
17 /** A byte-string represented as a sequence of chunks from a PageStore
18  *
19  *  In order to save memory when taking memory snapshots, a given byte-string
20  *  is split in fixed-size chunks. Identical chunks (either from the same
21  *  snapshot or more probably from different snpashots) share the same memory
22  *  storage.
23  *
24  *  Thus a chunked is represented as a sequence of indices of each chunk.
25  */
26 class ChunkedData {
27   /** This is where we store the chunks */
28   PageStore* store_ = nullptr;
29   /** Indices of the chunks in the `PageStore` */
30   std::vector<std::size_t> pagenos_;
31
32 public:
33   ChunkedData() = default;
34   void clear()
35   {
36     for (std::size_t const& pageno : pagenos_)
37       store_->unref_page(pageno);
38     pagenos_.clear();
39   }
40   ~ChunkedData() { clear(); }
41
42   // Copy and move
43   ChunkedData(ChunkedData const& that) : store_(that.store_), pagenos_(that.pagenos_)
44   {
45     for (std::size_t const& pageno : pagenos_)
46       store_->ref_page(pageno);
47   }
48   ChunkedData(ChunkedData&& that) : store_(that.store_), pagenos_(std::move(that.pagenos_))
49   {
50     that.store_ = nullptr;
51     that.pagenos_.clear();
52   }
53   ChunkedData& operator=(ChunkedData const& that)
54   {
55     if (this != &that) {
56       this->clear();
57       store_   = that.store_;
58       pagenos_ = that.pagenos_;
59       for (std::size_t const& pageno : pagenos_)
60         store_->ref_page(pageno);
61     }
62     return *this;
63   }
64   ChunkedData& operator=(ChunkedData&& that)
65   {
66     this->clear();
67     store_      = that.store_;
68     that.store_ = nullptr;
69     pagenos_    = std::move(that.pagenos_);
70     that.pagenos_.clear();
71     return *this;
72   }
73
74   /** How many pages are used */
75   std::size_t page_count() const { return pagenos_.size(); }
76
77   /** Get a chunk index */
78   std::size_t pageno(std::size_t i) const { return pagenos_[i]; }
79
80   /** Get a view of the chunk indices */
81   const std::size_t* pagenos() const { return pagenos_.data(); }
82
83   /** Get a a pointer to a chunk */
84   const void* page(std::size_t i) const { return store_->get_page(pagenos_[i]); }
85
86   ChunkedData(PageStore& store, AddressSpace& as, RemotePtr<void> addr, std::size_t page_count);
87 };
88
89 } // namespace mc
90 } // namespace simgrid
91
92 #endif