#include <mem_pool.h>
Definition at line 20 of file mem_pool.h.
Botan::Memory_Pool::Memory_Pool |
( |
const std::vector< void * > & |
pages, |
|
|
size_t |
page_size |
|
) |
| |
Initialize a memory pool. The memory is not owned by *this, it must be freed by the caller.
- Parameters
-
pages | a list of pages to allocate from |
page_size | the system page size, each page should point to exactly this much memory. |
Definition at line 293 of file mem_pool.cpp.
References Botan::clear_bytes(), and Botan::OS::page_prohibit_access().
294 m_page_size(page_size)
296 m_min_page_ptr = ~static_cast<uintptr_t>(0);
299 for(
size_t i = 0; i != pages.size(); ++i)
301 const uintptr_t p =
reinterpret_cast<uintptr_t
>(pages[i]);
303 m_min_page_ptr = std::min(p, m_min_page_ptr);
304 m_max_page_ptr = std::max(p, m_max_page_ptr);
307 #if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
310 m_free_pages.push_back(static_cast<uint8_t*>(pages[i]));
317 m_max_page_ptr += page_size;
void clear_bytes(void *ptr, size_t bytes)
void page_prohibit_access(void *page)
Botan::Memory_Pool::~Memory_Pool |
( |
| ) |
|
Definition at line 320 of file mem_pool.cpp.
References Botan::OS::page_allow_access().
322 #if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
323 for(
size_t i = 0; i != m_free_pages.size(); ++i)
void page_allow_access(void *page)
Botan::Memory_Pool::Memory_Pool |
( |
const Memory_Pool & |
| ) |
|
|
delete |
void * Botan::Memory_Pool::allocate |
( |
size_t |
size | ) |
|
Definition at line 330 of file mem_pool.cpp.
References BOTAN_ASSERT_NOMSG, and Botan::OS::page_allow_access().
335 const size_t n_bucket = choose_bucket(n);
339 lock_guard_type<mutex_type> lock(m_mutex);
341 std::deque<Bucket>& buckets = m_buckets_for[n_bucket];
349 for(
auto& bucket : buckets)
351 if(uint8_t* p = bucket.alloc())
358 if(m_free_pages.size() > 0)
360 uint8_t* ptr = m_free_pages[0];
361 m_free_pages.pop_front();
362 #if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
365 buckets.push_front(Bucket(ptr, m_page_size, n_bucket));
366 void* p = buckets[0].alloc();
#define BOTAN_ASSERT_NOMSG(expr)
void page_allow_access(void *page)
bool Botan::Memory_Pool::deallocate |
( |
void * |
p, |
|
|
size_t |
size |
|
) |
| |
|
noexcept |
Definition at line 376 of file mem_pool.cpp.
References m_mutex, and Botan::OS::page_prohibit_access().
379 const uintptr_t p_val =
reinterpret_cast<uintptr_t
>(p);
380 if(p_val < m_min_page_ptr || p_val > m_max_page_ptr)
383 const size_t n_bucket = choose_bucket(len);
389 lock_guard_type<mutex_type> lock(m_mutex);
391 std::deque<Bucket>& buckets = m_buckets_for[n_bucket];
393 for(
size_t i = 0; i != buckets.size(); ++i)
395 Bucket& bucket = buckets[i];
400 #if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
403 m_free_pages.push_back(bucket.ptr());
405 if(i != buckets.size() - 1)
406 std::swap(buckets.back(), buckets[i]);
void page_prohibit_access(void *page)
The documentation for this class was generated from the following files: