X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fserialize.h;h=320ce9d2ae01be2c2144f34140327f5548d9e271;hb=aec5c5fe26293452d3fe7acf1e4c20830613812c;hp=ee39c0703dba114c97442d32ccfdeb524c146109;hpb=b17be7e14b4a62b7935c75fe7e7645e736fd68d2;p=novacoin.git diff --git a/src/serialize.h b/src/serialize.h index ee39c07..320ce9d 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -1,15 +1,24 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2011 The Bitcoin developers // Distributed under the MIT/X11 software license, see the accompanying // file license.txt or http://www.opensource.org/licenses/mit-license.php. +#ifndef BITCOIN_SERIALIZE_H +#define BITCOIN_SERIALIZE_H #include #include #include #include +#include +#include +#include +#include + #include #include #include #include + #if defined(_MSC_VER) || defined(__BORLANDC__) typedef __int64 int64; typedef unsigned __int64 uint64; @@ -20,19 +29,47 @@ typedef unsigned long long uint64; #if defined(_MSC_VER) && _MSC_VER < 1300 #define for if (false) ; else for #endif + +#ifdef __WXMSW__ +// This is used to attempt to keep keying material out of swap +// Note that VirtualLock does not provide this as a guarantee on Windows, +// but, in practice, memory that has been VirtualLock'd almost never gets written to +// the pagefile except in rare circumstances where memory is extremely low. +#include +#define mlock(p, n) VirtualLock((p), (n)); +#define munlock(p, n) VirtualUnlock((p), (n)); +#else +#include +#include +/* This comes from limits.h if it's not defined there set a sane default */ +#ifndef PAGESIZE +#include +#define PAGESIZE sysconf(_SC_PAGESIZE) +#endif +#define mlock(a,b) \ + mlock(((void *)(((size_t)(a)) & (~((PAGESIZE)-1)))),\ + (((((size_t)(a)) + (b) - 1) | ((PAGESIZE) - 1)) + 1) - (((size_t)(a)) & (~((PAGESIZE) - 1)))) +#define munlock(a,b) \ + munlock(((void *)(((size_t)(a)) & (~((PAGESIZE)-1)))),\ + (((((size_t)(a)) + (b) - 1) | ((PAGESIZE) - 1)) + 1) - (((size_t)(a)) & (~((PAGESIZE) - 1)))) +#endif + class CScript; class CDataStream; class CAutoFile; static const unsigned int MAX_SIZE = 0x02000000; -static const int VERSION = 32200; +static const int VERSION = 40100; static const char* pszSubVer = ""; static const bool VERSION_IS_BETA = true; - - - - +// Used to bypass the rule against non-const reference to temporary +// where it makes sense with wrappers such as CFlatData or CTxDB +template +inline T& REF(const T& val) +{ + return const_cast(val); +} ///////////////////////////////////////////////////////////////// // @@ -277,11 +314,11 @@ template class CFixedFieldString { protected: - const string* pcstr; - string* pstr; + const std::string* pcstr; + std::string* pstr; public: - explicit CFixedFieldString(const string& str) : pcstr(&str), pstr(NULL) { } - explicit CFixedFieldString(string& str) : pcstr(&str), pstr(&str) { } + explicit CFixedFieldString(const std::string& str) : pcstr(&str), pstr(NULL) { } + explicit CFixedFieldString(std::string& str) : pcstr(&str), pstr(&str) { } unsigned int GetSerializeSize(int, int=0) const { @@ -317,9 +354,9 @@ public: // // string -template unsigned int GetSerializeSize(const basic_string& str, int, int=0); -template void Serialize(Stream& os, const basic_string& str, int, int=0); -template void Unserialize(Stream& is, basic_string& str, int, int=0); +template unsigned int GetSerializeSize(const std::basic_string& str, int, int=0); +template void Serialize(Stream& os, const std::basic_string& str, int, int=0); +template void Unserialize(Stream& is, std::basic_string& str, int, int=0); // vector template unsigned int GetSerializeSize_impl(const std::vector& v, int nType, int nVersion, const boost::true_type&); @@ -398,13 +435,13 @@ inline void Unserialize(Stream& is, T& a, long nType, int nVersion=VERSION) // string // template -unsigned int GetSerializeSize(const basic_string& str, int, int) +unsigned int GetSerializeSize(const std::basic_string& str, int, int) { return GetSizeOfCompactSize(str.size()) + str.size() * sizeof(str[0]); } template -void Serialize(Stream& os, const basic_string& str, int, int) +void Serialize(Stream& os, const std::basic_string& str, int, int) { WriteCompactSize(os, str.size()); if (!str.empty()) @@ -412,7 +449,7 @@ void Serialize(Stream& os, const basic_string& str, int, int) } template -void Unserialize(Stream& is, basic_string& str, int, int) +void Unserialize(Stream& is, std::basic_string& str, int, int) { unsigned int nSize = ReadCompactSize(is); str.resize(nSize); @@ -483,7 +520,7 @@ void Unserialize_impl(Stream& is, std::vector& v, int nType, int nVersion, unsigned int i = 0; while (i < nSize) { - unsigned int blk = min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T))); + unsigned int blk = std::min(nSize - i, (unsigned int)(1 + 4999999 / sizeof(T))); v.resize(i + blk); is.read((char*)&v[i], blk * sizeof(T)); i += blk; @@ -526,19 +563,19 @@ inline void Unserialize(Stream& is, std::vector& v, int nType, int nVersio // inline unsigned int GetSerializeSize(const CScript& v, int nType, int nVersion) { - return GetSerializeSize((const vector&)v, nType, nVersion); + return GetSerializeSize((const std::vector&)v, nType, nVersion); } template void Serialize(Stream& os, const CScript& v, int nType, int nVersion) { - Serialize(os, (const vector&)v, nType, nVersion); + Serialize(os, (const std::vector&)v, nType, nVersion); } template void Unserialize(Stream& is, CScript& v, int nType, int nVersion) { - Unserialize(is, (vector&)v, nType, nVersion); + Unserialize(is, (std::vector&)v, nType, nVersion); } @@ -575,26 +612,26 @@ template unsigned int GetSerializeSize(const boost::tuple& item, int nType, int nVersion) { unsigned int nSize = 0; - nSize += GetSerializeSize(get<0>(item), nType, nVersion); - nSize += GetSerializeSize(get<1>(item), nType, nVersion); - nSize += GetSerializeSize(get<2>(item), nType, nVersion); + nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion); + nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion); + nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion); return nSize; } template void Serialize(Stream& os, const boost::tuple& item, int nType, int nVersion) { - Serialize(os, get<0>(item), nType, nVersion); - Serialize(os, get<1>(item), nType, nVersion); - Serialize(os, get<2>(item), nType, nVersion); + Serialize(os, boost::get<0>(item), nType, nVersion); + Serialize(os, boost::get<1>(item), nType, nVersion); + Serialize(os, boost::get<2>(item), nType, nVersion); } template void Unserialize(Stream& is, boost::tuple& item, int nType, int nVersion) { - Unserialize(is, get<0>(item), nType, nVersion); - Unserialize(is, get<1>(item), nType, nVersion); - Unserialize(is, get<2>(item), nType, nVersion); + Unserialize(is, boost::get<0>(item), nType, nVersion); + Unserialize(is, boost::get<1>(item), nType, nVersion); + Unserialize(is, boost::get<2>(item), nType, nVersion); } @@ -606,29 +643,29 @@ template unsigned int GetSerializeSize(const boost::tuple& item, int nType, int nVersion) { unsigned int nSize = 0; - nSize += GetSerializeSize(get<0>(item), nType, nVersion); - nSize += GetSerializeSize(get<1>(item), nType, nVersion); - nSize += GetSerializeSize(get<2>(item), nType, nVersion); - nSize += GetSerializeSize(get<3>(item), nType, nVersion); + nSize += GetSerializeSize(boost::get<0>(item), nType, nVersion); + nSize += GetSerializeSize(boost::get<1>(item), nType, nVersion); + nSize += GetSerializeSize(boost::get<2>(item), nType, nVersion); + nSize += GetSerializeSize(boost::get<3>(item), nType, nVersion); return nSize; } template void Serialize(Stream& os, const boost::tuple& item, int nType, int nVersion) { - Serialize(os, get<0>(item), nType, nVersion); - Serialize(os, get<1>(item), nType, nVersion); - Serialize(os, get<2>(item), nType, nVersion); - Serialize(os, get<3>(item), nType, nVersion); + Serialize(os, boost::get<0>(item), nType, nVersion); + Serialize(os, boost::get<1>(item), nType, nVersion); + Serialize(os, boost::get<2>(item), nType, nVersion); + Serialize(os, boost::get<3>(item), nType, nVersion); } template void Unserialize(Stream& is, boost::tuple& item, int nType, int nVersion) { - Unserialize(is, get<0>(item), nType, nVersion); - Unserialize(is, get<1>(item), nType, nVersion); - Unserialize(is, get<2>(item), nType, nVersion); - Unserialize(is, get<3>(item), nType, nVersion); + Unserialize(is, boost::get<0>(item), nType, nVersion); + Unserialize(is, boost::get<1>(item), nType, nVersion); + Unserialize(is, boost::get<2>(item), nType, nVersion); + Unserialize(is, boost::get<3>(item), nType, nVersion); } @@ -661,7 +698,7 @@ void Unserialize(Stream& is, std::map& m, int nType, int nVersion typename std::map::iterator mi = m.begin(); for (unsigned int i = 0; i < nSize; i++) { - pair item; + std::pair item; Unserialize(is, item, nType, nVersion); mi = m.insert(mi, item); } @@ -747,7 +784,8 @@ struct ser_streamplaceholder // -// Allocator that clears its contents before deletion +// Allocator that locks its contents from being paged +// out of memory and clears its contents before deletion. // template struct secure_allocator : public std::allocator @@ -769,11 +807,23 @@ struct secure_allocator : public std::allocator template struct rebind { typedef secure_allocator<_Other> other; }; + T* allocate(std::size_t n, const void *hint = 0) + { + T *p; + p = std::allocator::allocate(n, hint); + if (p != NULL) + mlock(p, sizeof(T) * n); + return p; + } + void deallocate(T* p, std::size_t n) { if (p != NULL) + { memset(p, 0, sizeof(T) * n); - allocator::deallocate(p, n); + munlock(p, sizeof(T) * n); + } + std::allocator::deallocate(p, n); } }; @@ -787,7 +837,7 @@ struct secure_allocator : public std::allocator class CDataStream { protected: - typedef vector > vector_type; + typedef std::vector > vector_type; vector_type vch; unsigned int nReadPos; short state; @@ -828,12 +878,12 @@ public: Init(nTypeIn, nVersionIn); } - CDataStream(const vector& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end()) + CDataStream(const std::vector& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch(vchIn.begin(), vchIn.end()) { Init(nTypeIn, nVersionIn); } - CDataStream(const vector& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch((char*)&vchIn.begin()[0], (char*)&vchIn.end()[0]) + CDataStream(const std::vector& vchIn, int nTypeIn=SER_NETWORK, int nVersionIn=VERSION) : vch((char*)&vchIn.begin()[0], (char*)&vchIn.end()[0]) { Init(nTypeIn, nVersionIn); } @@ -844,7 +894,7 @@ public: nType = nTypeIn; nVersion = nVersionIn; state = 0; - exceptmask = ios::badbit | ios::failbit; + exceptmask = std::ios::badbit | std::ios::failbit; } CDataStream& operator+=(const CDataStream& b) @@ -860,9 +910,9 @@ public: return (ret); } - string str() const + std::string str() const { - return (string(begin(), end())); + return (std::string(begin(), end())); } @@ -895,7 +945,7 @@ public: vch.insert(it, first, last); } - void insert(iterator it, vector::const_iterator first, vector::const_iterator last) + void insert(iterator it, std::vector::const_iterator first, std::vector::const_iterator last) { if (it == vch.begin() + nReadPos && last - first <= nReadPos) { @@ -985,7 +1035,7 @@ public: } bool eof() const { return size() == 0; } - bool fail() const { return state & (ios::badbit | ios::failbit); } + bool fail() const { return state & (std::ios::badbit | std::ios::failbit); } bool good() const { return !eof() && (state == 0); } void clear(short n) { state = n; } // name conflict with vector clear() short exceptions() { return exceptmask; } @@ -1009,7 +1059,7 @@ public: { if (nReadPosNext > vch.size()) { - setstate(ios::failbit, "CDataStream::read() : end of data"); + setstate(std::ios::failbit, "CDataStream::read() : end of data"); memset(pch, 0, nSize); nSize = vch.size() - nReadPos; } @@ -1032,7 +1082,7 @@ public: { if (nReadPosNext > vch.size()) { - setstate(ios::failbit, "CDataStream::ignore() : end of data"); + setstate(std::ios::failbit, "CDataStream::ignore() : end of data"); nSize = vch.size() - nReadPos; } nReadPos = 0; @@ -1167,7 +1217,7 @@ public: nType = nTypeIn; nVersion = nVersionIn; state = 0; - exceptmask = ios::badbit | ios::failbit; + exceptmask = std::ios::badbit | std::ios::failbit; } ~CAutoFile() @@ -1201,7 +1251,7 @@ public: throw std::ios_base::failure(psz); } - bool fail() const { return state & (ios::badbit | ios::failbit); } + bool fail() const { return state & (std::ios::badbit | std::ios::failbit); } bool good() const { return state == 0; } void clear(short n = 0) { state = n; } short exceptions() { return exceptmask; } @@ -1219,7 +1269,7 @@ public: if (!file) throw std::ios_base::failure("CAutoFile::read : file handle is NULL"); if (fread(pch, 1, nSize, file) != nSize) - setstate(ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed"); + setstate(std::ios::failbit, feof(file) ? "CAutoFile::read : end of file" : "CAutoFile::read : fread failed"); return (*this); } @@ -1228,7 +1278,7 @@ public: if (!file) throw std::ios_base::failure("CAutoFile::write : file handle is NULL"); if (fwrite(pch, 1, nSize, file) != nSize) - setstate(ios::failbit, "CAutoFile::write : write failed"); + setstate(std::ios::failbit, "CAutoFile::write : write failed"); return (*this); } @@ -1259,3 +1309,5 @@ public: return (*this); } }; + +#endif