RavEngine
Loading...
Searching...
No Matches
phmap_dump.h
1#if !defined(phmap_dump_h_guard_)
2#define phmap_dump_h_guard_
3
4// ---------------------------------------------------------------------------
5// Copyright (c) 2019, Gregory Popovitch - greg7mdp@gmail.com
6//
7// providing dump/load/mmap_load
8//
9// Licensed under the Apache License, Version 2.0 (the "License");
10// you may not use this file except in compliance with the License.
11// You may obtain a copy of the License at
12//
13// https://www.apache.org/licenses/LICENSE-2.0
14//
15// Unless required by applicable law or agreed to in writing, software
16// distributed under the License is distributed on an "AS IS" BASIS,
17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18// See the License for the specific language governing permissions and
19// limitations under the License.
20// ---------------------------------------------------------------------------
21
22#include <iostream>
23#include <fstream>
24#include <sstream>
25#include "phmap.h"
26namespace phmap
27{
28
29namespace type_traits_internal {
30
31#if defined(__GLIBCXX__) && __GLIBCXX__ < 20150801
32 template<typename T> struct IsTriviallyCopyable : public std::integral_constant<bool, __has_trivial_copy(T)> {};
33#else
34 template<typename T> struct IsTriviallyCopyable : public std::is_trivially_copyable<T> {};
35#endif
36
37template <class T1, class T2>
38struct IsTriviallyCopyable<std::pair<T1, T2>> {
40};
41}
42
43namespace priv {
44
45// ------------------------------------------------------------------------
46// dump/load for raw_hash_set
47// ------------------------------------------------------------------------
48template <class Policy, class Hash, class Eq, class Alloc>
49template<typename OutputArchive>
50bool raw_hash_set<Policy, Hash, Eq, Alloc>::dump(OutputArchive& ar) const {
52 "value_type should be trivially copyable");
53
54 if (!ar.dump(size_)) {
55 std::cerr << "Failed to dump size_" << std::endl;
56 return false;
57 }
58 if (size_ == 0) {
59 return true;
60 }
61 if (!ar.dump(capacity_)) {
62 std::cerr << "Failed to dump capacity_" << std::endl;
63 return false;
64 }
65 if (!ar.dump(reinterpret_cast<char*>(ctrl_),
66 sizeof(ctrl_t) * (capacity_ + Group::kWidth + 1))) {
67
68 std::cerr << "Failed to dump ctrl_" << std::endl;
69 return false;
70 }
71 if (!ar.dump(reinterpret_cast<char*>(slots_),
72 sizeof(slot_type) * capacity_)) {
73 std::cerr << "Failed to dump slot_" << std::endl;
74 return false;
75 }
76 return true;
77}
78
79template <class Policy, class Hash, class Eq, class Alloc>
80template<typename InputArchive>
81bool raw_hash_set<Policy, Hash, Eq, Alloc>::load(InputArchive& ar) {
82 static_assert(type_traits_internal::IsTriviallyCopyable<value_type>::value,
83 "value_type should be trivially copyable");
84 raw_hash_set<Policy, Hash, Eq, Alloc>().swap(*this); // clear any existing content
85 if (!ar.load(&size_)) {
86 std::cerr << "Failed to load size_" << std::endl;
87 return false;
88 }
89 if (size_ == 0) {
90 return true;
91 }
92 if (!ar.load(&capacity_)) {
93 std::cerr << "Failed to load capacity_" << std::endl;
94 return false;
95 }
96
97 // allocate memory for ctrl_ and slots_
98 initialize_slots();
99 if (!ar.load(reinterpret_cast<char*>(ctrl_),
100 sizeof(ctrl_t) * (capacity_ + Group::kWidth + 1))) {
101 std::cerr << "Failed to load ctrl" << std::endl;
102 return false;
103 }
104 if (!ar.load(reinterpret_cast<char*>(slots_),
105 sizeof(slot_type) * capacity_)) {
106 std::cerr << "Failed to load slot" << std::endl;
107 return false;
108 }
109 return true;
110}
111
112// ------------------------------------------------------------------------
113// dump/load for parallel_hash_set
114// ------------------------------------------------------------------------
115template <size_t N,
116 template <class, class, class, class> class RefSet,
117 class Mtx_,
118 class Policy, class Hash, class Eq, class Alloc>
119template<typename OutputArchive>
120bool parallel_hash_set<N, RefSet, Mtx_, Policy, Hash, Eq, Alloc>::dump(OutputArchive& ar) const {
121 static_assert(type_traits_internal::IsTriviallyCopyable<value_type>::value,
122 "value_type should be trivially copyable");
123
124 if (! ar.dump(subcnt())) {
125 std::cerr << "Failed to dump meta!" << std::endl;
126 return false;
127 }
128 for (size_t i = 0; i < sets_.size(); ++i) {
129 auto& inner = sets_[i];
130 typename Lockable::UniqueLock m(const_cast<Inner&>(inner));
131 if (!inner.set_.dump(ar)) {
132 std::cerr << "Failed to dump submap " << i << std::endl;
133 return false;
134 }
135 }
136 return true;
137}
138
139template <size_t N,
140 template <class, class, class, class> class RefSet,
141 class Mtx_,
142 class Policy, class Hash, class Eq, class Alloc>
143template<typename InputArchive>
144bool parallel_hash_set<N, RefSet, Mtx_, Policy, Hash, Eq, Alloc>::load(InputArchive& ar) {
145 static_assert(type_traits_internal::IsTriviallyCopyable<value_type>::value,
146 "value_type should be trivially copyable");
147
148 size_t submap_count = 0;
149 if (!ar.load(&submap_count)) {
150 std::cerr << "Failed to load submap count!" << std::endl;
151 return false;
152 }
153
154 if (submap_count != subcnt()) {
155 std::cerr << "submap count(" << submap_count << ") != N(" << N << ")" << std::endl;
156 return false;
157 }
158
159 for (size_t i = 0; i < submap_count; ++i) {
160 auto& inner = sets_[i];
161 typename Lockable::UniqueLock m(const_cast<Inner&>(inner));
162 if (!inner.set_.load(ar)) {
163 std::cerr << "Failed to load submap " << i << std::endl;
164 return false;
165 }
166 }
167 return true;
168}
169} // namespace priv
170
171
172
173// ------------------------------------------------------------------------
174// BinaryArchive
175// File is closed when archive object is destroyed
176// ------------------------------------------------------------------------
177
178// ------------------------------------------------------------------------
179// ------------------------------------------------------------------------
181public:
182 BinaryOutputArchive(const char *file_path) {
183 ofs_.open(file_path, std::ios_base::binary);
184 }
185
186 bool dump(const char *p, size_t sz) {
187 ofs_.write(p, sz);
188 return true;
189 }
190
191 template<typename V>
192 typename std::enable_if<type_traits_internal::IsTriviallyCopyable<V>::value, bool>::type
193 dump(const V& v) {
194 ofs_.write(reinterpret_cast<const char *>(&v), sizeof(V));
195 return true;
196 }
197
198private:
199 std::ofstream ofs_;
200};
201
202
204public:
205 BinaryInputArchive(const char * file_path) {
206 ifs_.open(file_path, std::ios_base::binary);
207 }
208
209 bool load(char* p, size_t sz) {
210 ifs_.read(p, sz);
211 return true;
212 }
213
214 template<typename V>
215 typename std::enable_if<type_traits_internal::IsTriviallyCopyable<V>::value, bool>::type
216 load(V* v) {
217 ifs_.read(reinterpret_cast<char *>(v), sizeof(V));
218 return true;
219 }
220
221private:
222 std::ifstream ifs_;
223};
224
225} // namespace phmap
226
227#endif // phmap_dump_h_guard_
Definition phmap_dump.h:203
Definition phmap_dump.h:180