PoolResource

A memory resource similar to std::pmr::unsynchronized_pool_resource, but optimized for node-based containers. It has the following properties:

Synopsis

Declared in <support/allocators/pool.h>

template<
    std::size_t MAX_BLOCK_SIZE_BYTES,
    std::size_t ALIGN_BYTES>
class PoolResource final

Description

* Owns the allocated memory and frees it on destruction, even when deallocate has not been called on the allocated blocks.

* Consists of a number of pools, each one for a different block size. Each pool holds blocks of uniform size in a freelist.

* Exhausting memory in a freelist causes a new allocation of a fixed size chunk. This chunk is used to carve out blocks.

* Block sizes or alignments that can not be served by the pools are allocated and deallocated by operator new().

PoolResource is not thread-safe. It is intended to be used by PoolAllocator.

An example: If you create a PoolResource<128, 8>(262144) and perform a bunch of allocations and deallocate 2 blocks with size 8 bytes, and 3 blocks with size 16, the members will look like this:

m_free_lists m_allocated_chunks ┌───┐ ┌───┐ ┌────────────-------──────┐ │ │ blocks │ ├─►│ 262144 B │ │ │ ┌─────┐ ┌─────┐ └─┬─┘ └────────────-------──────┘ │ 1 ├─►│ 8 B ├─►│ 8 B │ │ │ │ └─────┘ └─────┘ : │ │ │ │ │ ┌─────┐ ┌─────┐ ┌─────┐ ▼ │ 2 ├─►│16 B ├─►│16 B ├─►│16 B │ ┌───┐ ┌─────────────────────────┐ │ │ └─────┘ └─────┘ └─────┘ │ ├─►│ ▲ │ ▲ │ │ └───┘ └──────────┬──────────────┘ │ │ . │ │ m_available_memory_end │ . │ m_available_memory_it │ . │ │ │ │ │ │16 │ └───┘

Here m_free_lists[1]holds the 2 blocks of size 8 bytes, and m_free_lists[2] holds the 3 blocks of size 16. The blocks came from the data stored in the m_allocated_chunks list. Each chunk has bytes 262144. The last chunk has still some memory available for the blocks, and when m_available_memory_it is at the end, a new chunk will be allocated and added to the list.

Member Functions

NameDescription
PoolResource [constructor]Constructors
~PoolResource [destructor]Deallocates all memory allocated associated with the memory resource.
operator= Assignment operators
Allocate Allocates a block of bytes. If possible the freelist is used, otherwise allocation is forwarded to ::operator new().
ChunkSizeBytes Size in bytes to allocate per chunk, currently hardcoded to a fixed size.
Deallocate Returns a block to the freelists, or deletes the block when it did not come from the chunks.
NumAllocatedChunks Number of allocated chunks

Friends

NameDescription
PoolResourceTesterAccess to internals for testing purpose only

Template Parameters

NameDescription
MAX_BLOCK_SIZE_BYTESMaximum size to allocate with the pool. If larger sizes are requested, allocation falls back to new().
ALIGN_BYTESRequired alignment for the allocations.