2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2008 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2014-02-10 11:54:46 -07:00
|
|
|
#pragma once
|
2008-12-07 21:46:09 -07:00
|
|
|
|
|
|
|
// Extremely simple serialization framework.
|
|
|
|
|
|
|
|
// (mis)-features:
|
|
|
|
// + Super fast
|
|
|
|
// + Very simple
|
|
|
|
// + Same code is used for serialization and deserializaition (in most cases)
|
|
|
|
// - Zero backwards/forwards compatibility
|
|
|
|
// - Serialization code for anything complex has to be manually written.
|
|
|
|
|
2014-09-20 05:32:04 -06:00
|
|
|
#include <array>
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <cstddef>
|
2017-09-09 13:52:35 -06:00
|
|
|
#include <cstring>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <deque>
|
|
|
|
#include <list>
|
2008-12-07 21:46:09 -07:00
|
|
|
#include <map>
|
2013-08-25 18:06:58 -06:00
|
|
|
#include <set>
|
2008-12-07 21:46:09 -07:00
|
|
|
#include <string>
|
2013-04-09 17:57:39 -06:00
|
|
|
#include <type_traits>
|
2014-02-19 20:11:52 -07:00
|
|
|
#include <utility>
|
2014-02-17 03:18:15 -07:00
|
|
|
#include <vector>
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2015-09-26 14:39:47 -06:00
|
|
|
#include "Common/Assert.h"
|
2014-09-07 19:06:58 -06:00
|
|
|
#include "Common/CommonTypes.h"
|
2018-06-09 09:34:43 -06:00
|
|
|
#include "Common/Compiler.h"
|
2014-08-24 17:38:33 -06:00
|
|
|
#include "Common/Flag.h"
|
2015-09-26 14:39:47 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2008-12-07 21:46:09 -07:00
|
|
|
|
2017-12-25 18:18:13 -07:00
|
|
|
// XXX: Replace this with std::is_trivially_copyable<T> once we stop using volatile
|
|
|
|
// on things that are put in savestates, as volatile types are not trivially copyable.
|
|
|
|
template <typename T>
|
|
|
|
constexpr bool IsTriviallyCopyable = std::is_trivially_copyable<std::remove_volatile_t<T>>::value;
|
2013-08-25 18:06:58 -06:00
|
|
|
|
2009-02-27 18:26:56 -07:00
|
|
|
// Wrapper class
|
2008-12-07 21:46:09 -07:00
|
|
|
class PointerWrap
|
|
|
|
{
|
|
|
|
public:
|
2013-03-02 02:28:44 -07:00
|
|
|
enum Mode
|
|
|
|
{
|
2010-04-17 15:02:03 -06:00
|
|
|
MODE_READ = 1, // load
|
|
|
|
MODE_WRITE, // save
|
|
|
|
MODE_MEASURE, // calculate size
|
|
|
|
MODE_VERIFY, // compare
|
2008-12-07 21:46:09 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
u8** ptr;
|
|
|
|
Mode mode;
|
|
|
|
|
|
|
|
public:
|
|
|
|
PointerWrap(u8** ptr_, Mode mode_) : ptr(ptr_), mode(mode_) {}
|
2013-03-02 02:28:44 -07:00
|
|
|
void SetMode(Mode mode_) { mode = mode_; }
|
|
|
|
Mode GetMode() const { return mode; }
|
|
|
|
template <typename K, class V>
|
|
|
|
void Do(std::map<K, V>& x)
|
2008-12-07 21:46:09 -07:00
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
u32 count = (u32)x.size();
|
|
|
|
Do(count);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
switch (mode)
|
|
|
|
{
|
2009-11-24 10:10:38 -07:00
|
|
|
case MODE_READ:
|
2013-03-02 02:28:44 -07:00
|
|
|
for (x.clear(); count != 0; --count)
|
2009-11-24 10:10:38 -07:00
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
std::pair<K, V> pair;
|
|
|
|
Do(pair.first);
|
|
|
|
Do(pair.second);
|
|
|
|
x.insert(pair);
|
2009-11-24 10:10:38 -07:00
|
|
|
}
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2009-11-24 10:10:38 -07:00
|
|
|
case MODE_WRITE:
|
|
|
|
case MODE_MEASURE:
|
2010-04-17 15:02:03 -06:00
|
|
|
case MODE_VERIFY:
|
2013-10-28 23:09:01 -06:00
|
|
|
for (auto& elem : x)
|
2009-11-24 10:10:38 -07:00
|
|
|
{
|
2013-10-28 23:09:01 -06:00
|
|
|
Do(elem.first);
|
|
|
|
Do(elem.second);
|
2009-11-24 10:10:38 -07:00
|
|
|
}
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2009-11-24 10:10:38 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-08-25 18:06:58 -06:00
|
|
|
template <typename V>
|
|
|
|
void Do(std::set<V>& x)
|
|
|
|
{
|
|
|
|
u32 count = (u32)x.size();
|
|
|
|
Do(count);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-08-25 18:06:58 -06:00
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case MODE_READ:
|
|
|
|
for (x.clear(); count != 0; --count)
|
|
|
|
{
|
|
|
|
V value;
|
|
|
|
Do(value);
|
|
|
|
x.insert(value);
|
|
|
|
}
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-08-25 18:06:58 -06:00
|
|
|
case MODE_WRITE:
|
|
|
|
case MODE_MEASURE:
|
|
|
|
case MODE_VERIFY:
|
2017-01-14 09:38:35 -07:00
|
|
|
for (const V& val : x)
|
2013-08-25 18:06:58 -06:00
|
|
|
{
|
2014-02-12 08:00:34 -07:00
|
|
|
Do(val);
|
2013-08-25 18:06:58 -06:00
|
|
|
}
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2013-08-25 18:06:58 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::vector<T>& x)
|
2011-03-22 01:27:23 -06:00
|
|
|
{
|
2019-01-31 19:02:22 -07:00
|
|
|
DoContiguousContainer(x);
|
2008-12-07 21:46:09 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::list<T>& x)
|
2011-05-27 13:55:07 -06:00
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
DoContainer(x);
|
2011-05-27 13:55:07 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::deque<T>& x)
|
2008-12-07 21:46:09 -07:00
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
DoContainer(x);
|
2008-12-07 21:46:09 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::basic_string<T>& x)
|
2011-12-18 23:01:46 -07:00
|
|
|
{
|
2019-01-31 19:02:22 -07:00
|
|
|
DoContiguousContainer(x);
|
2011-12-18 23:01:46 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-08-25 18:06:58 -06:00
|
|
|
template <typename T, typename U>
|
|
|
|
void Do(std::pair<T, U>& x)
|
|
|
|
{
|
|
|
|
Do(x.first);
|
|
|
|
Do(x.second);
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-09-20 05:32:04 -06:00
|
|
|
template <typename T, std::size_t N>
|
2015-02-15 12:43:31 -07:00
|
|
|
void DoArray(std::array<T, N>& x)
|
2014-09-20 05:32:04 -06:00
|
|
|
{
|
2019-01-31 19:02:22 -07:00
|
|
|
DoArray(x.data(), static_cast<u32>(x.size()));
|
2014-09-20 05:32:04 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-01-31 19:02:22 -07:00
|
|
|
template <typename T, typename std::enable_if_t<IsTriviallyCopyable<T>, int> = 0>
|
2013-03-02 02:28:44 -07:00
|
|
|
void DoArray(T* x, u32 count)
|
|
|
|
{
|
2014-08-27 17:50:52 -06:00
|
|
|
DoVoid(x, count * sizeof(T));
|
2013-03-19 19:51:12 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2019-01-31 19:02:22 -07:00
|
|
|
template <typename T, typename std::enable_if_t<!IsTriviallyCopyable<T>, int> = 0>
|
|
|
|
void DoArray(T* x, u32 count)
|
|
|
|
{
|
|
|
|
for (u32 i = 0; i < count; ++i)
|
|
|
|
Do(x[i]);
|
|
|
|
}
|
|
|
|
|
2015-09-29 10:35:30 -06:00
|
|
|
template <typename T, std::size_t N>
|
|
|
|
void DoArray(T (&arr)[N])
|
|
|
|
{
|
|
|
|
DoArray(arr, static_cast<u32>(N));
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-08-24 17:38:33 -06:00
|
|
|
void Do(Common::Flag& flag)
|
|
|
|
{
|
|
|
|
bool s = flag.IsSet();
|
|
|
|
Do(s);
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
flag.Set(s);
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2015-03-31 02:09:33 -06:00
|
|
|
template <typename T>
|
|
|
|
void Do(std::atomic<T>& atomic)
|
|
|
|
{
|
|
|
|
T temp = atomic.load();
|
|
|
|
Do(temp);
|
|
|
|
if (mode == MODE_READ)
|
|
|
|
atomic.store(temp);
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename T>
|
|
|
|
void Do(T& x)
|
|
|
|
{
|
2017-12-25 18:18:13 -07:00
|
|
|
static_assert(IsTriviallyCopyable<T>, "Only sane for trivially copyable types");
|
2014-08-27 17:50:52 -06:00
|
|
|
// Note:
|
|
|
|
// Usually we can just use x = **ptr, etc. However, this doesn't work
|
|
|
|
// for unions containing BitFields (long story, stupid language rules)
|
|
|
|
// or arrays. This will get optimized anyway.
|
2013-03-02 02:28:44 -07:00
|
|
|
DoVoid((void*)&x, sizeof(x));
|
2008-12-07 21:46:09 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-04-09 17:57:39 -06:00
|
|
|
template <typename T>
|
|
|
|
void DoPOD(T& x)
|
|
|
|
{
|
|
|
|
DoVoid((void*)&x, sizeof(x));
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-04-14 20:22:34 -06:00
|
|
|
void Do(bool& x)
|
|
|
|
{
|
|
|
|
// bool's size can vary depending on platform, which can
|
|
|
|
// cause breakages. This treats all bools as if they were
|
|
|
|
// 8 bits in size.
|
|
|
|
u8 stable = static_cast<u8>(x);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-04-14 20:22:34 -06:00
|
|
|
Do(stable);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-04-14 20:22:34 -06:00
|
|
|
if (mode == MODE_READ)
|
|
|
|
x = stable != 0;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
template <typename T>
|
|
|
|
void DoPointer(T*& x, T* const base)
|
|
|
|
{
|
2012-01-04 01:42:22 -07:00
|
|
|
// pointers can be more than 2^31 apart, but you're using this function wrong if you need that
|
|
|
|
// much range
|
2013-11-13 02:03:46 -07:00
|
|
|
ptrdiff_t offset = x - base;
|
2012-01-04 01:42:22 -07:00
|
|
|
Do(offset);
|
|
|
|
if (mode == MODE_READ)
|
2013-11-13 02:03:46 -07:00
|
|
|
{
|
2012-01-04 01:42:22 -07:00
|
|
|
x = base + offset;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
2013-11-13 02:03:46 -07:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-03-12 13:33:41 -06:00
|
|
|
void DoMarker(const std::string& prevName, u32 arbitraryNumber = 0x42)
|
2011-12-17 17:49:24 -07:00
|
|
|
{
|
|
|
|
u32 cookie = arbitraryNumber;
|
|
|
|
Do(cookie);
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
if (mode == PointerWrap::MODE_READ && cookie != arbitraryNumber)
|
2011-12-17 17:49:24 -07:00
|
|
|
{
|
2013-03-02 02:28:44 -07:00
|
|
|
PanicAlertT("Error: After \"%s\", found %d (0x%X) instead of save marker %d (0x%X). Aborting "
|
|
|
|
"savestate load...",
|
2014-03-12 13:33:41 -06:00
|
|
|
prevName.c_str(), cookie, cookie, arbitraryNumber, arbitraryNumber);
|
2011-12-17 17:49:24 -07:00
|
|
|
mode = PointerWrap::MODE_MEASURE;
|
|
|
|
}
|
|
|
|
}
|
2013-03-19 19:51:12 -06:00
|
|
|
|
2016-09-01 00:42:52 -06:00
|
|
|
template <typename T, typename Functor>
|
|
|
|
void DoEachElement(T& container, Functor member)
|
|
|
|
{
|
|
|
|
u32 size = static_cast<u32>(container.size());
|
|
|
|
Do(size);
|
|
|
|
container.resize(size);
|
|
|
|
|
|
|
|
for (auto& elem : container)
|
|
|
|
member(*this, elem);
|
|
|
|
}
|
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
private:
|
2019-01-31 19:02:22 -07:00
|
|
|
template <typename T>
|
|
|
|
void DoContiguousContainer(T& container)
|
|
|
|
{
|
|
|
|
u32 size = static_cast<u32>(container.size());
|
|
|
|
Do(size);
|
|
|
|
container.resize(size);
|
|
|
|
|
2019-02-09 20:26:46 -07:00
|
|
|
if (size > 0)
|
|
|
|
DoArray(&container[0], size);
|
2019-01-31 19:02:22 -07:00
|
|
|
}
|
|
|
|
|
2014-07-05 21:03:43 -06:00
|
|
|
template <typename T>
|
|
|
|
void DoContainer(T& x)
|
|
|
|
{
|
2016-09-01 00:42:52 -06:00
|
|
|
DoEachElement(x, [](PointerWrap& p, typename T::value_type& elem) { p.Do(elem); });
|
2014-07-05 21:03:43 -06:00
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2018-06-09 09:47:01 -06:00
|
|
|
DOLPHIN_FORCE_INLINE void DoVoid(void* data, u32 size)
|
2013-03-02 02:28:44 -07:00
|
|
|
{
|
|
|
|
switch (mode)
|
|
|
|
{
|
|
|
|
case MODE_READ:
|
2014-08-27 17:50:52 -06:00
|
|
|
memcpy(data, *ptr, size);
|
2013-03-02 02:28:44 -07:00
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
case MODE_WRITE:
|
2014-08-27 17:50:52 -06:00
|
|
|
memcpy(*ptr, data, size);
|
2013-03-02 02:28:44 -07:00
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
case MODE_MEASURE:
|
|
|
|
break;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2013-03-02 02:28:44 -07:00
|
|
|
case MODE_VERIFY:
|
2018-03-14 18:34:35 -06:00
|
|
|
DEBUG_ASSERT_MSG(COMMON, !memcmp(data, *ptr, size),
|
2014-08-27 17:50:52 -06:00
|
|
|
"Savestate verification failure: buf %p != %p (size %u).\n", data, *ptr,
|
|
|
|
size);
|
2013-03-02 02:28:44 -07:00
|
|
|
break;
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2014-08-27 17:50:52 -06:00
|
|
|
*ptr += size;
|
2013-03-02 02:28:44 -07:00
|
|
|
}
|
2008-12-07 21:46:09 -07:00
|
|
|
};
|