1 /* Copyright (c) 2014-2023. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_MC_CHUNKED_DATA_HPP
7 #define SIMGRID_MC_CHUNKED_DATA_HPP
11 #include "src/mc/mc_forward.hpp"
12 #include "src/mc/remote/RemotePtr.hpp"
13 #include "src/mc/sosp/PageStore.hpp"
15 namespace simgrid::mc {
17 /** A byte-string represented as a sequence of chunks from a PageStore
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 snapshots) share the same memory
24 * Thus a chunked is represented as a sequence of indices of each chunk.
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_;
33 ChunkedData() = default;
36 for (std::size_t const& pageno : pagenos_)
37 store_->unref_page(pageno);
40 ~ChunkedData() { clear(); }
43 ChunkedData(ChunkedData const& that) : store_(that.store_), pagenos_(that.pagenos_)
45 for (std::size_t const& pageno : pagenos_)
46 store_->ref_page(pageno);
48 ChunkedData(ChunkedData&& that) noexcept : pagenos_(std::move(that.pagenos_))
50 std::swap(store_, that.store_);
51 that.pagenos_.clear();
53 ChunkedData& operator=(ChunkedData const& that)
58 pagenos_ = that.pagenos_;
59 for (std::size_t const& pageno : pagenos_)
60 store_->ref_page(pageno);
64 ChunkedData& operator=(ChunkedData&& that) noexcept
69 that.store_ = nullptr;
70 pagenos_ = std::move(that.pagenos_);
71 that.pagenos_.clear();
76 /** How many pages are used */
77 std::size_t page_count() const { return pagenos_.size(); }
79 /** Get a chunk index */
80 std::size_t pageno(std::size_t i) const { return pagenos_[i]; }
82 /** Get a view of the chunk indices */
83 const std::size_t* pagenos() const { return pagenos_.data(); }
85 /** Get a pointer to a chunk */
86 void* page(std::size_t i) const { return store_->get_page(pagenos_[i]); }
88 ChunkedData(PageStore& store, const AddressSpace& as, RemotePtr<void> addr, std::size_t page_count);
91 } // namespace simgrid::mc