Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto # 2 : : // Copyright (c) 2009-2021 The Bitcoin Core developers # 3 : : // Distributed under the MIT software license, see the accompanying # 4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php. # 5 : : # 6 : : #ifndef BITCOIN_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H # 7 : : #define BITCOIN_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H # 8 : : # 9 : : #include <support/cleanse.h> # 10 : : # 11 : : #include <memory> # 12 : : #include <vector> # 13 : : # 14 : : template <typename T> # 15 : : struct zero_after_free_allocator : public std::allocator<T> { # 16 : : using base = std::allocator<T>; # 17 : : using traits = std::allocator_traits<base>; # 18 : : using size_type = typename traits::size_type; # 19 : : using difference_type = typename traits::difference_type; # 20 : : using pointer = typename traits::pointer; # 21 : : using const_pointer = typename traits::const_pointer; # 22 : : using value_type = typename traits::value_type; # 23 : 12532779 : zero_after_free_allocator() noexcept {} # 24 : 225656 : zero_after_free_allocator(const zero_after_free_allocator& a) noexcept : base(a) {} # 25 : : template <typename U> # 26 : : zero_after_free_allocator(const zero_after_free_allocator<U>& a) noexcept : base(a) # 27 : : { # 28 : : } # 29 : 12758439 : ~zero_after_free_allocator() noexcept {} # 30 : : template <typename _Other> # 31 : : struct rebind { # 32 : : typedef zero_after_free_allocator<_Other> other; # 33 : : }; # 34 : : # 35 : : void deallocate(T* p, std::size_t n) # 36 : 16694659 : { # 37 [ + - ]: 16694659 : if (p != nullptr) # 38 : 16694659 : memory_cleanse(p, sizeof(T) * n); # 39 : 16694659 : std::allocator<T>::deallocate(p, n); # 40 : 16694659 : } # 41 : : }; # 42 : : # 43 : : /** Byte-vector that clears its contents before deletion. */ # 44 : : using SerializeData = std::vector<std::byte, zero_after_free_allocator<std::byte>>; # 45 : : # 46 : : #endif // BITCOIN_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H