LCOV - code coverage report
Current view: top level - src/support/allocators - secure.h (source / functions) Hit Total Coverage
Test: bitcoincash_test.info Lines: 10 10 100.0 %
Date: 2018-04-13 15:12:50 Functions: 13 21 61.9 %

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2016 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_SECURE_H
       7             : #define BITCOIN_SUPPORT_ALLOCATORS_SECURE_H
       8             : 
       9             : #include "support/cleanse.h"
      10             : #include "support/lockedpool.h"
      11             : 
      12             : #include <string>
      13             : 
      14             : //
      15             : // Allocator that locks its contents from being paged
      16             : // out of memory and clears its contents before deletion.
      17             : //
      18             : template <typename T> struct secure_allocator : public std::allocator<T> {
      19             :     // MSVC8 default copy constructor is broken
      20             :     typedef std::allocator<T> base;
      21             :     typedef typename base::size_type size_type;
      22             :     typedef typename base::difference_type difference_type;
      23             :     typedef typename base::pointer pointer;
      24             :     typedef typename base::const_pointer const_pointer;
      25             :     typedef typename base::reference reference;
      26             :     typedef typename base::const_reference const_reference;
      27             :     typedef typename base::value_type value_type;
      28         128 :     secure_allocator() throw() {}
      29         236 :     secure_allocator(const secure_allocator &a) throw() : base(a) {}
      30             :     template <typename U>
      31             :     secure_allocator(const secure_allocator<U> &a) throw() : base(a) {}
      32         437 :     ~secure_allocator() throw() {}
      33             :     template <typename _Other> struct rebind {
      34             :         typedef secure_allocator<_Other> other;
      35             :     };
      36             : 
      37         502 :     T *allocate(std::size_t n, const void *hint = 0) {
      38             :         return static_cast<T *>(
      39         502 :             LockedPoolManager::Instance().alloc(sizeof(T) * n));
      40             :     }
      41             : 
      42         319 :     void deallocate(T *p, std::size_t n) {
      43         319 :         if (p != nullptr) {
      44         319 :             memory_cleanse(p, sizeof(T) * n);
      45             :         }
      46         319 :         LockedPoolManager::Instance().free(p);
      47         319 :     }
      48             : };
      49             : 
      50             : // This is exactly like std::string, but with a custom allocator.
      51             : typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char>>
      52             :     SecureString;
      53             : 
      54             : #endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H

Generated by: LCOV version 1.12