Botan 3.9.0
Crypto and TLS for C&
Botan::Memory_Pool Class Referencefinal

#include <mem_pool.h>

Public Member Functions

void * allocate (size_t size)
bool deallocate (void *p, size_t size) noexcept
 Memory_Pool (const Memory_Pool &)=delete
 Memory_Pool (const std::vector< void * > &pages, size_t page_size) noexcept
 Memory_Pool (Memory_Pool &&)=delete
Memory_Pooloperator= (const Memory_Pool &)=delete
Memory_Pooloperator= (Memory_Pool &&)=delete
 ~Memory_Pool () noexcept

Detailed Description

Definition at line 20 of file mem_pool.h.

Constructor & Destructor Documentation

◆ Memory_Pool() [1/3]

Botan::Memory_Pool::Memory_Pool ( const std::vector< void * > & pages,
size_t page_size )
noexcept

Initialize a memory pool. The memory is not owned by *this, it must be freed by the caller.

Parameters
pagesa list of pages to allocate from
page_sizethe system page size, each page should point to exactly this much memory.

Definition at line 293 of file mem_pool.cpp.

293 : m_page_size(page_size) {
294 for(auto* page : pages) {
295 const uintptr_t p = reinterpret_cast<uintptr_t>(page);
296
297 m_min_page_ptr = std::min(p, m_min_page_ptr);
298 m_max_page_ptr = std::max(p, m_max_page_ptr);
299
300 clear_bytes(page, m_page_size);
301#if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
303#endif
304 m_free_pages.push_back(static_cast<uint8_t*>(page));
305 }
306
307 /*
308 Right now this points to the start of the last page, adjust it to instead
309 point to the first byte of the following page
310 */
311 m_max_page_ptr += page_size;
312}
void page_prohibit_access(void *page)
Definition os_utils.cpp:623
constexpr void clear_bytes(void *ptr, size_t bytes)
Definition mem_ops.h:102

References Botan::clear_bytes(), and Botan::OS::page_prohibit_access().

Referenced by Memory_Pool(), Memory_Pool(), operator=(), and operator=().

◆ ~Memory_Pool()

Botan::Memory_Pool::~Memory_Pool ( )
noexcept

Definition at line 314 of file mem_pool.cpp.

315{
316#if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
317 for(size_t i = 0; i != m_free_pages.size(); ++i) {
318 OS::page_allow_access(m_free_pages[i]);
319 }
320#endif
321}
void page_allow_access(void *page)
Definition os_utils.cpp:609

References Botan::OS::page_allow_access().

◆ Memory_Pool() [2/3]

Botan::Memory_Pool::Memory_Pool ( const Memory_Pool & )
delete

References Memory_Pool().

◆ Memory_Pool() [3/3]

Botan::Memory_Pool::Memory_Pool ( Memory_Pool && )
delete

References Memory_Pool().

Member Function Documentation

◆ allocate()

void * Botan::Memory_Pool::allocate ( size_t size)

Definition at line 323 of file mem_pool.cpp.

323 {
324 if(n > m_page_size) {
325 return nullptr;
326 }
327
328 const size_t n_bucket = choose_bucket(n);
329
330 if(n_bucket > 0) {
332
333 std::deque<Bucket>& buckets = m_buckets_for[n_bucket];
334
335 /*
336 It would be optimal to pick the bucket with the most usage,
337 since a bucket with say 1 item allocated out of it has a high
338 chance of becoming later freed and then the whole page can be
339 recycled.
340 */
341 for(auto& bucket : buckets) {
342 if(uint8_t* p = bucket.alloc()) {
343 return p;
344 }
345
346 // If the bucket is full, maybe move it to the end of the list?
347 // Otoh bucket search should be very fast
348 }
349
350 if(!m_free_pages.empty()) {
351 uint8_t* ptr = m_free_pages[0];
352 m_free_pages.pop_front();
353#if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
355#endif
356 buckets.push_front(Bucket(ptr, m_page_size, n_bucket));
357 void* p = buckets[0].alloc();
358 BOTAN_ASSERT_NOMSG(p != nullptr);
359 return p;
360 }
361 }
362
363 // out of room
364 return nullptr;
365}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
secure_vector< T > lock(const std::vector< T > &in)
Definition secmem.h:81
lock_guard< T > lock_guard_type
Definition mutex.h:55

References BOTAN_ASSERT_NOMSG, Botan::lock(), and Botan::OS::page_allow_access().

◆ deallocate()

bool Botan::Memory_Pool::deallocate ( void * p,
size_t size )
noexcept

Definition at line 367 of file mem_pool.cpp.

367 {
368 // Do a fast range check first, before taking the lock
369 const uintptr_t p_val = reinterpret_cast<uintptr_t>(p);
370 if(p_val < m_min_page_ptr || p_val > m_max_page_ptr) {
371 return false;
372 }
373
374 const size_t n_bucket = choose_bucket(len);
375
376 if(n_bucket != 0) {
377 try {
379
380 std::deque<Bucket>& buckets = m_buckets_for[n_bucket];
381
382 for(size_t i = 0; i != buckets.size(); ++i) {
383 Bucket& bucket = buckets[i];
384 if(bucket.free(p)) {
385 if(bucket.empty()) {
386#if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
387 OS::page_prohibit_access(bucket.ptr());
388#endif
389 m_free_pages.push_back(bucket.ptr());
390
391 if(i != buckets.size() - 1) {
392 std::swap(buckets.back(), buckets[i]);
393 }
394 buckets.pop_back();
395 }
396 return true;
397 }
398 }
399 } catch(...) {
400 /*
401 * The only exception throws that can occur in the above code are from
402 * either the STL or BOTAN_ASSERT failures. In either case, such an
403 * error indicates a logic error or data corruption in the memory
404 * allocator such that it is no longer safe to continue executing.
405 *
406 * Since this function is noexcept, simply letting the exception escape
407 * is sufficient for terminate to be called. However in this scenario
408 * it is implementation defined if any stack unwinding is performed.
409 * Since stack unwinding could cause further memory deallocations this
410 * could result in further corruption in this allocator state. To prevent
411 * this, call terminate directly.
412 */
413 std::terminate();
414 }
415 }
416
417 return false;
418}

References Botan::lock(), and Botan::OS::page_prohibit_access().

◆ operator=() [1/2]

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

References Memory_Pool().

◆ operator=() [2/2]

Memory_Pool & Botan::Memory_Pool::operator= ( Memory_Pool && )
delete

References Memory_Pool().


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