Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2009-2010 Satoshi Nakamoto2// Copyright (c) 2009-present The Bitcoin Core developers3// Distributed under the MIT software license, see the accompanying4// file COPYING or http://www.opensource.org/licenses/mit-license.php.6#ifndef BITCOIN_SUPPORT_ALLOCATORS_SECURE_H7#define BITCOIN_SUPPORT_ALLOCATORS_SECURE_H9#include <support/lockedpool.h>10#include <support/cleanse.h>12#include <memory>13#include <string>15//16// Allocator that locks its contents from being paged17// out of memory and clears its contents before deletion.18//19template <typename T>20struct secure_allocator {21using value_type = T;23secure_allocator() = default;24template <typename U>25secure_allocator(const secure_allocator<U>&) noexcept {}27T* allocate(std::size_t n)28{29T* allocation = static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n));30if (!allocation) {31throw std::bad_alloc();32}33return allocation;34}36void deallocate(T* p, std::size_t n)37{38if (p != nullptr) {39memory_cleanse(p, sizeof(T) * n);40}41LockedPoolManager::Instance().free(p);42}44template <typename U>45friend bool operator==(const secure_allocator&, const secure_allocator<U>&) noexcept46{47return true;48}49};51// This is exactly like std::string, but with a custom allocator.52// TODO: Consider finding a way to make incoming RPC request.params[i] mlock()ed as well53typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;55template<typename T>56struct SecureUniqueDeleter {57void operator()(T* t) noexcept {58secure_allocator<T>().deallocate(t, 1);59}60};62template<typename T>63using secure_unique_ptr = std::unique_ptr<T, SecureUniqueDeleter<T>>;65template<typename T, typename... Args>66secure_unique_ptr<T> make_secure_unique(Args&&... as)67{68T* p = secure_allocator<T>().allocate(1);70// initialize in place, and return as secure_unique_ptr71try {72return secure_unique_ptr<T>(new (p) T(std::forward<Args>(as)...));73} catch (...) {74secure_allocator<T>().deallocate(p, 1);75throw;76}77}79#endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H