Memory Allocation

内容表

关于

The MAXON API provides tools to safely allocate new memory. Typically it is not needed to allocate raw memory, structures like maxon::BaseArray should be used instead (see BaseArray Manual ). Memory is allocated using different allocators.

For object creation see Entity Creation .

Allocation

These macros allocate raw memory using the maxon::DefaultAllocator :

To safely handle memory one can use references, see maxon::AutoMem in 参考 .

Alternatively one can use the finally macro to ensure the release of memory, see Finally .

// This example allocates raw memory and stores it with AutoMem.

// allocate memory maxon::AutoMem<maxon::Char> data = NewMemClear ( maxon::Char , count) iferr_return ;

// use memory for ( maxon::Int i = 0; i < count; ++i) data[i] = ( maxon::Char )i;

// when the scope is left, maxon::AutoMem will free the memory

Memory Utility

Further memory utility tools are:

// This example allocates new memory. The memory content is written, copied and compared.
maxon::Char * data = nullptr ; maxon::Char * copy = nullptr ; finally { // assure release of memory at the end maxon::DeleteMem (data); maxon::DeleteMem (copy); };

// allocate memory data = NewMemClear ( maxon::Char , count) iferr_return ; copy = NewMemClear ( maxon::Char , count) iferr_return ;

// use memory for ( maxon::Int i = 0; i < count; ++i) data[i] = ( maxon::Char )i;

// copy data maxon::MemCopy (copy, data, count);

// compare const maxon::Int res = maxon::CompareMem (data, copy, count); if (res == 0) DiagnosticOutput ( "Memory blocks are identical." );

// clear memory maxon::SecureClearMem (data, count);

Allocators

An allocator allocates and releases memory. Typically the maxon::DefaultAllocator is used. In rare cases when special memory alignment is needed, a custom allocator can be used.

If memory from the C standard lib is needed, use maxon::CStdLibAllocator .

// This example uses FixedBufferAllocator to define a BaseArray type which guarantees a certain amout of memory.

// define a BaseArray with guaranteed 1024 bytes from the stack using FixedBufferArray = maxon::BaseArray<maxon::Char, 16, maxon::BASEARRAYFLAGS::NONE, maxon::FixedBufferAllocator<maxon::Char, 1024, maxon::DefaultAllocator> >;

// create array FixedBufferArray testArray;

// check array size const maxon::Int size = ( maxon::Int ) SIZEOF (testArray); DiagnosticOutput ( "Size: @" , size);

// fill array // as long as the needed size is less than 1024, no memory calls are invoked for ( maxon::Int i = 0; i < 1024; ++i) { testArray.Append() iferr_return ; }

延伸阅读

NewMemClear
#define NewMemClear(T, cnt)
定义: defaultallocator.h:205
maxon::CompareMem
MAXON_ATTRIBUTE_FORCE_INLINE Int CompareMem(const void *block1, const void *block2, Int size)
定义: defaultallocator.h:368
iferr_return
#define iferr_return
定义: resultbase.h:1434
maxon::MemCopy
MAXON_ATTRIBUTE_FORCE_INLINE void MemCopy(void *dst, const void *src, Int size)
定义: defaultallocator.h:285
maxon::BaseArray
定义: basearray.h:366
DiagnosticOutput
#define DiagnosticOutput(formatString,...)
定义: debugdiagnostics.h:166
maxon::SecureClearMem
MAXON_ATTRIBUTE_FORCE_INLINE void SecureClearMem(volatile void *mem, Int size, UChar value=0)
定义: defaultallocator.h:353
SIZEOF
#define SIZEOF(x)
Calculates the size of a datatype or element.
定义: apibasemath.h:205
maxon::Int
Int64 Int
signed 32/64 bit int, size depends on the platform
定义: apibase.h:184
maxon::DeleteMem
void DeleteMem(T *&p)
定义: defaultallocator.h:258
maxon::Char
char Char
signed 8 bit character
定义: apibase.h:180
maxon::BaseRef
定义: apibase.h:1522

Copyright  © 2014-2025 乐数软件    

工业和信息化部: 粤ICP备14079481号-1