ArrayAllocator< SIZE, ARRAYSELECTOR, FLUSH, NO_FREE, SUPPORT_ITERATOR > Class Template Reference

#include <arrayallocator.h>

Detailed Description

template<Int SIZE, typename ARRAYSELECTOR, Bool FLUSH = false, Bool NO_FREE = false, Bool SUPPORT_ITERATOR = false>
class maxon::ArrayAllocator< SIZE, ARRAYSELECTOR, FLUSH, NO_FREE, SUPPORT_ITERATOR >

Array allocator which only allocates and frees entries of SIZE bytes. Depending on the type of array the allocation might relocate already existing items.

Template Parameters
SIZEThe size of an entry in bytes.
ARRAYSELECTORSelector for array type to be used as allocator.
FLUSHUse true if Flush() shall be called on the array instead of Reset() when all entries have been freed.
NO_FREEUse true if the Free() function won't be called (then all allocations have to be freed at once by DeleteAll()). This enables certain optimizations, for example a HashMap can iterate over the allocated entries in memory order because it's known that all entries are still in use.
SUPPORT_ITERATORUse true if the allocator shall support iterators over the entries in use (in memory order). This requires a tiny bit of memory overhead (1 additional Int per entry), and similar to NO_FREE it's useful in conjunction with HashMaps as it allows them to iterate over entries in memory order.

Classes

struct  FreeEntry
 

Public Types

using ValueType = ArrayAllocatorValueType< SIZE >
 
using ArrayType = typename ARRAYSELECTOR::template Type< ValueType >
 
using IsArrayAllocator = std::true_type
 
using Iterator = typename ArrayType::Iterator
 
using ConstIterator = typename ArrayType::ConstIterator
 

Public Member Functions

 ArrayAllocator ()
 
 ArrayAllocator (ArrayAllocator &&src)
 
Int ComputeArraySize (Int currentSize, Int increment, Int minChunkSize)
 
void * Alloc (Int64 s, MAXON_SOURCE_LOCATION_DECLARATION)
 
void * Realloc (void *, Int64, MAXON_SOURCE_LOCATION_DECLARATION)
 
template<typename T >
void Free (T *&p)
 
Bool IsCompatibleWithDefaultAllocator (void *p) const
 
ArrayType & GetArray ()
 
const ArrayType & GetArray () const
 
Bool HasFreedEntries () const
 
template<typename T >
Bool DeleteAll ()
 
Iterator Begin ()
 
ConstIterator Begin () const
 
Iterator End ()
 
ConstIterator End () const
 

Static Public Attributes

static constexpr Bool SUPPORTS_ITERATOR
 

Private Attributes

Int _usedEntryCnt
 
FreeEntry * _freeEntry
 
ArrayType _buffer
 

Member Typedef Documentation

◆ ValueType

◆ ArrayType

using ArrayType = typename ARRAYSELECTOR::template Type<ValueType>

◆ IsArrayAllocator

using IsArrayAllocator = std::true_type

◆ Iterator

using Iterator = typename ArrayType::Iterator

◆ ConstIterator

using ConstIterator = typename ArrayType::ConstIterator

Constructor & Destructor Documentation

◆ ArrayAllocator() [1/2]

Default Constructor.

◆ ArrayAllocator() [2/2]

ArrayAllocator ( ArrayAllocator< SIZE, ARRAYSELECTOR, FLUSH, NO_FREE, SUPPORT_ITERATOR > &&  src)

Move constructor.

Member Function Documentation

◆ ComputeArraySize()

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

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()

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()

void* Realloc ( void *  ,
Int64  ,
MAXON_SOURCE_LOCATION_DECLARATION   
)

Not supported for this allocator.

Returns
Nullptr.

◆ Free()

void Free ( T *&  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.

◆ GetArray() [1/2]

ArrayType& GetArray ( )

◆ GetArray() [2/2]

const ArrayType& GetArray ( ) const

◆ HasFreedEntries()

Bool HasFreedEntries ( ) const

◆ DeleteAll()

Bool DeleteAll ( )

◆ Begin() [1/2]

Iterator Begin ( )

◆ Begin() [2/2]

ConstIterator Begin ( ) const

◆ End() [1/2]

Iterator End ( )

◆ End() [2/2]

ConstIterator End ( ) const

Member Data Documentation

◆ SUPPORTS_ITERATOR

constexpr Bool SUPPORTS_ITERATOR
staticconstexpr

This indicates if the ArrayAllocator always allows to iterate over all entries which are currently in use.

◆ _usedEntryCnt

Int _usedEntryCnt
private

◆ _freeEntry

FreeEntry* _freeEntry
private

◆ _buffer

ArrayType _buffer
private