diff --git a/src/support/allocators/secure.h b/src/support/allocators/secure.h index 558f835f116..4395567722e 100644 --- a/src/support/allocators/secure.h +++ b/src/support/allocators/secure.h @@ -57,4 +57,28 @@ struct secure_allocator { // TODO: Consider finding a way to make incoming RPC request.params[i] mlock()ed as well typedef std::basic_string, secure_allocator > SecureString; +template +struct SecureUniqueDeleter { + void operator()(T* t) noexcept { + secure_allocator().deallocate(t, 1); + } +}; + +template +using secure_unique_ptr = std::unique_ptr>; + +template +secure_unique_ptr make_secure_unique(Args&&... as) +{ + T* p = secure_allocator().allocate(1); + + // initialize in place, and return as secure_unique_ptr + try { + return secure_unique_ptr(new (p) T(std::forward(as)...)); + } catch (...) { + secure_allocator().deallocate(p, 1); + throw; + } +} + #endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H