MemoryAllocator

Type Aliases

void (

RED_CUSTOM_NEW

void(*

RED_CUSTOM_DELETE

void (

RED_CUSTOM_REALLOC

Functions

MemoryAllocator &

Get

void

SetMemoryTracking

MemoryLeakTracker *

GetMemoryLeakTracker

RED_CUSTOM_NEW

SetCustomNew

RED_CUSTOM_NEW

SetCustomNewArray

RED_CUSTOM_DELETE

SetCustomDelete

RED_CUSTOM_DELETE

SetCustomDeleteArray

RED_CUSTOM_REALLOC

SetCustomRealloc

void *

Allocate

void *

Allocate

void *

Reallocate

void *

Reallocate

void

Free

Detailed Description

class MemoryAllocator

Memory allocation class.

@related Backbone, class RED::MemoryLeakTracker

This class is a singleton class (it can be instantiated once). It encapsulates memory management routines. Every instance of the RED engine will use this allocator to perform memory allocation and management. A SDK user can freely plug its memory allocation/deallocation callbacks through this class to gain control over the memory management.

The memory allocator is accessed using RED::MemoryAllocator::Get.

A memory leak tracking tool (RED::MemoryLeakTracker) can be accessed from the memory allocator’s instance (see the RED::MemoryAllocator::GetMemoryLeakTracker method).

Public Types

typedef void *(*RED_CUSTOM_NEW)(size_t iSize)

Custom memory allocator.

typedef void (*RED_CUSTOM_DELETE)(void *iMemory)

Custom memory de-allocator.

typedef void *(*RED_CUSTOM_REALLOC)(void *iMemory, size_t iSize)

Custom memory re-allocator.

Public Functions

void SetMemoryTracking(bool iTracking)

Enables or disables memory tracking.

Please note that the memory is only tracked after the memory tracking has started and stops being tracked after the memory tracking has been disabled.

If enabled at the application’s termination, the memory leak tracker will automatically dump all remaining allocations that are leaks.

Parameters

iTracking – On / Off.

inline RED::MemoryLeakTracker *GetMemoryLeakTracker() const

Gets the memory leak tracker.

Returns

The address of the RED::MemoryLeakTracker instance used to keep track of all engine allocations. The returned address is NULL if the memory leak tracker has not been enabled.

RED_CUSTOM_NEW SetCustomNew(RED_CUSTOM_NEW iCustomNew)

Sets a custom new operator.

The custom new operator will be used for all the RED::Object or inherited creations. Call this method passing NULL to reset to the default system new operator.

Parameters

iCustomNew – A pointer to the custom new operator.

Returns

A pointer to the old new operator.

RED_CUSTOM_NEW SetCustomNewArray(RED_CUSTOM_NEW iCustomNewArray)

Sets a custom new array operator.

The custom new array operator will be used for all the RED::Object or inherited creations. Call this method passing NULL to reset to the default system new array operator.

Parameters

iCustomNewArray – A pointer to the custom new array operator.

Returns

A pointer to the old new array operator.

RED_CUSTOM_DELETE SetCustomDelete(RED_CUSTOM_DELETE iCustomDelete)

Sets a custom delete operator.

The custom delete operator will be used for all the RED::Object or inherited destructions. Call this method passing NULL to reset to the default system delete operator.

Parameters

iCustomDelete – A pointer to the custom delete operator.

Returns

A pointer to the old delete operator.

RED_CUSTOM_DELETE SetCustomDeleteArray(RED_CUSTOM_DELETE iCustomDeleteArray)

Sets a custom delete array operator.

The custom delete array operator will be used for all the RED::Object or inherited destructions. Call this method passing NULL to reset to the default system delete array operator.

Parameters

iCustomDeleteArray – A pointer to the custom delete array operator.

Returns

A pointer to the old delete array operator.

RED_CUSTOM_REALLOC SetCustomRealloc(RED_CUSTOM_REALLOC iCustomRealloc)

Sets a custom realloc operator.

The custom realloc operator will be called in replacement of each call to rrealloc or by RED::MemoryAllocator::Reallocate(). Call this method passing NULL to reset to the default system realloc operator.

Parameters

iCustomRealloc – A pointer to the custom realloc operator.

Returns

A pointer to the old realloc operator.

void *Allocate(size_t iSize, int iCaller)

Memory allocation method.

Parameters
  • iSize – Byte size of the allocation.

  • iCaller – Identifier of the allocation caller.

Returns

The allocated address. NULL in case of error.

void *Allocate(size_t iSize)

Memory allocation method.

This allocation method does not provide any caller information.

Parameters

iSize – Byte size of the allocation.

Returns

The allocated address. NULL in case of error.

void *Reallocate(void *iSource, size_t iSize, int iCaller)

Memory re-allocation method.

Note that this method do not go through overloaded new / delete operators. This method will release the ‘iSource’ memory upon a failure to reallocate iSource with ‘iSize’ bytes. In this case, the method returns NULL. Note that the standard C++ realloc behavior does not release the source memory segment on failure to extend it, unlike what this method does.

Parameters
  • iSource – Address whose memory size is to be modified.

  • iSize – Byte size of the allocation.

  • iCaller – Identifier of the allocation caller.

Returns

The allocated address. NULL in case of error.

void *Reallocate(void *iSource, size_t iSize)

Memory re-allocation method.

Note that this method do not go through overloaded new / delete operators. This method will release the ‘iSource’ memory upon a failure to reallocate iSource with ‘iSize’ bytes. In this case, the method returns NULL. Note that the standard C++ realloc behavior does not release the source memory segment on failure to extend it, unlike what this method does.

Parameters
  • iSource – Address whose memory size is to be modified.

  • iSize – Byte size of the allocation.

Returns

The allocated address. NULL in case of error.

void Free(void *iAddress)

Releases a memory chunk.

Parameters

iAddress – Released memory address.

Public Static Functions

static MemoryAllocator &Get()

Returns the unique instance of the MemoryAllocator class.

Returns

A const reference to the unique instance of the MemoryAllocator.