Botan 3.6.1
Crypto and TLS for C&
Botan::mlock_allocator Class Referencefinal

#include <locking_allocator.h>

Public Member Functions

void * allocate (size_t num_elems, size_t elem_size)
 
bool deallocate (void *p, size_t num_elems, size_t elem_size) noexcept
 
 mlock_allocator ()
 
 mlock_allocator (const mlock_allocator &)=delete
 
mlock_allocatoroperator= (const mlock_allocator &)=delete
 
 ~mlock_allocator ()
 

Static Public Member Functions

static mlock_allocatorinstance ()
 

Detailed Description

Definition at line 19 of file locking_allocator.h.

Constructor & Destructor Documentation

◆ mlock_allocator() [1/2]

Botan::mlock_allocator::mlock_allocator ( const mlock_allocator & )
delete

◆ mlock_allocator() [2/2]

Botan::mlock_allocator::mlock_allocator ( )

Definition at line 45 of file locking_allocator.cpp.

45 {
46 const size_t mem_to_lock = OS::get_memory_locking_limit();
47 const size_t page_size = OS::system_page_size();
48
49 if(mem_to_lock > 0 && mem_to_lock % page_size == 0) {
50 m_locked_pages = OS::allocate_locked_pages(mem_to_lock / page_size);
51
52 if(!m_locked_pages.empty()) {
53 m_pool = std::make_unique<Memory_Pool>(m_locked_pages, page_size);
54 }
55 }
56}
size_t get_memory_locking_limit()
Definition os_utils.cpp:381
std::vector< void * > allocate_locked_pages(size_t count)
Definition os_utils.cpp:515
size_t system_page_size()
Definition os_utils.cpp:361

References Botan::OS::allocate_locked_pages(), Botan::OS::get_memory_locking_limit(), and Botan::OS::system_page_size().

◆ ~mlock_allocator()

Botan::mlock_allocator::~mlock_allocator ( )

Definition at line 58 of file locking_allocator.cpp.

58 {
59 if(m_pool) {
60 m_pool.reset();
61 // OS::free_locked_pages scrubs the memory before free
62 OS::free_locked_pages(m_locked_pages);
63 }
64}
void free_locked_pages(const std::vector< void * > &pages)
Definition os_utils.cpp:634

References Botan::OS::free_locked_pages().

Member Function Documentation

◆ allocate()

void * Botan::mlock_allocator::allocate ( size_t num_elems,
size_t elem_size )

Definition at line 16 of file locking_allocator.cpp.

16 {
17 if(!m_pool) {
18 return nullptr;
19 }
20
21 if(auto n = checked_mul(num_elems, elem_size)) {
22 return m_pool->allocate(n.value());
23 } else {
24 // overflow!
25 return nullptr;
26 }
27}
constexpr std::optional< T > checked_mul(T a, T b)
Definition int_utils.h:46

References Botan::checked_mul().

◆ deallocate()

bool Botan::mlock_allocator::deallocate ( void * p,
size_t num_elems,
size_t elem_size )
noexcept

Definition at line 29 of file locking_allocator.cpp.

29 {
30 if(!m_pool) {
31 return false;
32 }
33
34 if(auto n = checked_mul(num_elems, elem_size)) {
35 return m_pool->deallocate(p, n.value());
36 } else {
37 /*
38 We return nullptr in allocate if there was an overflow, so if an
39 overflow occurs here we know the pointer was not allocated by this pool.
40 */
41 return false;
42 }
43}

References Botan::checked_mul().

◆ instance()

mlock_allocator & Botan::mlock_allocator::instance ( )
static

Definition at line 73 of file locking_allocator.cpp.

73 {
74 return g_mlock_allocator;
75}

Referenced by Botan::allocate_memory(), Botan::deallocate_memory(), and Botan::initialize_allocator().

◆ operator=()

mlock_allocator & Botan::mlock_allocator::operator= ( const mlock_allocator & )
delete

The documentation for this class was generated from the following files: