PlayRho  1.1.0
An Interactive Real-Time-Oriented C++ Physics Engine & Library
playrho::BlockAllocator Class Reference

Block allocator. More...

#include <BlockAllocator.hpp>

Collaboration diagram for playrho::BlockAllocator:
[legend]

Classes

struct  Block
 Block. More...
 
struct  Chunk
 Chunk. More...
 

Public Types

using size_type = std::size_t
 Size type.
 

Public Member Functions

 BlockAllocator (const BlockAllocator &other)=delete
 
 BlockAllocator (BlockAllocator &&other)=delete
 
BlockAllocatoroperator= (const BlockAllocator &other)=delete
 
BlockAllocatoroperator= (BlockAllocator &&other)=delete
 
void * Allocate (size_type n)
 Allocates memory. More...
 
template<typename T >
T * AllocateArray (size_type n)
 Allocates an array. More...
 
void Free (void *p, size_type n)
 Frees memory. More...
 
void Clear ()
 Clears this allocator. More...
 
auto GetChunkCount () const noexcept
 Gets the chunk count.
 

Static Public Member Functions

static constexpr size_type GetMaxBlockSize () noexcept
 Max block size (before using external allocator).
 
static constexpr size_type GetChunkArrayIncrement () noexcept
 Chunk array increment.
 

Static Public Attributes

static constexpr std::size_t AllocatorBlockSizes []
 Allocator block sizes array data. More...
 
static constexpr auto ChunkSize = size_type{16 * 1024}
 Chunk size.
 

Private Attributes

size_type m_chunkCount = 0
 Chunk count.
 
size_type m_chunkSpace = GetChunkArrayIncrement()
 Chunk space.
 
Chunkm_chunks
 Chunks array.
 
Blockm_freeLists [size(AllocatorBlockSizes)]
 Free lists.
 

Detailed Description

Block allocator.

This is a small object allocator used for allocating small objects that persist for more than one time step.

Note
This data structure is 136-bytes large (on at least one 64-bit platform).
See also
http://www.codeproject.com/useritems/Small_Block_Allocator.asp

Member Function Documentation

◆ Allocate()

void * playrho::BlockAllocator::Allocate ( size_type  n)

Allocates memory.

Allocates uninitialized storage. Uses Alloc if the size is larger than GetMaxBlockSize(). Otherwise looks for an appropriately sized block from the free list. Failing that, Alloc is used to grow the free list from which memory is returned.

Returns
Non-null pointer if asked to make non-zero sized allocation, nullptr otherwise.
Exceptions
std::bad_allocIf unable to allocate non-zero size of memory.
See also
Alloc.

Referenced by AllocateArray().

◆ AllocateArray()

template<typename T >
T* playrho::BlockAllocator::AllocateArray ( size_type  n)
inline

Allocates an array.

Exceptions
std::bad_allocIf unable to allocate non-zero elements of non-zero size.

◆ Clear()

void playrho::BlockAllocator::Clear ( )

Clears this allocator.

Note
This resets the chunk-count back to zero.

◆ Free()

void playrho::BlockAllocator::Free ( void *  p,
size_type  n 
)

Frees memory.

This will use free if the size is larger than GetMaxBlockSize().

Referenced by playrho::Delete(), and playrho::BlockDeallocator::operator()().

Member Data Documentation

◆ AllocatorBlockSizes

constexpr std::size_t playrho::BlockAllocator::AllocatorBlockSizes[]
staticconstexpr
Initial value:
=
{
16, 32, 64, 96, 128, 160, 192, 224, 256, 320, 384, 448, 512, 640,
}

Allocator block sizes array data.

Referenced by Allocate(), Free(), and GetMaxBlockSize().


The documentation for this class was generated from the following files: