Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

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/lockedpool.h>
10 #include <support/cleanse.h>
11 
12 #include <string>
13 #include <vector>
14 
15 //
16 // Allocator that locks its contents from being paged
17 // out of memory and clears its contents before deletion.
18 //
19 template <typename T>
20 struct secure_allocator : public std::allocator<T> {
21  // MSVC8 default copy constructor is broken
22  typedef std::allocator<T> base;
23  typedef typename base::size_type size_type;
24  typedef typename base::difference_type difference_type;
25  typedef typename base::pointer pointer;
26  typedef typename base::const_pointer const_pointer;
27  typedef typename base::reference reference;
28  typedef typename base::const_reference const_reference;
29  typedef typename base::value_type value_type;
30  secure_allocator() noexcept {}
31  secure_allocator(const secure_allocator& a) noexcept : base(a) {}
32  template <typename U>
33  secure_allocator(const secure_allocator<U>& a) noexcept : base(a)
34  {
35  }
36  ~secure_allocator() noexcept {}
37  template <typename _Other>
38  struct rebind {
40  };
41 
42  T* allocate(std::size_t n, const void* hint = 0)
43  {
44  return static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n));
45  }
46 
47  void deallocate(T* p, std::size_t n)
48  {
49  if (p != nullptr) {
50  memory_cleanse(p, sizeof(T) * n);
51  }
53  }
54 };
55 
56 // This is exactly like std::string, but with a custom allocator.
57 typedef std::basic_string<char, std::char_traits<char>, secure_allocator<char> > SecureString;
58 
59 typedef std::vector<unsigned char, secure_allocator<unsigned char> > SecureVector;
60 
61 #endif // BITCOIN_SUPPORT_ALLOCATORS_SECURE_H
base::const_reference const_reference
Definition: secure.h:28
base::pointer pointer
Definition: secure.h:25
static LockedPoolManager & Instance()
Return the current instance, or create it once.
Definition: lockedpool.h:213
base::size_type size_type
Definition: secure.h:23
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:57
base::value_type value_type
Definition: secure.h:29
secure_allocator< _Other > other
Definition: secure.h:39
secure_allocator() noexcept
Definition: secure.h:30
void memory_cleanse(void *ptr, size_t len)
Definition: cleanse.cpp:31
std::size_t size_t
Definition: bits.hpp:21
std::vector< unsigned char, secure_allocator< unsigned char > > SecureVector
Definition: secure.h:59
void * alloc(size_t size)
Allocate size bytes from this arena.
Definition: lockedpool.cpp:269
secure_allocator(const secure_allocator &a) noexcept
Definition: secure.h:31
base::reference reference
Definition: secure.h:27
std::allocator< T > base
Definition: secure.h:22
base::const_pointer const_pointer
Definition: secure.h:26
void free(void *ptr)
Free a previously allocated chunk of memory.
Definition: lockedpool.cpp:291
secure_allocator(const secure_allocator< U > &a) noexcept
Definition: secure.h:33
T * allocate(std::size_t n, const void *hint=0)
Definition: secure.h:42
base::difference_type difference_type
Definition: secure.h:24
void deallocate(T *p, std::size_t n)
Definition: secure.h:47
~secure_allocator() noexcept
Definition: secure.h:36
Released under the MIT license