DefaultAllocator Class Reference

#include <defaultallocator.h>

Inheritance diagram for DefaultAllocator:

详细描述

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.

The DefaultAllocator guarantees that allocations of at least 64 bytes will have a 64 byte alignment (cache line). Smaller allocations of at least 32 bytes have an AVX-safe alignment. Everything smaller has at least an SSE-safe 16 byte alignment.

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.

静态公共成员函数

static Int   ComputeArraySize ( Int currentSize, Int increment, Int minChunkSize)
static MAXON_ATTRIBUTE_FORCE_INLINE void *  Alloc ( Int32 s, MAXON_SOURCE_LOCATION_DECLARATION )
static MAXON_ATTRIBUTE_FORCE_INLINE void *  Alloc ( Int64 s, MAXON_SOURCE_LOCATION_DECLARATION )
static MAXON_ATTRIBUTE_FORCE_INLINE void *  AllocClear ( Int32 s, MAXON_SOURCE_LOCATION_DECLARATION )
static MAXON_ATTRIBUTE_FORCE_INLINE void *  AllocClear ( Int64 s, MAXON_SOURCE_LOCATION_DECLARATION )
static MAXON_ATTRIBUTE_FORCE_INLINE void *  Realloc (void *p, Int32 n, MAXON_SOURCE_LOCATION_DECLARATION )
static MAXON_ATTRIBUTE_FORCE_INLINE void *  Realloc (void *p, Int64 n, MAXON_SOURCE_LOCATION_DECLARATION )
template<typename T >
static MAXON_ATTRIBUTE_FORCE_INLINE void  Free (T *&p)
static MAXON_ATTRIBUTE_FORCE_INLINE Bool   IsCompatibleWithDefaultAllocator (void *)

静态公共属性

static const UInt   ALIGNMENT
static const UInt   ALIGNMENT_MASK
static const UInt   MIN_ALIGNMENT_MASK

成员函数文档编制

◆  ComputeArraySize()

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

Computes the new capacity for a growing array. THREADSAFE.

参数
[in] currentSize Current number of elements.
[in] increment Number of elements to be added (>= 1)
[in] minChunkSize The minimum number of elements upon array creation.
返回
New capacity (maximum number of elements).

◆  Alloc() [1/2]

static MAXON_ATTRIBUTE_FORCE_INLINE void* Alloc ( Int32   s ,
MAXON_SOURCE_LOCATION_DECLARATION    
)
static

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

参数
[in] s Block size in bytes (values < 0 will return nullptr)
[in] allocLocation Pass MAXON_SOURCE_LOCATION(_NAME) to add the current source line and file.
返回
Memory block address or nullptr.

◆  Alloc() [2/2]

static MAXON_ATTRIBUTE_FORCE_INLINE void* Alloc ( Int64   s ,
MAXON_SOURCE_LOCATION_DECLARATION    
)
static

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

参数
[in] s Block size in bytes (values < 0 will return nullptr)
[in] allocLocation Pass MAXON_SOURCE_LOCATION(_NAME) to add the current source line and file.
返回
Memory block address or nullptr.

◆  AllocClear() [1/2]

static MAXON_ATTRIBUTE_FORCE_INLINE void* AllocClear ( Int32   s ,
MAXON_SOURCE_LOCATION_DECLARATION    
)
static

Allocates a memory block and clears it. THREADSAFE.

参数
[in] s Block size in bytes (values < 0 will return nullptr)
[in] allocLocation Pass MAXON_SOURCE_LOCATION(_NAME) to add the current source line and file.
返回
Memory block address or nullptr.

◆  AllocClear() [2/2]

static MAXON_ATTRIBUTE_FORCE_INLINE void* AllocClear ( Int64   s ,
MAXON_SOURCE_LOCATION_DECLARATION    
)
static

Allocates a memory block and clears it. THREADSAFE.

参数
[in] s Block size in bytes (values < 0 will return nullptr)
[in] allocLocation Pass MAXON_SOURCE_LOCATION(_NAME) to add the current source line and file.
返回
Memory block address or nullptr.

◆  Realloc() [1/2]

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

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.

参数
[in] p Current memory block (can be nullptr)
[in] n New block size in bytes (values < 0 will return nullptr)
[in] allocLocation Pass MAXON_SOURCE_LOCATION(_NAME) to add the current source line and file.
返回
Memory block address or nullptr if resize is not possible (p is still valid in this case)

◆  Realloc() [2/2]

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

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.

参数
[in] p Current memory block (can be nullptr)
[in] n New block size in bytes (values < 0 will return nullptr)
[in] allocLocation Pass MAXON_SOURCE_LOCATION(_NAME) to add the current source line and file.
返回
Memory block address or nullptr if resize is not possible (p is still valid in this case)

◆  Free()

static MAXON_ATTRIBUTE_FORCE_INLINE void Free ( T *&  p )
static

Frees a memory block. THREADSAFE.

参数
[in,out] p Memory block address (can be nullptr, will be nullptr after return)

◆  IsCompatibleWithDefaultAllocator()

static MAXON_ATTRIBUTE_FORCE_INLINE Bool IsCompatibleWithDefaultAllocator ( void *  )
static

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

返回
Always true.

Member Data Documentation

◆  ALIGNMENT

const UInt ALIGNMENT static

◆  ALIGNMENT_MASK

const UInt ALIGNMENT_MASK static

◆  MIN_ALIGNMENT_MASK

const UInt MIN_ALIGNMENT_MASK static