memory
Typedefs | Static Functions | Member Functions
growing_block_allocator< RawAllocator, Num, Den > Class Template Reference

Detailed Description

template<class RawAllocator = default_allocator, unsigned Num = 2, unsigned Den = 1>
class foonathan::memory::growing_block_allocator< RawAllocator, Num, Den >

A BlockAllocator that uses a given RawAllocator for allocating the blocks.

It calls the allocate_array() function with a node of size 1 and maximum alignment on the used allocator for the block allocation. The size of the next memory block will grow by a given factor after each allocation, allowing an amortized constant allocation time in the higher level allocator. The factor can be given as rational in the template parameter, default is 2.

Typedefs

using allocator_type = typename traits::allocator_type
 

Static Functions

static float growth_factor () noexcept
 
static std::size_t grow_block_size (std::size_t block_size) noexcept
 

Member Functions

 growing_block_allocator (std::size_t block_size, allocator_type alloc=allocator_type()) noexcept
 
memory_block allocate_block ()
 
void deallocate_block (memory_block block) noexcept
 
std::size_t next_block_size () const noexcept
 
allocator_type & get_allocator () noexcept
 

Constructors

◆ growing_block_allocator()

growing_block_allocator ( std::size_t  block_size,
allocator_type  alloc = allocator_type() 
)
explicitnoexcept
Effects:
Creates it by giving it the initial block size, the allocator object and the growth factor. By default, it uses a default-constructed allocator object and a growth factor of 2.
Requires:
block_size must be greater than 0.

Member Functions

◆ allocate_block()

memory_block allocate_block ( )
Effects:
Allocates a new memory block and increases the block size for the next allocation.
Returns:
The new memory_block.
Throws:
Anything thrown by the allocate_array() function of the RawAllocator.

◆ deallocate_block()

void deallocate_block ( memory_block  block)
noexcept
Effects:
Deallocates a previously allocated memory block. This does not decrease the block size.
Requires:
block must be previously returned by a call to allocate_block().

◆ next_block_size()

std::size_t next_block_size ( ) const
noexcept
Returns:
The size of the memory block returned by the next call to allocate_block().

◆ get_allocator()

allocator_type & get_allocator ( )
noexcept
Returns:
A reference to the used RawAllocator object.

◆ growth_factor()

static float growth_factor ( )
staticnoexcept
Returns:
The growth factor.