Logo AND Algorithmique Numérique Distribuée

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