Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / mc / ChunkedData.hpp
1 /* Copyright (c) 2014-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef SIMGRID_MC_CHUNKED_DATA_HPP
8 #define SIMGRID_MC_CHUNKED_DATA_HPP
9
10 #include <cstddef>
11 #include <cstdint>
12
13 #include <utility>
14 #include <vector>
15
16 #include "src/mc/mc_forward.hpp"
17 #include "src/mc/PageStore.hpp"
18
19 namespace simgrid {
20 namespace mc {
21
22 /** A byte-string represented as a sequence of chunks from a PageStore
23  *
24  *  In order to save memory when taking memory snapshots, a given byte-string
25  *  is split in fixed-size chunks. Identical chunks (either from the same
26  *  snapshot or more probably from different snpashots) share the same memory
27  *  storage.
28  *
29  *  Thus a chunked is represented as a sequence of indices of each chunk.
30  */
31 class ChunkedData {
32   /** This is where we store the chunks */
33   PageStore* store_ = nullptr;
34   /** Indices of the chunks in the `PageStore` */
35   std::vector<std::size_t> pagenos_;
36 public:
37
38   ChunkedData() {}
39   void clear()
40   {
41     for (std::size_t pageno : pagenos_)
42       store_->unref_page(pageno);
43     pagenos_.clear();
44   }
45   ~ChunkedData()
46   {
47     clear();
48   }
49
50   // Copy and move
51   ChunkedData(ChunkedData const& that)
52   {
53     store_ = that.store_;
54     pagenos_ = that.pagenos_;
55     for (std::size_t pageno : pagenos_)
56       store_->ref_page(pageno);
57   }
58   ChunkedData(ChunkedData&& that)
59   {
60     store_ = that.store_;
61     that.store_ = nullptr;
62     pagenos_ = std::move(that.pagenos_);
63     that.pagenos_.clear();
64   }
65   ChunkedData& operator=(ChunkedData const& that)
66   {
67     this->clear();
68     store_ = that.store_;
69     pagenos_ = that.pagenos_;
70     for (std::size_t pageno : pagenos_)
71       store_->ref_page(pageno);
72     return *this;
73   }
74   ChunkedData& operator=(ChunkedData && that)
75   {
76     this->clear();
77     store_ = that.store_;
78     that.store_ = nullptr;
79     pagenos_ = std::move(that.pagenos_);
80     that.pagenos_.clear();
81     return *this;
82   }
83
84   /** How many pages are used */
85   std::size_t page_count()          const { return pagenos_.size(); }
86
87   /** Get a chunk index */
88   std::size_t pageno(std::size_t i) const { return pagenos_[i]; }
89
90   /** Get a view of the chunk indices */
91   const std::size_t* pagenos()      const { return pagenos_.data(); }
92
93   /** Get a a pointer to a chunk */
94   const void* page(std::size_t i) const
95   {
96     return store_->get_page(pagenos_[i]);
97   }
98
99   ChunkedData(PageStore& store, AddressSpace& as,
100     RemotePtr<void> addr, std::size_t page_count);
101 };
102
103 }
104 }
105
106 #endif