Botan 3.8.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)
 
 Memory_Pool (Memory_Pool &&)=delete
 
Memory_Pooloperator= (const Memory_Pool &)=delete
 
Memory_Pooloperator= (Memory_Pool &&)=delete
 
 ~Memory_Pool ()
 

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 )

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 295 of file mem_pool.cpp.

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

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 ( )

Definition at line 319 of file mem_pool.cpp.

320{
321#if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
322 for(size_t i = 0; i != m_free_pages.size(); ++i) {
323 OS::page_allow_access(m_free_pages[i]);
324 }
325#endif
326}
void page_allow_access(void *page)
Definition os_utils.cpp:599

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 328 of file mem_pool.cpp.

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

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

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: