Thrill
0.1
|
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)
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.
#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... | |
Pool & | operator= (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... | |
|
explicitnoexcept |
construct with base allocator
Definition at line 408 of file pool.cpp.
References Pool::AllocateFreeArena(), Pool::allocs_, Pool::arena_bin_, Pool::check_limit, Pool::debug_check_pairing, Pool::default_arena_size_, Pool::free_, Pool::min_free_, Pool::mutex_, Pool::num_bins, Pool::object_128_, Pool::object_256_, Pool::object_32_, and Pool::object_64_.
|
noexcept |
dtor
Definition at line 427 of file pool.cpp.
References Pool::allocs_, Pool::IntDeallocateAll(), Pool::mutex_, Pool::object_128_, Pool::object_256_, Pool::object_32_, Pool::object_64_, and Pool::size_.
void * allocate | ( | size_t | bytes | ) |
allocate a continuous segment of n bytes in the arenas
Definition at line 624 of file pool.cpp.
References Pool::AllocateFreeArena(), Pool::arena_bin_, Pool::ArenaFindFree(), Pool::bytes_per_arena(), thrill::mem::calc_bin_for_size(), Pool::debug, Pool::default_arena_size_, die, Pool::mutex_, Pool::num_bins, Pool::object_128_, Pool::object_256_, Pool::object_32_, and Pool::object_64_.
Referenced by thrill::mem::GPool(), and Pool::make().
|
private |
allocate a new Arena blob
Definition at line 537 of file pool.cpp.
References Pool::arena_bin_, thrill::mem::bypass_aligned_alloc(), thrill::mem::calc_bin_for_size(), Pool::debug, Pool::default_arena_size_, die_unequal, die_unless, Pool::free_, Pool::num_bins, and Pool::size_.
Referenced by Pool::allocate(), Pool::ArenaFindFree(), and Pool::Pool().
|
private |
find free area inside an Arena
Definition at line 449 of file pool.cpp.
References Pool::AllocateFreeArena(), Pool::allocs_, Pool::arena_bin_, thrill::mem::bin_lower_bound(), bytes, thrill::mem::calc_bin_for_size(), Pool::debug, Pool::debug_check_pairing, Pool::default_arena_size_, Pool::free_, Pool::min_free_, Pool::size_, and TLX_UNLIKELY.
Referenced by Pool::allocate().
|
private |
calculate maximum bytes fitting into an Arena with given size.
Definition at line 620 of file pool.cpp.
Referenced by Pool::allocate().
void deallocate | ( | void * | ptr, |
size_t | bytes | ||
) |
Deallocate a continuous segment of n bytes in the arenas, the size n MUST match the allocation.
Definition at line 700 of file pool.cpp.
References Pool::allocs_, Pool::arena_bin_, thrill::mem::bypass_aligned_free(), thrill::mem::calc_bin_for_size(), Pool::debug, Pool::debug_check_pairing, Pool::default_arena_size_, die_unless, Pool::free_, Pool::min_free_, Pool::mutex_, Pool::num_bins, Pool::object_128_, Pool::object_256_, Pool::object_32_, Pool::object_64_, and Pool::size_.
Referenced by Pool::destroy(), and thrill::mem::GPool().
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().
|
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()().
|
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().
|
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().
|
noexcept |
maximum size possible to allocate
Definition at line 616 of file pool.cpp.
References max().
Referenced by Pool::destroy().
non-copyable: delete assignment operator
Referenced by FixedPoolAllocator< Type, pool_ >::FixedPoolAllocator(), and PoolAllocator< Type >::PoolAllocator().
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().
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().
|
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().
|
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().
|
staticprivate |
Definition at line 80 of file pool.hpp.
Referenced by Pool::Pool().
|
staticprivate |
Definition at line 76 of file pool.hpp.
Referenced by Pool::allocate(), Pool::AllocateFreeArena(), Pool::ArenaFindFree(), Pool::deallocate(), and thrill::mem::GPool().
|
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().
|
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().
|
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().
|
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().
|
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().
|
staticprivate |
number of bins
Definition at line 148 of file pool.hpp.
Referenced by Pool::allocate(), Pool::AllocateFreeArena(), Pool::deallocate(), Pool::IntDeallocateAll(), Pool::Pool(), and Pool::print().
|
private |
Definition at line 168 of file pool.hpp.
Referenced by Pool::allocate(), Pool::deallocate(), Pool::Pool(), and Pool::~Pool().
|
private |
Definition at line 169 of file pool.hpp.
Referenced by Pool::allocate(), Pool::deallocate(), Pool::Pool(), and Pool::~Pool().
|
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().
|
private |
Definition at line 167 of file pool.hpp.
Referenced by Pool::allocate(), Pool::deallocate(), Pool::Pool(), and Pool::~Pool().
|
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().