Changeset View
Changeset View
Standalone View
Standalone View
src/serialize.h
Show All 20 Lines | |||||
#include <set> | #include <set> | ||||
#include <string> | #include <string> | ||||
#include <utility> | #include <utility> | ||||
#include <vector> | #include <vector> | ||||
static const uint64_t MAX_SIZE = 0x02000000; | static const uint64_t MAX_SIZE = 0x02000000; | ||||
/** | /** | ||||
* Maximum amount of memory (in bytes) to allocate at once when deserializing | |||||
* vectors. | |||||
*/ | |||||
static const unsigned int MAX_VECTOR_ALLOCATE = 5000000; | |||||
/** | |||||
* Dummy data type to identify deserializing constructors. | * Dummy data type to identify deserializing constructors. | ||||
* | * | ||||
* By convention, a constructor of a type T with signature | * By convention, a constructor of a type T with signature | ||||
* | * | ||||
* template <typename Stream> T::T(deserialize_type, Stream& s) | * template <typename Stream> T::T(deserialize_type, Stream& s) | ||||
* | * | ||||
* is a deserializing constructor, which builds the type by deserializing it | * is a deserializing constructor, which builds the type by deserializing it | ||||
* from s. If T contains const fields, this is likely the only way to do so. | * from s. If T contains const fields, this is likely the only way to do so. | ||||
▲ Show 20 Lines • Show All 824 Lines • ▼ Show 20 Lines | |||||
template <typename Stream, unsigned int N, typename T, typename V> | template <typename Stream, unsigned int N, typename T, typename V> | ||||
void Unserialize_impl(Stream &is, prevector<N, T> &v, const V &) { | void Unserialize_impl(Stream &is, prevector<N, T> &v, const V &) { | ||||
v.clear(); | v.clear(); | ||||
size_t nSize = ReadCompactSize(is); | size_t nSize = ReadCompactSize(is); | ||||
size_t i = 0; | size_t i = 0; | ||||
size_t nMid = 0; | size_t nMid = 0; | ||||
while (nMid < nSize) { | while (nMid < nSize) { | ||||
nMid += 5000000 / sizeof(T); | nMid += MAX_VECTOR_ALLOCATE / sizeof(T); | ||||
if (nMid > nSize) { | if (nMid > nSize) { | ||||
nMid = nSize; | nMid = nSize; | ||||
} | } | ||||
v.resize_uninitialized(nMid); | v.resize_uninitialized(nMid); | ||||
for (; i < nMid; ++i) { | for (; i < nMid; ++i) { | ||||
Unserialize(is, v[i]); | Unserialize(is, v[i]); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | |||||
template <typename Stream, typename T, typename A, typename V> | template <typename Stream, typename T, typename A, typename V> | ||||
void Unserialize_impl(Stream &is, std::vector<T, A> &v, const V &) { | void Unserialize_impl(Stream &is, std::vector<T, A> &v, const V &) { | ||||
v.clear(); | v.clear(); | ||||
size_t nSize = ReadCompactSize(is); | size_t nSize = ReadCompactSize(is); | ||||
size_t i = 0; | size_t i = 0; | ||||
size_t nMid = 0; | size_t nMid = 0; | ||||
while (nMid < nSize) { | while (nMid < nSize) { | ||||
nMid += 5000000 / sizeof(T); | nMid += MAX_VECTOR_ALLOCATE / sizeof(T); | ||||
if (nMid > nSize) { | if (nMid > nSize) { | ||||
nMid = nSize; | nMid = nSize; | ||||
} | } | ||||
v.resize(nMid); | v.resize(nMid); | ||||
for (; i < nMid; i++) { | for (; i < nMid; i++) { | ||||
Unserialize(is, v[i]); | Unserialize(is, v[i]); | ||||
} | } | ||||
} | } | ||||
▲ Show 20 Lines • Show All 188 Lines • Show Last 20 Lines |