Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2013 The Bitcoin developers 3 : // Distributed under the MIT/X11 software license, see the accompanying 4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 5 : 6 : 7 : #ifndef PIVX_SUPPORT_ALLOCATORS_SECURE_H 8 : #define PIVX_SUPPORT_ALLOCATORS_SECURE_H 9 : 10 : #include "support/lockedpool.h" 11 : #include "support/cleanse.h" 12 : 13 : #include <string> 14 : 15 : // 16 : // Allocator that locks its contents from being paged 17 : // out of memory and clears its contents before deletion. 18 : // 19 : template <typename T> 20 : struct secure_allocator : public std::allocator<T> { 21 : // MSVC8 default copy constructor is broken 22 : typedef std::allocator<T> base; 23 : typedef typename base::size_type size_type; 24 : typedef typename base::difference_type difference_type; 25 : typedef typename base::pointer pointer; 26 : typedef typename base::const_pointer const_pointer; 27 : typedef typename base::reference reference; 28 : typedef typename base::const_reference const_reference; 29 : typedef typename base::value_type value_type; 30 4037777 : secure_allocator() noexcept {} 31 132058 : secure_allocator(const secure_allocator& a) noexcept : base(a) {} 32 : template <typename U> 33 : secure_allocator(const secure_allocator<U>& a) noexcept : base(a) 34 : { 35 : } 36 4274508 : ~secure_allocator() noexcept {} 37 : template <typename _Other> 38 : struct rebind { 39 : typedef secure_allocator<_Other> other; 40 : }; 41 : 42 4059639 : T* allocate(std::size_t n, const void* hint = 0) 43 : { 44 4059639 : T* allocation = static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n)); 45 4059639 : if (!allocation) { 46 0 : throw std::bad_alloc(); 47 : } 48 4059639 : return allocation; 49 : } 50 : 51 4059636 : void deallocate(T* p, std::size_t n) 52 : { 53 4059636 : if (p != nullptr) { 54 4059636 : memory_cleanse(p, sizeof(T) * n); 55 : } 56 4059636 : LockedPoolManager::Instance().free(p); 57 4059636 : } 58 : }; 59 : 60 : // This is exactly like std::string, but with a custom allocator. 61 : typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString; 62 : 63 : #endif // PIVX_SUPPORT_ALLOCATORS_SECURE_H