DefaultAllocator Class Reference

#include <defaultallocator.h>

Inheritance diagram for DefaultAllocator:

Static Public Member Functions

static Int ComputeArraySize (Int current_size, Int increment, Int min_chunk_size)
 
static void * Alloc (Int32 s)
 
static void * Alloc (Int64 s)
 
static void * AllocClear (Int32 s)
 
static void * AllocClear (Int64 s)
 
static void * Realloc (void *p, Int n)
 
template<class T >
static void Free (T *&p)
 

Detailed Description

Default implementation of an allocator. An allocator is used by arrays, lists and other data structurs to allocate and release memory. By default this implementation of an allocator is used.

There might be rare cases when you need a special memory alignment, a different resize strategy or have to use a special memory area (stack, shared memory, ...). This can be done by writing a custom allocator and specifying it as parameter upon array construction. A custom allocator must implement the ComputeArraySize(), Alloc(), AllocClear(), Realloc() and Free() methods, but it doesn't (and usually shouldn't) inherit from DefaultAllocator. The allocator methods don't have to be static if your allocator requires member variables, but the DefaultAllocator doesn't and therefore uses static methods for better performance.

Please note that an allocator is copied upon array construction - it would be a bad idea if your custom allocator object would consist of more than a few variables.

THREADSAFE

Member Function Documentation

◆ ComputeArraySize()

static Int ComputeArraySize ( Int  current_size,
Int  increment,
Int  min_chunk_size 
)
static

Computes the new size for a growing array.

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

◆ Alloc() [1/2]

static void* Alloc ( Int32  s)
static

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) THREADSAFE.
Returns
Memory block address or nullptr.

◆ Alloc() [2/2]

static void* Alloc ( Int64  s)
static

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) THREADSAFE.
Returns
Memory block address or nullptr.

◆ AllocClear() [1/2]

static void* AllocClear ( Int32  s)
static

Allocates a memory block and clears it.

Parameters
[in]sBlock size in bytes (values < 0 will return nullptr) THREADSAFE.
Returns
Memory block address or nullptr.

◆ AllocClear() [2/2]

static void* AllocClear ( Int64  s)
static

Allocates a memory block and clears it.

Parameters
[in]sBlock size in bytes (values < 0 will return nullptr) THREADSAFE.
Returns
Memory block address or nullptr.

◆ Realloc()

static void* Realloc ( void *  p,
Int  n 
)
static

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

Parameters
[in]pCurrent memory block (can be nullptr)
[in]nNew block size in bytes (values < 0 will return nullptr) THREADSAFE.
Returns
Memory block address or nullptr if resize is not possible (p is still valid in this case)

◆ Free()

static void Free ( T *&  p)
static

Frees a memory block.

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