memory_pool< PoolType, BlockOrRawAllocator > Class Template Reference
Allocator implementations

## 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

## Static Functions

static constexpr std::size_t | min_block_size (std::size_t node_size, std::size_t number_of_nodes) noexcept

## 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 * | try_allocate_node () noexcept
void * | allocate_array (std::size_t n)
void * | try_allocate_array (std::size_t n) noexcept
void | deallocate_node (void *ptr) noexcept
bool | try_deallocate_node (void *ptr) noexcept
void | deallocate_array (void *ptr, std::size_t n) noexcept
bool | try_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
bool | owns (const void *ptr) const noexcept
| memory_pool (memory_pool &&other) noexcept
memory_pool & | operator= (memory_pool &&other) noexcept

## Constructors

## memory_pool() [1/2]

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>
template<typename... Args>

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 the `min_node_size`, the `min_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 and `block_size` must be at least `min_block_size(node_size, 1)`.

## ~memory_pool()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

~memory_pool | ( |  | ) |

noexcept

Effects:
  Destroys the memory_pool by returning all memory blocks, regardless of properly deallocated back to the BlockAllocator.

## memory_pool() [2/2]

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

memory_pool | ( | memory_pool< PoolType, BlockOrRawAllocator > && | other | ) |

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

## min_block_size()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

static constexpr std::size_t min_block_size | ( | std::size_t | node_size,
|  | std::size_t | number_of_nodes
| ) |  |

static constexpr noexcept

Returns:
  The minimum block size required for certain number of node.

Requires:
  `node_size` must be a valid node size and `number_of_nodes` must be a non-zero value.

Note
  MSVC's implementation of `std::list` for example is never empty and always allocates proxy nodes. To get enough memory for `N` elements of a list, `number_of_nodes` needs to include the proxy count in addition to `N`.

## operator=()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

memory_pool & operator= | ( | memory_pool< PoolType, BlockOrRawAllocator > && | other | ) |

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.

## allocate_node()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

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.

## try_allocate_node()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

void * try_allocate_node | ( |  | ) |

noexcept

Effects:
  Allocates a single node similar to allocate_node(). But if the free list is empty, a new block will not be allocated.

Returns:
  A suitable aligned node of size node_size() or `nullptr`.

## allocate_array()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

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 the `PoolType` 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.

## try_allocate_array()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

void * try_allocate_array | ( | std::size_t | n | ) |

noexcept

Effects:
  Allocates an array of nodes similar to allocate_array(). But it will never allocate a new memory block.

Returns:
  An array of `n` nodes of size node_size() suitable aligned or `nullptr`.

## deallocate_node()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

void deallocate_node | ( | void * | ptr | ) |

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.

## try_deallocate_node()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

bool try_deallocate_node | ( | void * | ptr | ) |

noexcept

Effects:
  Deallocates a single node but it does not be a result of a previous call to allocate_node().

Returns:
  `true` if the node could be deallocated, `false` otherwise.

Note
  Some free list implementations can deallocate any memory, doesn't matter where it is coming from.

## deallocate_array()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

void deallocate_array | ( | void * | ptr,
|  | std::size_t | n
| ) |  |

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 same `n` on the same free list, i.e. either this allocator object or a new object created by moving this to it.

## try_deallocate_array()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

bool try_deallocate_array | ( | void * | ptr,
|  | std::size_t | n
| ) |  |

noexcept

Effects:
  Deallocates an array but it does not be a result of a previous call to allocate_array().

Returns:
  `true` if the node could be deallocated, `false` otherwise.

Note
  Some free list implementations can deallocate any memory, doesn't matter where it is coming from.

## node_size()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

std::size_t node_size | ( |  | ) | const

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.

## capacity_left()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

std::size_t capacity_left | ( |  | ) | const

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.

## next_capacity()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

std::size_t next_capacity | ( |  | ) | const

noexcept

Returns:
  The size of the next memory block after the free list gets empty and the arena grows. capacity_left() will increase by this amount.

Note
  Due to fence memory in debug mode this cannot be just divided by the node_size() to get the number of nodes.

## get_allocator()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

allocator_type & get_allocator | ( |  | ) |

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.

## owns()

template<typename PoolType = node_pool, class BlockOrRawAllocator = default_allocator>

bool owns | ( | const void * | ptr | ) | const

noexcept

Returns:
  If `ptr` is in memory owned by the underlying arena.