LCOV - code coverage report
Current view: top level - src/support/allocators - zeroafterfree.h (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 100.0 % 7 7
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 1 1
Branches: 50.0 % 6 3

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-2022 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 {
      16                 :             :     using value_type = T;
      17                 :             : 
      18                 :             :     zero_after_free_allocator() noexcept = default;
      19                 :             :     template <typename U>
      20                 :             :     zero_after_free_allocator(const zero_after_free_allocator<U>&) noexcept
      21                 :             :     {
      22                 :             :     }
      23                 :             : 
      24         [ -  + ]:     6917662 :     T* allocate(std::size_t n)
      25                 :             :     {
      26         [ -  + ]:     6917662 :         return std::allocator<T>{}.allocate(n);
      27                 :             :     }
      28                 :             : 
      29                 :     6917662 :     void deallocate(T* p, std::size_t n)
      30                 :             :     {
      31         [ +  - ]:     6917662 :         if (p != nullptr)
      32                 :     6917662 :             memory_cleanse(p, sizeof(T) * n);
      33                 :     6917662 :         std::allocator<T>{}.deallocate(p, n);
      34                 :     6917662 :     }
      35                 :             : 
      36                 :             :     template <typename U>
      37                 :             :     friend bool operator==(const zero_after_free_allocator&, const zero_after_free_allocator<U>&) noexcept
      38                 :             :     {
      39                 :             :         return true;
      40                 :             :     }
      41                 :             :     template <typename U>
      42                 :             :     friend bool operator!=(const zero_after_free_allocator&, const zero_after_free_allocator<U>&) noexcept
      43                 :             :     {
      44                 :             :         return false;
      45                 :             :     }
      46                 :             : };
      47                 :             : 
      48                 :             : /** Byte-vector that clears its contents before deletion. */
      49                 :             : using SerializeData = std::vector<std::byte, zero_after_free_allocator<std::byte>>;
      50                 :             : 
      51                 :             : #endif // BITCOIN_SUPPORT_ALLOCATORS_ZEROAFTERFREE_H
        

Generated by: LCOV version 2.0-1