Memory allocation
When allocating memory in the framework it is prefered (but not required) to use framework allocator functions instead of the standard new / delete operators or malloc / free.
- Use
B3DNewinstead of new andB3DDeleteinstead of delete. - Use
B3DNewMultipleinstead of new[] andB3DDeleteMultipleinstead of delete[]. - Use
B3DAllocateinstead of malloc andB3DFreeinstead of free.
This ensures the framework can keep track of all allocated memory, which ensures better debugging and profiling, as well as ensuring that internal memory allocation method can be changed in the future.
// Helper structure
struct MyStruct
{
MyStruct() {}
MyStruct(i32 integerValue, bool booleanValue)
:IntegerValue(integerValue), BooleanValue(booleanValue)
{ }
i32 IntegerValue;
bool BooleanValue;
};
// Allocating memory the normal way
MyStruct* structPointer = new MyStruct(123, false);
MyStruct** structPointerArray = new MyStruct[5];
void* rawMemory = malloc(12);
delete structPointer;
delete[] structPointerArray;
free(rawMemory);
// Allocating memory the framework way
MyStruct* bansheeStructPointer = B3DNew<MyStruct>(123, false);
MyStruct** bansheeStructPointerArray = B3DNewMultiple<MyStruct>(5);
void* bansheeRawMemory = B3DAllocate(12);
B3DDelete(bansheeStructPointer);
B3DDeleteMultiple(bansheeStructPointerArray, 5);
B3DFree(bansheeRawMemory);