Botan 3.6.1
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 277 of file mem_pool.cpp.

277 : m_page_size(page_size) {
278 m_min_page_ptr = ~static_cast<uintptr_t>(0);
279 m_max_page_ptr = 0;
280
281 for(auto page : pages) {
282 const uintptr_t p = reinterpret_cast<uintptr_t>(page);
283
284 m_min_page_ptr = std::min(p, m_min_page_ptr);
285 m_max_page_ptr = std::max(p, m_max_page_ptr);
286
287 clear_bytes(page, m_page_size);
288#if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
290#endif
291 m_free_pages.push_back(static_cast<uint8_t*>(page));
292 }
293
294 /*
295 Right now this points to the start of the last page, adjust it to instead
296 point to the first byte of the following page
297 */
298 m_max_page_ptr += page_size;
299}
void page_prohibit_access(void *page)
Definition os_utils.cpp:620
constexpr void clear_bytes(void *ptr, size_t bytes)
Definition mem_ops.h:103

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

◆ ~Memory_Pool()

Botan::Memory_Pool::~Memory_Pool ( )

Definition at line 301 of file mem_pool.cpp.

302{
303#if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
304 for(size_t i = 0; i != m_free_pages.size(); ++i) {
305 OS::page_allow_access(m_free_pages[i]);
306 }
307#endif
308}
void page_allow_access(void *page)
Definition os_utils.cpp:606

References Botan::OS::page_allow_access().

◆ Memory_Pool() [2/3]

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

◆ Memory_Pool() [3/3]

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

Member Function Documentation

◆ allocate()

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

Definition at line 310 of file mem_pool.cpp.

310 {
311 if(n > m_page_size) {
312 return nullptr;
313 }
314
315 const size_t n_bucket = choose_bucket(n);
316
317 if(n_bucket > 0) {
319
320 std::deque<Bucket>& buckets = m_buckets_for[n_bucket];
321
322 /*
323 It would be optimal to pick the bucket with the most usage,
324 since a bucket with say 1 item allocated out of it has a high
325 chance of becoming later freed and then the whole page can be
326 recycled.
327 */
328 for(auto& bucket : buckets) {
329 if(uint8_t* p = bucket.alloc()) {
330 return p;
331 }
332
333 // If the bucket is full, maybe move it to the end of the list?
334 // Otoh bucket search should be very fast
335 }
336
337 if(!m_free_pages.empty()) {
338 uint8_t* ptr = m_free_pages[0];
339 m_free_pages.pop_front();
340#if defined(BOTAN_MEM_POOL_USE_MMU_PROTECTIONS)
342#endif
343 buckets.push_front(Bucket(ptr, m_page_size, n_bucket));
344 void* p = buckets[0].alloc();
345 BOTAN_ASSERT_NOMSG(p != nullptr);
346 return p;
347 }
348 }
349
350 // out of room
351 return nullptr;
352}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
secure_vector< T > lock(const std::vector< T > &in)
Definition secmem.h:70
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 354 of file mem_pool.cpp.

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

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

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

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