Debian Codegraph @

Source/Core/Common/ChunkFile.h

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 // Extremely simple serialization framework.
7
8 // (mis)-features:
9 // + Super fast
10 // + Very simple
11 // + Same code is used for serialization and deserializaition (in most cases)
12 // - Zero backwards/forwards compatibility
13 // - Serialization code for anything complex has to be manually written.
14
15 #include <array>
16 #include <cstddef>
17 #include <cstring>
18 #include <deque>
19 #include <list>
20 #include <map>
21 #include <optional>
22 #include <set>
23 #include <string>
24 #include <type_traits>
25 #include <utility>
26 #include <vector>
27
28 #include <fmt/format.h>
29
30 #include "Common/Assert.h"
31 #include "Common/CommonTypes.h"
32 #include "Common/EnumMap.h"
33 #include "Common/Flag.h"
34 #include "Common/Inline.h"
35
36 // Wrapper class
37 class PointerWrap
38 {
39 public:
40 enum class Mode
41 {
42 Read,
43 Write,
44 Measure,
45 Verify,
46 };
47
48 private:
49 u8** m_ptr_current;
50 u8* m_ptr_end;
51 Mode m_mode;
52
53 public:
54 PointerWrap(u8** ptr, size_t size, Mode mode)
55 : m_ptr_current(ptr), m_ptr_end(*ptr + size), m_mode(mode)
56 {
57 }
58
59 void SetMeasureMode() { m_mode = Mode::Measure; }
60 void SetVerifyMode() { m_mode = Mode::Verify; }
61 bool IsReadMode() const { return m_mode == Mode::Read; }
62 bool IsWriteMode() const { return m_mode == Mode::Write; }
63 bool IsMeasureMode() const { return m_mode == Mode::Measure; }
64 bool IsVerifyMode() const { return m_mode == Mode::Verify; }
65
66 template <typename K, class V>
67 void Do(std::map<K, V>& x)
68 {
69 u32 count = (u32)x.size();
70 Do(count);
71
72 switch (m_mode)
73 {
74 case Mode::Read:
75 for (x.clear(); count != 0; --count)
76 {
77 std::pair<K, V> pair;
78 Do(pair.first);
79 Do(pair.second);
80 x.insert(pair);
81 }
82 break;
83
84 case Mode::Write:
85 case Mode::Measure:
86 case Mode::Verify:
87 for (auto& elem : x)
88 {
89 Do(elem.first);
90 Do(elem.second);
91 }
92 break;
93 }
94 }
95
96 template <typename V>
97 void Do(std::set<V>& x)
98 {
99 u32 count = (u32)x.size();
100 Do(count);
101
102 switch (m_mode)
103 {
104 case Mode::Read:
105 for (x.clear(); count != 0; --count)
106 {
107 V value = {};
108 Do(value);
109 x.insert(value);
110 }
111 break;
112
113 case Mode::Write:
114 case Mode::Measure:
115 case Mode::Verify:
116 for (const V& val : x)
117 {
118 Do(val);
119 }
120 break;
121 }
122 }
123
124 template <typename T>
125 void Do(std::vector<T>& x)
126 {
127 DoContiguousContainer(x);
128 }
129
130 template <typename T>
131 void Do(std::list<T>& x)
132 {
133 DoContainer(x);
134 }
135
136 template <typename T>
137 void Do(std::deque<T>& x)
138 {
139 DoContainer(x);
140 }
141
142 template <typename T>
143 void Do(std::basic_string<T>& x)
144 {
145 DoContiguousContainer(x);
146 }
147
148 template <typename T, typename U>
149 void Do(std::pair<T, U>& x)
150 {
151 Do(x.first);
152 Do(x.second);
153 }
154
155 template <typename T>
156 void Do(std::optional<T>& x)
157 {
158 bool present = x.has_value();
159 Do(present);
160
161 switch (m_mode)
162 {
163 case Mode::Read:
164 if (present)
165 {
166 x = std::make_optional<T>();
167 Do(x.value());
168 }
169 else
170 {
171 x = std::nullopt;
172 }
173 break;
174
175 case Mode::Write:
176 case Mode::Measure:
177 case Mode::Verify:
178 if (present)
179 Do(x.value());
180
181 break;
182 }
183 }
184
185 template <typename T, std::size_t N>
186 void DoArray(std::array<T, N>& x)
187 {
188 DoArray(x.data(), static_cast<u32>(x.size()));
189 }
190
191 template <typename V, auto last_member, typename = decltype(last_member)>
192 void DoArray(Common::EnumMap<V, last_member>& x)
193 {
194 DoArray(x.data(), static_cast<u32>(x.size()));
195 }
196
197 template <typename T>
198 requires(std::is_trivially_copyable_v<T>)
199 void DoArray(T* x, u32 count)
200 {
201 DoVoid(x, count * sizeof(T));
202 }
203
204 template <typename T>
205 requires(!std::is_trivially_copyable_v<T>)
206 void DoArray(T* x, u32 count)
207 {
208 for (u32 i = 0; i < count; ++i)
209 Do(x[i]);
210 }
211
212 template <typename T, std::size_t N>
213 void DoArray(T (&arr)[N])
214 {
215 DoArray(arr, static_cast<u32>(N));
216 }
217
218 // The caller is required to inspect the mode of this PointerWrap
219 // and deal with the pointer returned from this function themself.
220 [[nodiscard]] u8* DoExternal(u32& count)
221 {
222 Do(count);
223 u8* current = *m_ptr_current;
224 *m_ptr_current += count;
225 if (!IsMeasureMode() && *m_ptr_current > m_ptr_end)
226 {
227 // trying to read/write past the end of the buffer, prevent this
228 SetMeasureMode();
229 }
230 return current;
231 }
232
233 // The reserved u32 is set to 0, and a pointer to it is returned.
234 // The caller needs to fill in the reserved u32 with the appropriate value later on, if they
235 // want a non-zero value there.
236 [[nodiscard]] u8* ReserveU32()
237 {
238 u32 temp = 0;
239 u8* previous_pointer = *m_ptr_current;
240 Do(temp);
241 return previous_pointer;
242 }
243
244 u32 GetOffsetFromPreviousPosition(u8* previous_pointer)
245 {
246 return static_cast<u32>((*m_ptr_current) - previous_pointer);
247 }
248
249 void Do(Common::Flag& flag)
250 {
251 bool s = flag.IsSet();
252 Do(s);
253 if (IsReadMode())
254 flag.Set(s);
255 }
256
257 template <typename T>
258 void Do(std::atomic<T>& atomic)
259 {
260 T temp = atomic.load(std::memory_order_relaxed);
261 Do(temp);
262 if (IsReadMode())
263 atomic.store(temp, std::memory_order_relaxed);
264 }
265
266 template <typename T>
267 requires(std::is_trivially_copyable_v<T> && !std::is_pointer_v<T>)
268 void Do(T& x)
269 {
270 // Note:
271 // Usually we can just use x = **ptr, etc. However, this doesn't work
272 // for unions containing BitFields (long story, stupid language rules)
273 // or arrays. This will get optimized anyway.
274 DoVoid((void*)&x, sizeof(x));
275 }
276
277 void Do(bool& x)
278 {
279 // bool's size can vary depending on platform, which can
280 // cause breakages. This treats all bools as if they were
281 // 8 bits in size.
282 u8 stable = static_cast<u8>(x);
283
284 Do(stable);
285
286 if (IsReadMode())
287 x = stable != 0;
288 }
289
290 template <typename T>
291 void DoPointer(T*& x, T* const base)
292 {
293 // pointers can be more than 2^31 apart, but you're using this function wrong if you need that
294 // much range
295 ptrdiff_t offset = x - base;
296 Do(offset);
297 if (IsReadMode())
298 {
299 x = base + offset;
300 }
301 }
302
303 void DoPointer(void*& x, void* const base)
304 {
305 auto* ptr = static_cast<u8*>(x);
306 DoPointer(ptr, static_cast<u8*>(base));
307 x = ptr;
308 }
309
310 void DoMarker(const std::string& prevName, u32 arbitraryNumber = 0x42)
311 {
312 u32 cookie = arbitraryNumber;
313 Do(cookie);
314
315 if (IsReadMode() && cookie != arbitraryNumber)
316 {
317 PanicAlertFmtT(
318 "Error: After \"{0}\", found {1} ({2:#x}) instead of save marker {3} ({4:#x}). Aborting "
319 "savestate load...",
320 prevName, cookie, cookie, arbitraryNumber, arbitraryNumber);
321 SetMeasureMode();
322 }
323 }
324
325 template <typename T, typename Functor>
326 void DoEachElement(T& container, Functor member)
327 {
328 u32 size = static_cast<u32>(container.size());
329 Do(size);
330 container.resize(size);
331
332 for (auto& elem : container)
333 member(*this, elem);
334 }
335
336 private:
337 template <typename T>
338 void DoContiguousContainer(T& container)
339 {
340 u32 size = static_cast<u32>(container.size());
341 Do(size);
342 container.resize(size);
343
344 if (size > 0)
345 DoArray(&container[0], size);
346 }
347
348 template <typename T>
349 void DoContainer(T& x)
350 {
351 DoEachElement(x, [](PointerWrap& p, typename T::value_type& elem) { p.Do(elem); });
352 }
353
354 DOLPHIN_FORCE_INLINE void DoVoid(void* data, u32 size)
355 {
356 if (!IsMeasureMode() && (*m_ptr_current + size) > m_ptr_end)
357 {
358 // trying to read/write past the end of the buffer, prevent this
359 SetMeasureMode();
360 }
361
362 switch (m_mode)
363 {
364 case Mode::Read:
365 memcpy(data, *m_ptr_current, size);
366 break;
367
368 case Mode::Write:
369 memcpy(*m_ptr_current, data, size);
370 break;
371
372 case Mode::Measure:
373 break;
374
375 case Mode::Verify:
376 DEBUG_ASSERT_MSG(COMMON, !memcmp(data, *m_ptr_current, size),
377 "Savestate verification failure: buf {} != {} (size {}).\n", fmt::ptr(data),
378 fmt::ptr(*m_ptr_current), size);
379 break;
380 }
381
382 *m_ptr_current += size;
383 }
384 };