LCOV - code coverage report
Current view: top level - src/support/allocators - secure.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 14 14 100.0 %
Date: 2015-10-12 22:39:14 Functions: 9 17 52.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2             : // Copyright (c) 2009-2013 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/pagelocker.h"
      10             : 
      11             : #include <string>
      12             : 
      13             : //
      14             : // Allocator that locks its contents from being paged
      15             : // out of memory and clears its contents before deletion.
      16             : //
      17             : template <typename T>
      18             : 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           2 :     secure_allocator() throw() {}
      29          10 :     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             :     {
      33             :     }
      34        7484 :     ~secure_allocator() throw() {}
      35             :     template <typename _Other>
      36             :     struct rebind {
      37             :         typedef secure_allocator<_Other> other;
      38             :     };
      39             : 
      40        7105 :     T* allocate(std::size_t n, const void* hint = 0)
      41             :     {
      42             :         T* p;
      43       14210 :         p = std::allocator<T>::allocate(n, hint);
      44        7105 :         if (p != NULL)
      45        7105 :             LockedPageManager::Instance().LockRange(p, sizeof(T) * n);
      46        7105 :         return p;
      47             :     }
      48             : 
      49        7105 :     void deallocate(T* p, std::size_t n)
      50             :     {
      51        7105 :         if (p != NULL) {
      52        7105 :             memory_cleanse(p, sizeof(T) * n);
      53        7105 :             LockedPageManager::Instance().UnlockRange(p, sizeof(T) * n);
      54             :         }
      55        7105 :         std::allocator<T>::deallocate(p, n);
      56        7105 :     }
      57             : };
      58             : 
      59             : // This is exactly like std::string, but with a custom allocator.
      60             : typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
      61             : 
      62             : #endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H

Generated by: LCOV version 1.11