Changeset View
Changeset View
Standalone View
Standalone View
src/support/allocators/secure.h
Show All 29 Lines | template <typename T> struct secure_allocator : public std::allocator<T> { | ||||
template <typename U> | template <typename U> | ||||
secure_allocator(const secure_allocator<U> &a) noexcept : base(a) {} | secure_allocator(const secure_allocator<U> &a) noexcept : base(a) {} | ||||
~secure_allocator() noexcept {} | ~secure_allocator() noexcept {} | ||||
template <typename _Other> struct rebind { | template <typename _Other> struct rebind { | ||||
typedef secure_allocator<_Other> other; | typedef secure_allocator<_Other> other; | ||||
}; | }; | ||||
T *allocate(std::size_t n, const void *hint = 0) { | T *allocate(std::size_t n, const void *hint = 0) { | ||||
return static_cast<T *>( | T *allocation = static_cast<T *>( | ||||
LockedPoolManager::Instance().alloc(sizeof(T) * n)); | LockedPoolManager::Instance().alloc(sizeof(T) * n)); | ||||
if (!allocation) { | |||||
throw std::bad_alloc(); | |||||
} | |||||
return allocation; | |||||
} | } | ||||
void deallocate(T *p, std::size_t n) { | void deallocate(T *p, std::size_t n) { | ||||
if (p != nullptr) { | if (p != nullptr) { | ||||
memory_cleanse(p, sizeof(T) * n); | memory_cleanse(p, sizeof(T) * n); | ||||
} | } | ||||
LockedPoolManager::Instance().free(p); | LockedPoolManager::Instance().free(p); | ||||
} | } | ||||
}; | }; | ||||
// This is exactly like std::string, but with a custom allocator. | // This is exactly like std::string, but with a custom allocator. | ||||
typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char>> | typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char>> | ||||
SecureString; | SecureString; | ||||
#endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H | #endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H |