Logo AND Algorithmique Numérique Distribuée

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