From: Matt Corallo Date: Tue, 3 Jan 2012 08:03:07 +0000 (-0800) Subject: Fix horrific performance found by gmaxwell. X-Git-Tag: v0.4.0-unstable~129^2~283^2 X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=commitdiff_plain;h=f7a9a11391b18e8d18fce4482a4fcb6a7d6c9796 Fix horrific performance found by gmaxwell. --- diff --git a/src/serialize.h b/src/serialize.h index 5455590..626f7d6 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -819,6 +819,38 @@ struct secure_allocator : public std::allocator }; +// +// Allocator that clears its contents before deletion. +// +template +struct zero_after_free_allocator : public std::allocator +{ + // MSVC8 default copy constructor is broken + typedef std::allocator base; + typedef typename base::size_type size_type; + typedef typename base::difference_type difference_type; + typedef typename base::pointer pointer; + typedef typename base::const_pointer const_pointer; + typedef typename base::reference reference; + typedef typename base::const_reference const_reference; + typedef typename base::value_type value_type; + zero_after_free_allocator() throw() {} + zero_after_free_allocator(const zero_after_free_allocator& a) throw() : base(a) {} + template + zero_after_free_allocator(const zero_after_free_allocator& a) throw() : base(a) {} + ~zero_after_free_allocator() throw() {} + template struct rebind + { typedef zero_after_free_allocator<_Other> other; }; + + void deallocate(T* p, std::size_t n) + { + if (p != NULL) + memset(p, 0, sizeof(T) * n); + std::allocator::deallocate(p, n); + } +}; + + // // Double ended buffer combining vector and stream-like interfaces. @@ -828,7 +860,7 @@ struct secure_allocator : public std::allocator class CDataStream { protected: - typedef std::vector > vector_type; + typedef std::vector > vector_type; vector_type vch; unsigned int nReadPos; short state;