FixedBufferAllocator< T, COUNT, PARENT_ALLOCATOR, SINGLE_ALLOCATION > Class Template Reference

#include <fixedbufferallocator.h>

Inheritance diagram for FixedBufferAllocator< T, COUNT, PARENT_ALLOCATOR, SINGLE_ALLOCATION >:

Detailed Description

template<typename T, Int COUNT, typename PARENT_ALLOCATOR, Bool SINGLE_ALLOCATION = false>
class maxon::FixedBufferAllocator< T, COUNT, PARENT_ALLOCATOR, SINGLE_ALLOCATION >

Fixed allocator. A fixed allocator contains 'SIZEOF(T) * COUNT' bytes memory on stack. The first memory request (and any Resize calls) that are smaller than this size will return this static memory. All other requests will be routed to the PARENT_ALLOCATOR. The fixed allocator is not thread-safe!

Here an example for the use of a fixed allocator:

BaseArray<Char, 16, BASEARRAYFLAGS::NONE, FixedBufferAllocator<Char, 1024, DefaultAllocator>> buf;

In the example a BaseArray will use the guaranteed 1024 bytes from the stack / fixed allocator first. Only when the BaseArray grows bigger than 1024 characters more memory from the DefaultAllocator will be fetched. This is a convenient way to allow routines be flexible without (length) limits, but avoiding constant unnecessary memory allocations.

Template Parameters
TA type for the size computation.
COUNTNumber of T-typed elements to reserve on the stack.
PARENT_ALLOCATORThe allocator that shall be used when a request cannot be satisfied using the static memory.
SINGLE_ALLOCATIONFalse by default. True is used by the BaseArray as optimization.

Public Member Functions

 FixedBufferAllocator (Bool used=false)
 
 FixedBufferAllocator (const PARENT_ALLOCATOR &parent)
 
void * Alloc (Int32 s, MAXON_SOURCE_LOCATION_DECLARATION)
 
void * Alloc (Int64 s, MAXON_SOURCE_LOCATION_DECLARATION)
 
void * Realloc (void *p, Int32 n, MAXON_SOURCE_LOCATION_DECLARATION)
 
void * Realloc (void *p, Int64 n, MAXON_SOURCE_LOCATION_DECLARATION)
 
template<typename PTRTYPE >
void Free (PTRTYPE *&p)
 
Bool IsCompatibleWithDefaultAllocator (void *p) const
 

Static Public Member Functions

static Int ComputeArraySize (Int currentSize, Int increment, Int minChunkSize)
 

Private Member Functions

 MAXON_DISALLOW_COPY_AND_ASSIGN (FixedBufferAllocator)
 

Private Attributes

FixedBufferMemory< SIZE, PARENT_ALLOCATOR, SINGLE_ALLOCATION==false > _memory
 

Static Private Attributes

static const Int SIZE
 

Constructor & Destructor Documentation

◆ FixedBufferAllocator() [1/2]

FixedBufferAllocator ( Bool  used = false)
explicit

Default constructor.

◆ FixedBufferAllocator() [2/2]

FixedBufferAllocator ( const PARENT_ALLOCATOR &  parent)
explicit

Copy constructor.

Member Function Documentation

◆ ComputeArraySize()

static Int ComputeArraySize ( Int  currentSize,
Int  increment,
Int  minChunkSize 
)
static

Computes the new size for a growing array THREADSAFE.

Parameters
[in]currentSizeCurrent number of elements.
[in]incrementNumber of elements to be added (>= 1)
[in]minChunkSizeThe minimum number of elements upon array creation.
Returns
New number of elements.

◆ Alloc() [1/2]

void* Alloc ( Int32  s,
MAXON_SOURCE_LOCATION_DECLARATION   
)

Allocates a memory block. The memory is not cleared, it may contain a certain byte pattern in debug mode.

Parameters
[in]sBlock size in bytes (values < 0 will return nullptr)
[in]allocLocationPass MAXON_SOURCE_LOCATION(_NAME) to add the current source line and file.
Returns
Memory block address or nullptr.

◆ Alloc() [2/2]

void* Alloc ( Int64  s,
MAXON_SOURCE_LOCATION_DECLARATION   
)

Allocates a memory block. The memory is not cleared, it may contain a certain byte pattern in debug mode.

Parameters
[in]sBlock size in bytes (values < 0 will return nullptr)
[in]allocLocationPass MAXON_SOURCE_LOCATION(_NAME) to add the current source line and file.
Returns
Memory block address or nullptr.

◆ Realloc() [1/2]

void* Realloc ( void *  p,
Int32  n,
MAXON_SOURCE_LOCATION_DECLARATION   
)

Resizes a memory block. The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved to a new location. The additional memory is not cleared, it may contain a certain byte pattern in debug mode. In case that p is a nullptr the function behaves like Alloc. If the function fails to allocate the requested block of memory a nullptr is returned and the memory block pointed to by argument p is not deallocated (it is still valid with its contents unchanged). THREADSAFE.

Parameters
[in]pCurrent memory block (can be nullptr)
[in]nNew block size in bytes (values < 0 will return nullptr)
[in]allocLocationPass MAXON_SOURCE_LOCATION(_NAME) to add the current source line and file.
Returns
Memory block address or nullptr if resize is not possible (p is still valid in this case)

◆ Realloc() [2/2]

void* Realloc ( void *  p,
Int64  n,
MAXON_SOURCE_LOCATION_DECLARATION   
)

Resizes a memory block. The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved to a new location. The additional memory is not cleared, it may contain a certain byte pattern in debug mode. In case that p is a nullptr the function behaves like Alloc. If the function fails to allocate the requested block of memory a nullptr is returned and the memory block pointed to by argument p is not deallocated (it is still valid with its contents unchanged). THREADSAFE.

Parameters
[in]pCurrent memory block (can be nullptr)
[in]nNew block size in bytes (values < 0 will return nullptr)
[in]allocLocationPass MAXON_SOURCE_LOCATION(_NAME) to add the current source line and file.
Returns
Memory block address or nullptr if resize is not possible (p is still valid in this case)

◆ Free()

void Free ( PTRTYPE *&  p)

Frees a memory block.

Parameters
[in,out]pMemory block address (can be nullptr, will be nullptr after return)

◆ IsCompatibleWithDefaultAllocator()

Bool IsCompatibleWithDefaultAllocator ( void *  p) const

Returns if a memory block allocated via this allocator can be reallocated or freed by the DefaultAllocator.

Parameters
[in]pMemory block address.
Returns
True if the memory block can be reallocated or freed by the DefaultAllocator.

◆ MAXON_DISALLOW_COPY_AND_ASSIGN()

MAXON_DISALLOW_COPY_AND_ASSIGN ( FixedBufferAllocator< T, COUNT, PARENT_ALLOCATOR, SINGLE_ALLOCATION >  )
private

Member Data Documentation

◆ SIZE

const Int SIZE
staticprivate

◆ _memory

FixedBufferMemory<SIZE, PARENT_ALLOCATOR, SINGLE_ALLOCATION == false> _memory
private