Dash Core  0.12.2.1
P2P Digital Currency
secure.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 #include <vector>
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>
19 struct secure_allocator : public std::allocator<T> {
20  // MSVC8 default copy constructor is broken
21  typedef std::allocator<T> base;
22  typedef typename base::size_type size_type;
23  typedef typename base::difference_type difference_type;
24  typedef typename base::pointer pointer;
25  typedef typename base::const_pointer const_pointer;
26  typedef typename base::reference reference;
27  typedef typename base::const_reference const_reference;
28  typedef typename base::value_type value_type;
29  secure_allocator() throw() {}
30  secure_allocator(const secure_allocator& a) throw() : base(a) {}
31  template <typename U>
32  secure_allocator(const secure_allocator<U>& a) throw() : base(a)
33  {
34  }
35  ~secure_allocator() throw() {}
36  template <typename _Other>
37  struct rebind {
39  };
40 
41  T* allocate(std::size_t n, const void* hint = 0)
42  {
43  T* p;
44  p = std::allocator<T>::allocate(n, hint);
45  if (p != NULL)
46  LockedPageManager::Instance().LockRange(p, sizeof(T) * n);
47  return p;
48  }
49 
50  void deallocate(T* p, std::size_t n)
51  {
52  if (p != NULL) {
53  memory_cleanse(p, sizeof(T) * n);
55  }
56  std::allocator<T>::deallocate(p, n);
57  }
58 };
59 
60 // This is exactly like std::string, but with a custom allocator.
61 typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
62 
63 typedef std::vector<unsigned char, secure_allocator<unsigned char> > SecureVector;
64 
65 #endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H
base::const_reference const_reference
Definition: secure.h:27
secure_allocator(const secure_allocator &a)
Definition: secure.h:30
base::pointer pointer
Definition: secure.h:24
secure_allocator()
Definition: secure.h:29
base::size_type size_type
Definition: secure.h:22
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:61
base::value_type value_type
Definition: secure.h:28
secure_allocator< _Other > other
Definition: secure.h:38
~secure_allocator()
Definition: secure.h:35
void memory_cleanse(void *ptr, size_t len)
Definition: cleanse.cpp:10
void LockRange(void *p, size_t size)
Definition: pagelocker.h:44
std::vector< unsigned char, secure_allocator< unsigned char > > SecureVector
Definition: secure.h:63
base::reference reference
Definition: secure.h:26
void UnlockRange(void *p, size_t size)
Definition: pagelocker.h:66
secure_allocator(const secure_allocator< U > &a)
Definition: secure.h:32
std::allocator< T > base
Definition: secure.h:21
base::const_pointer const_pointer
Definition: secure.h:25
T * allocate(std::size_t n, const void *hint=0)
Definition: secure.h:41
base::difference_type difference_type
Definition: secure.h:23
static LockedPageManager & Instance()
Definition: pagelocker.h:136
void deallocate(T *p, std::size_t n)
Definition: secure.h:50