PIVX Core  5.6.99
P2P Digital Currency
secure.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 
7 #ifndef PIVX_SUPPORT_ALLOCATORS_SECURE_H
8 #define PIVX_SUPPORT_ALLOCATORS_SECURE_H
9 
10 #include "support/lockedpool.h"
11 #include "support/cleanse.h"
12 
13 #include <string>
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  T* allocation = static_cast<T*>(LockedPoolManager::Instance().alloc(sizeof(T) * n));
45  if (!allocation) {
46  throw std::bad_alloc();
47  }
48  return allocation;
49  }
50 
51  void deallocate(T* p, std::size_t n)
52  {
53  if (p != nullptr) {
54  memory_cleanse(p, sizeof(T) * n);
55  }
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 #endif // PIVX_SUPPORT_ALLOCATORS_SECURE_H
void free(void *ptr)
Free a previously allocated chunk of memory.
Definition: lockedpool.cpp:322
void * alloc(size_t size)
Allocate size bytes from this arena.
Definition: lockedpool.cpp:300
static LockedPoolManager & Instance()
Return the current instance, or create it once.
Definition: lockedpool.h:223
void memory_cleanse(void *ptr, size_t len)
Definition: cleanse.cpp:27
#define T(expected, seed, data)
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
Definition: secure.h:61
secure_allocator< _Other > other
Definition: secure.h:39
base::value_type value_type
Definition: secure.h:29
base::const_reference const_reference
Definition: secure.h:28
base::difference_type difference_type
Definition: secure.h:24
base::reference reference
Definition: secure.h:27
base::pointer pointer
Definition: secure.h:25
secure_allocator(const secure_allocator< U > &a) noexcept
Definition: secure.h:33
~secure_allocator() noexcept
Definition: secure.h:36
base::size_type size_type
Definition: secure.h:23
base::const_pointer const_pointer
Definition: secure.h:26
secure_allocator() noexcept
Definition: secure.h:30
secure_allocator(const secure_allocator &a) noexcept
Definition: secure.h:31
std::allocator< T > base
Definition: secure.h:22
T * allocate(std::size_t n, const void *hint=0)
Definition: secure.h:42
void deallocate(T *p, std::size_t n)
Definition: secure.h:51