Thrill  0.1
Pool Class Reference

Detailed Description

A simple memory allocation manager.

The Pool gets chunks of memory of size ArenaSize from new/delete and delivers smaller byte areas to PoolAllocator.

The main reason for this allocation Pool is to keep memory for allocation of I/O data structures once the main malloc() memory pool runs out. The allocator itself may not be as faster as possible.

An Arena is organized as a singly-linked list of continuous free areas, where the free information is stored inside the free memory as a Slot. A Slot is exactly 8 bytes (containing only two uint32_t). All allocations are rounded up to a multiple of 8.

+–+--------—+---—+------------—+--------—+----------------------—+ |XX| head_slot | used | free slot ... | used .... | free slot ....... | +–+--------—+---—+------------—+--------—+----------------------—+ | ^ | ^ +---------------—+ +---------------------—+ (next indexes)

  • XX = additional Header information
  • head_slot = sentinel Slot containing .size as the free slots, and .next to the first free slot.
  • each free Slot contains .size and .next. Size is the amount of free slots in a free area. Next is the integer offset of the following free slot (information) or the end of the Arena.

During allocation the next fitting free slot is searched for. During deallocation multiple free areas may be consolidated.

For faster allocation, Arenas are categorized into many bins. Bin k always contains all Arenas with log_2(k) to log_2(k+1)-1 free space in them. On allocation and deallocation, the Arenas are moved between bins.

Definition at line 74 of file pool.hpp.

#include <pool.hpp>

Public Member Functions

 Pool (size_t default_arena_size=16384) noexcept
 construct with base allocator More...
 
 Pool (const Pool &)=delete
 non-copyable: delete copy-constructor More...
 
 ~Pool () noexcept
 dtor More...
 
void * allocate (size_t bytes)
 allocate a continuous segment of n bytes in the arenas More...
 
void deallocate (void *ptr, size_t bytes)
 
void DeallocateAll ()
 deallocate all Arenas More...
 
template<typename Type >
void destroy (Type *t)
 Destroy and deallocate a single item of given Type. More...
 
template<typename Type , typename... Args>
Type * make (Args &&... args)
 
size_t max_size () const noexcept
 maximum size possible to allocate More...
 
Pooloperator= (const Pool &)=delete
 non-copyable: delete assignment operator More...
 
void print (bool debug=true)
 Print out structure of the arenas. More...
 
void self_verify ()
 Print out structure of the arenas. More...
 

Private Member Functions

Arena * AllocateFreeArena (size_t arena_size, bool die_on_failure=true)
 allocate a new Arena blob More...
 
void * ArenaFindFree (Arena *curr_arena, size_t bin, size_t n, size_t bytes)
 find free area inside an Arena More...
 
size_t bytes_per_arena (size_t arena_size)
 calculate maximum bytes fitting into an Arena with given size. More...
 
void IntDeallocateAll ()
 deallocate all Arenas More...
 

Private Attributes

std::vector< std::pair< void *, size_t > > allocs_
 array of allocations for checking More...
 
Arena * arena_bin_ [num_bins+1]
 
size_t default_arena_size_
 size of default Arena allocation More...
 
size_t free_ = 0
 number of free slots in all arenas More...
 
size_t min_free_ = 1024 * 1024 / 8
 minimum amount of spare memory to keep in the Pool. More...
 
std::mutex mutex_
 
ObjectPool * object_128_
 
ObjectPool * object_256_
 
ObjectPool * object_32_
 object areas for small fixed size items More...
 
ObjectPool * object_64_
 
size_t size_ = 0
 overall number of used slots More...
 

Static Private Attributes

static constexpr size_t check_limit = 4 * 1024 * 1024
 
static constexpr bool debug = false
 
static constexpr bool debug_check_pairing = false
 debug flag to check pairing of allocate()/deallocate() client calls More...
 
static constexpr bool debug_verify = false
 
static const size_t num_bins = 14 - 3 + 1
 number of bins More...
 

Constructor & Destructor Documentation

◆ Pool() [1/2]

◆ Pool() [2/2]

Pool ( const Pool )
delete

non-copyable: delete copy-constructor

◆ ~Pool()

Member Function Documentation

◆ allocate()

◆ AllocateFreeArena()

Pool::Arena * AllocateFreeArena ( size_t  arena_size,
bool  die_on_failure = true 
)
private

◆ ArenaFindFree()

void * ArenaFindFree ( Arena *  curr_arena,
size_t  bin,
size_t  n,
size_t  bytes 
)
private

◆ bytes_per_arena()

size_t bytes_per_arena ( size_t  arena_size)
private

calculate maximum bytes fitting into an Arena with given size.

Definition at line 620 of file pool.cpp.

Referenced by Pool::allocate().

◆ deallocate()

void deallocate ( void *  ptr,
size_t  bytes 
)

◆ DeallocateAll()

void DeallocateAll ( )

deallocate all Arenas

Definition at line 599 of file pool.cpp.

References Pool::IntDeallocateAll(), and Pool::mutex_.

Referenced by Pool::destroy(), and thrill::mem::GPool().

◆ destroy()

void destroy ( Type *  t)
inline

Destroy and deallocate a single item of given Type.

Definition at line 112 of file pool.hpp.

References Pool::deallocate(), Pool::DeallocateAll(), Pool::max_size(), Pool::print(), and Pool::self_verify().

Referenced by ByteBlock::Deleter::operator()(), and GPoolDeleter< T >::operator()().

◆ IntDeallocateAll()

void IntDeallocateAll ( )
private

deallocate all Arenas

Definition at line 604 of file pool.cpp.

References Pool::arena_bin_, thrill::mem::bypass_aligned_free(), Pool::min_free_, and Pool::num_bins.

Referenced by Pool::DeallocateAll(), and Pool::~Pool().

◆ make()

Type* make ( Args &&...  args)
inline

Allocate and construct a single item of given Type using memory from the Pool.

Definition at line 104 of file pool.hpp.

References Pool::allocate().

Referenced by thrill::mem::safe_make_unique().

◆ max_size()

size_t max_size ( ) const
noexcept

maximum size possible to allocate

Definition at line 616 of file pool.cpp.

References max().

Referenced by Pool::destroy().

◆ operator=()

Pool& operator= ( const Pool )
delete

non-copyable: delete assignment operator

Referenced by FixedPoolAllocator< Type, pool_ >::FixedPoolAllocator(), and PoolAllocator< Type >::PoolAllocator().

◆ print()

void print ( bool  debug = true)

Print out structure of the arenas.

Definition at line 867 of file pool.cpp.

References Pool::arena_bin_, thrill::mem::calc_bin_for_size(), die_unequal, die_unless, free(), Pool::free_, Pool::num_bins, and Pool::size_.

Referenced by Pool::destroy(), and Pool::self_verify().

◆ self_verify()

void self_verify ( )

Print out structure of the arenas.

Definition at line 931 of file pool.cpp.

References Pool::print().

Referenced by Pool::destroy(), and thrill::mem::GPool().

Member Data Documentation

◆ allocs_

std::vector<std::pair<void*, size_t> > allocs_
private

array of allocations for checking

Definition at line 172 of file pool.hpp.

Referenced by Pool::ArenaFindFree(), Pool::deallocate(), Pool::Pool(), and Pool::~Pool().

◆ arena_bin_

Arena* arena_bin_[num_bins+1]
private

pointer to first arena in each bin, arenas are in allocation order, the last bin is for overflow allocations.

Definition at line 152 of file pool.hpp.

Referenced by Pool::allocate(), Pool::AllocateFreeArena(), Pool::ArenaFindFree(), Pool::deallocate(), Pool::IntDeallocateAll(), Pool::Pool(), and Pool::print().

◆ check_limit

constexpr size_t check_limit = 4 * 1024 * 1024
staticprivate

Definition at line 80 of file pool.hpp.

Referenced by Pool::Pool().

◆ debug

constexpr bool debug = false
staticprivate

◆ debug_check_pairing

constexpr bool debug_check_pairing = false
staticprivate

debug flag to check pairing of allocate()/deallocate() client calls

Definition at line 79 of file pool.hpp.

Referenced by Pool::ArenaFindFree(), Pool::deallocate(), and Pool::Pool().

◆ debug_verify

constexpr bool debug_verify = false
staticprivate

Definition at line 77 of file pool.hpp.

◆ default_arena_size_

size_t default_arena_size_
private

size of default Arena allocation

Definition at line 160 of file pool.hpp.

Referenced by Pool::allocate(), Pool::AllocateFreeArena(), Pool::ArenaFindFree(), Pool::deallocate(), and Pool::Pool().

◆ free_

size_t free_ = 0
private

number of free slots in all arenas

Definition at line 155 of file pool.hpp.

Referenced by Pool::AllocateFreeArena(), Pool::ArenaFindFree(), Pool::deallocate(), thrill::mem::GPool(), Pool::Pool(), and Pool::print().

◆ min_free_

size_t min_free_ = 1024 * 1024 / 8
private

minimum amount of spare memory to keep in the Pool.

Definition at line 163 of file pool.hpp.

Referenced by Pool::ArenaFindFree(), Pool::deallocate(), Pool::IntDeallocateAll(), and Pool::Pool().

◆ mutex_

std::mutex mutex_
private

mutex to protect data structures (remove this if you use it in another context than Thrill).

Definition at line 140 of file pool.hpp.

Referenced by Pool::allocate(), Pool::deallocate(), Pool::DeallocateAll(), Pool::Pool(), and Pool::~Pool().

◆ num_bins

const size_t num_bins = 14 - 3 + 1
staticprivate

◆ object_128_

ObjectPool* object_128_
private

Definition at line 168 of file pool.hpp.

Referenced by Pool::allocate(), Pool::deallocate(), Pool::Pool(), and Pool::~Pool().

◆ object_256_

ObjectPool* object_256_
private

Definition at line 169 of file pool.hpp.

Referenced by Pool::allocate(), Pool::deallocate(), Pool::Pool(), and Pool::~Pool().

◆ object_32_

ObjectPool* object_32_
private

object areas for small fixed size items

Definition at line 166 of file pool.hpp.

Referenced by Pool::allocate(), Pool::deallocate(), Pool::Pool(), and Pool::~Pool().

◆ object_64_

ObjectPool* object_64_
private

Definition at line 167 of file pool.hpp.

Referenced by Pool::allocate(), Pool::deallocate(), Pool::Pool(), and Pool::~Pool().

◆ size_

size_t size_ = 0
private

overall number of used slots

Definition at line 157 of file pool.hpp.

Referenced by Pool::AllocateFreeArena(), Pool::ArenaFindFree(), Pool::deallocate(), thrill::mem::GPool(), Pool::print(), and Pool::~Pool().


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