Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

pooled_secure.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2018 The Dash Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_SUPPORT_ALLOCATORS_POOLED_SECURE_H
6 #define BITCOIN_SUPPORT_ALLOCATORS_POOLED_SECURE_H
7 
8 #include <support/lockedpool.h>
9 #include <support/cleanse.h>
10 
11 #include <string>
12 #include <vector>
13 
14 #include <boost/pool/pool_alloc.hpp>
15 
16 //
17 // Allocator that allocates memory in chunks from a pool, which in turn allocates larger chunks from secure memory
18 // Memory is cleaned when freed as well. This allocator is NOT thread safe
19 //
20 template <typename T>
21 struct pooled_secure_allocator : public std::allocator<T> {
22  // MSVC8 default copy constructor is broken
23  typedef std::allocator<T> base;
24  typedef typename base::size_type size_type;
25  typedef typename base::difference_type difference_type;
26  typedef typename base::pointer pointer;
27  typedef typename base::const_pointer const_pointer;
28  typedef typename base::reference reference;
29  typedef typename base::const_reference const_reference;
30  typedef typename base::value_type value_type;
31  pooled_secure_allocator(const size_type nrequested_size = 32,
32  const size_type nnext_size = 32,
33  const size_type nmax_size = 0) noexcept :
34  pool(nrequested_size, nnext_size, nmax_size){}
36 
37  T* allocate(std::size_t n, const void* hint = 0)
38  {
39  size_t chunks = (n * sizeof(T) + pool.get_requested_size() - 1) / pool.get_requested_size();
40  return static_cast<T*>(pool.ordered_malloc(chunks));
41  }
42 
43  void deallocate(T* p, std::size_t n)
44  {
45  if (!p) {
46  return;
47  }
48 
49  size_t chunks = (n * sizeof(T) + pool.get_requested_size() - 1) / pool.get_requested_size();
50  memory_cleanse(p, chunks * pool.get_requested_size());
51  pool.ordered_free(p, chunks);
52  }
53 
54 public:
57  typedef std::ptrdiff_t difference_type;
58 
59  static char* malloc(const size_type bytes)
60  {
61  return static_cast<char*>(LockedPoolManager::Instance().alloc(bytes));
62  }
63 
64  static void free(char* const block)
65  {
67  }
68  };
69 private:
70  boost::pool<internal_secure_allocator> pool;
71 };
72 
73 #endif // BITCOIN_SUPPORT_ALLOCATORS_POOLED_SECURE_H
base::const_reference const_reference
Definition: pooled_secure.h:29
base::reference reference
Definition: pooled_secure.h:28
static LockedPoolManager & Instance()
Return the current instance, or create it once.
Definition: lockedpool.h:213
~pooled_secure_allocator() noexcept
Definition: pooled_secure.h:35
base::const_pointer const_pointer
Definition: pooled_secure.h:27
base::size_type size_type
Definition: pooled_secure.h:24
void deallocate(T *p, std::size_t n)
Definition: pooled_secure.h:43
base::value_type value_type
Definition: pooled_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::allocator< T > base
Definition: pooled_secure.h:23
void * alloc(size_t size)
Allocate size bytes from this arena.
Definition: lockedpool.cpp:269
base::difference_type difference_type
Definition: pooled_secure.h:25
void free(void *ptr)
Free a previously allocated chunk of memory.
Definition: lockedpool.cpp:291
pooled_secure_allocator(const size_type nrequested_size=32, const size_type nnext_size=32, const size_type nmax_size=0) noexcept
Definition: pooled_secure.h:31
boost::pool< internal_secure_allocator > pool
Definition: pooled_secure.h:70
static char * malloc(const size_type bytes)
Definition: pooled_secure.h:59
T * allocate(std::size_t n, const void *hint=0)
Definition: pooled_secure.h:37
Released under the MIT license