Documentation of memory
( foonathan/memory)
memory_pool< PoolType, BlockOrRawAllocator > Class Template Reference
Detailed Description
template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>
class foonathan::memory::memory_pool< PoolType, BlockOrRawAllocator >
A stateful RawAllocator that manages nodes of fixed size.
It uses a memory_arena with a given BlockOrRawAllocator
defaulting to growing_block_allocator, subdivides them in small nodes of given size and puts them onto a free list. Allocation and deallocation simply remove or add nodes from this list and are thus fast. The way the list is maintained can be controlled via the PoolType
which is either node_pool, array_pool or small_node_pool.
This kind of allocator is ideal for fixed size allocations and deallocations in any order, for example in a node based container like std::list
. It is not so good for different allocation sizes and has some drawbacks for arrays as described in memory_pool_type.hpp.
Typedefs | |
using | allocator_type = make_block_allocator_t< BlockOrRawAllocator > |
using | pool_type = PoolType |
Constants | |
static constexpr std::size_t | min_node_size |
Member Functions | |
template<typename... Args> | |
memory_pool (std::size_t node_size, std::size_t block_size, Args &&...args) | |
~memory_pool () noexcept | |
void * | allocate_node () |
void * | allocate_array (std::size_t n) |
void | deallocate_node (void *ptr) noexcept |
void | deallocate_array (void *ptr, std::size_t n) noexcept |
std::size_t | node_size () const noexcept |
std::size_t | capacity_left () const noexcept |
std::size_t | next_capacity () const noexcept |
allocator_type & | get_allocator () noexcept |
memory_pool (memory_pool &&other) noexcept | |
memory_pool & | operator= (memory_pool &&other) noexcept |
Constructors
memory_pool | ( | std::size_t | node_size, |
std::size_t | block_size, | ||
Args &&... | args | ||
) |
- Effects:
- Creates it by specifying the size each node will have, the initial block size for the arena and other constructor arguments for the BlockAllocator. If the
node_size
is less than themin_node_size
, themin_node_size
will be the actual node size. It will allocate an initial memory block with given size from the BlockAllocator and puts it onto the free list.
- Requires:
node_size
must be a valid node size andblock_size
must be a non-zero value.
|
noexcept |
- Effects:
- Destroys the memory_pool by returning all memory blocks, regardless of properly deallocated back to the BlockAllocator.
|
noexcept |
- Effects:
- Moving a memory_pool object transfers ownership over the free list, i.e. the moved from pool is completely empty and the new one has all its memory. That means that it is not allowed to call deallocate_node() on a moved-from allocator even when passing it memory that was previously allocated by this object.
Member Functions
|
noexcept |
- Effects:
- Moving a memory_pool object transfers ownership over the free list, i.e. the moved from pool is completely empty and the new one has all its memory. That means that it is not allowed to call deallocate_node() on a moved-from allocator even when passing it memory that was previously allocated by this object.
void* allocate_node | ( | ) |
- Effects:
- Allocates a single node by removing it from the free list. If the free list is empty, a new memory block will be allocated from the arena and put onto it. The new block size will be next_capacity() big.
- Returns:
- A node of size node_size() suitable aligned, i.e. suitable for any type where
sizeof(T) < node_size()
.
- Throws:
- Anything thrown by the used BlockAllocator's allocation function if a growth is needed.
void* allocate_array | ( | std::size_t | n | ) |
- Effects:
- Allocates an array of nodes by searching for
n
continuous nodes on the list and removing them. Depending on thePoolType
this can be a slow operation or not allowed at all. This can sometimes lead to a growth, even if technically there is enough continuous memory on the free list.
- Returns:
- An array of
n
nodes of size node_size() suitable aligned.
- Throws:
- Anything thrown by the used BlockAllocator's allocation function if a growth is needed, or bad_array_size if
n * node_size()
is too big.
- Requires:
n
must be valid array count.
|
noexcept |
- Effects:
- Deallocates a single node by putting it back onto the free list.
- Requires:
ptr
must be a result from a previous call to allocate_node() on the same free list, i.e. either this allocator object or a new object created by moving this to it.
|
noexcept |
- Effects:
- Deallocates an array by putting it back onto the free list.
- Requires:
ptr
must be a result from a previous call to allocate_array() with the samen
on the same free list, i.e. either this allocator object or a new object created by moving this to it.
|
noexcept |
- Returns:
- The size of each node in the pool, this is either the same value as in the constructor or
min_node_size
if the value was too small.
|
noexcept |
- Effects:
- Returns the total amount of bytes remaining on the free list. Divide it by node_size() to get the number of nodes that can be allocated without growing the arena.
- Note
- Array allocations may lead to a growth even if the capacity_left left is big enough.
|
noexcept |
- Returns:
- The size of the next memory block after the free list gets empty and the arena grows. This function just forwards to the memory_arena.
- Note
- Due to fence memory, alignment buffers and the like this may not be the exact result capacity_left() will return, but it is an upper bound to it.
|
noexcept |
- Returns:
- A reference to the BlockAllocator used for managing the arena.
- Requires:
- It is undefined behavior to move this allocator out into another object.
Generated by
