aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/utils/locking_allocator/locking_allocator.cpp
blob: 9d05cfbffeb9f997cf8f1bf7c745a4a896630a51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
* Mlock Allocator
* (C) 2012,2014,2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/locking_allocator.h>
#include <botan/internal/os_utils.h>
#include <botan/internal/mem_pool.h>

namespace Botan {

void* mlock_allocator::allocate(size_t num_elems, size_t elem_size)
   {
   if(!m_pool)
      return nullptr;

   const size_t n = num_elems * elem_size;
   if(n / elem_size != num_elems)
      return nullptr; // overflow!

   return m_pool->allocate(n);
   }

bool mlock_allocator::deallocate(void* p, size_t num_elems, size_t elem_size) noexcept
   {
   if(!m_pool)
      return false;

   size_t n = num_elems * elem_size;

   /*
   We return nullptr in allocate if there was an overflow, so if an
   overflow occurs here we know the pointer was not allocated by this pool.
   */
   if(n / elem_size != num_elems)
      return false;

   return m_pool->deallocate(p, n);
   }

mlock_allocator::mlock_allocator()
   {
   const size_t mem_to_lock = OS::get_memory_locking_limit();

   if(mem_to_lock)
      {
      m_locked_pages = static_cast<uint8_t*>(OS::allocate_locked_pages(mem_to_lock));

      if(m_locked_pages)
         {
         m_locked_pages_size = mem_to_lock;
         m_pool.reset(new Memory_Pool(m_locked_pages,
                                      m_locked_pages_size,
                                      OS::system_page_size(),
                                      BOTAN_MLOCK_ALLOCATOR_MIN_ALLOCATION,
                                      BOTAN_MLOCK_ALLOCATOR_MAX_ALLOCATION,
                                      4));
         }
      }
   }

mlock_allocator::~mlock_allocator()
   {
   if(m_pool)
      {
      m_pool.reset();
      // OS::free_locked_pages scrubs the memory before free
      OS::free_locked_pages(m_locked_pages, m_locked_pages_size);
      }
   }

mlock_allocator& mlock_allocator::instance()
   {
   static mlock_allocator mlock;
   return mlock;
   }

}