Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sonar don't like comments ending with ';'
[simgrid.git] / src / mc / ChunkedData.hpp
1 /* Copyright (c) 2014-2017. 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/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 public:
36
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()
45   {
46     clear();
47   }
48
49   // Copy and move
50   ChunkedData(ChunkedData const& that)
51      : store_ (that.store_)
52      , pagenos_(that.pagenos_)
53   {
54     for (std::size_t const& pageno : pagenos_)
55       store_->ref_page(pageno);
56   }
57   ChunkedData(ChunkedData&& that)
58      : store_(that.store_)
59      , pagenos_(std::move(that.pagenos_))
60   {
61     that.store_ = nullptr;
62     that.pagenos_.clear();
63   }
64   ChunkedData& operator=(ChunkedData const& that)
65   {
66     this->clear();
67     store_ = that.store_;
68     pagenos_ = that.pagenos_;
69     for (std::size_t const& pageno : pagenos_)
70       store_->ref_page(pageno);
71     return *this;
72   }
73   ChunkedData& operator=(ChunkedData && that)
74   {
75     this->clear();
76     store_ = that.store_;
77     that.store_ = nullptr;
78     pagenos_ = std::move(that.pagenos_);
79     that.pagenos_.clear();
80     return *this;
81   }
82
83   /** How many pages are used */
84   std::size_t page_count()          const { return pagenos_.size(); }
85
86   /** Get a chunk index */
87   std::size_t pageno(std::size_t i) const { return pagenos_[i]; }
88
89   /** Get a view of the chunk indices */
90   const std::size_t* pagenos()      const { return pagenos_.data(); }
91
92   /** Get a a pointer to a chunk */
93   const void* page(std::size_t i) const
94   {
95     return store_->get_page(pagenos_[i]);
96   }
97
98   ChunkedData(PageStore& store, AddressSpace& as,
99     RemotePtr<void> addr, std::size_t page_count);
100 };
101
102 }
103 }
104
105 #endif