Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes

Burger::GlobalMemoryManager Class Reference

Singleton class for global memory allocation. More...

#include <brglobalmemorymanager.h>

Inheritance diagram for Burger::GlobalMemoryManager:
Inheritance graph
[legend]
Collaboration diagram for Burger::GlobalMemoryManager:
Collaboration graph
[legend]

List of all members.

Public Member Functions

BURGER_INLINE MemoryManagerGetInstance (void) const
 Return the instance of the active global memory manager.

Static Public Member Functions

static void BURGER_API Init (MemoryManager *pInstance)
 Initialize the global memory allocator.
static void BURGER_API Shutdown (void)
 Shut down the global memory allocator.
static void *BURGER_API Alloc (WordPtr uSize)
 Allocate memory.
static void BURGER_API Free (const void *pInput)
 Release memory.
static void *BURGER_API Realloc (const void *pInput, WordPtr uSize)
 Reallocate previously allocated memory.
static void *BURGER_API AllocClear (WordPtr uSize)
 Allocate a block of pre-zeroed memory.

Private Attributes

MemoryManagerm_pInstance
 Pointer to the original derived class.

Static Private Attributes

static GlobalMemoryManager g_MemoryManager
 Global singleton data for the GlobalMemoryManager class.

Detailed Description

Singleton class for global memory allocation.

To implement a workable copy of malloc(), free() etc., a method was needed to contain the instance pointer of the burgerlib memory manager and retain the ability to change the implementation at will. This was needed since Burgerlib Memory Managers are all class based so a "this" pointer is needed in all calls whereas the ANSI calling convention assumes a global implementation.

By using this class, which is initialized either with Burger::MemoryManagerANSI or Burger::MemoryManagerFast all functions that allocate memory from the global pool can and will be redirected.

Mostly used to make search and replace of malloc and new trivial. Use of this class is not encouraged. It's only for quick porting of legacy code that isn't multi-thread friendly.

Note:
This class is assumed to have functions that are thread safe. Any custom implentations on target platforms that support multi-threading must all be multi-core thread safe.
Since this a reference to the controlled class, NEVER manually upcase the global instance of this class. Use the MemoryManager *BurgerGlobalMemoryManager::GetInstance(void) const function to get the pointer to the real class if upcasting is desired for system specific functionality is desired.

Member Function Documentation

void *BURGER_API Burger::GlobalMemoryManager::Alloc ( WordPtr  uSize  )  [static]

Allocate memory.

Using the redirection values, allocate the memory requested.

Parameters:
uSize Number of bytes requested to allocate
Returns:
NULL if no bytes are requested or of an out of memory condition exists.

Reimplemented from Burger::MemoryManager.

static void* BURGER_API Burger::GlobalMemoryManager::AllocClear ( WordPtr  uSize  )  [static]

Allocate a block of pre-zeroed memory.

Allocate a block of memory and return either NULL in the case of an out of memory condition or if the amount requested was zero, otherwise return a valid pointer of memory that has been preset to zero.

Parameters:
uSize Number of bytes to allocate.
Returns:
NULL on error or a valid pointer to allocated memory.

Reimplemented from Burger::MemoryManager.

void BURGER_API Burger::GlobalMemoryManager::Free ( const void *  pInput  )  [static]

Release memory.

Using the redirection values, release the memory. NULL performs no operation.

Parameters:
pInput Pointer to memory to release.

Reimplemented from Burger::MemoryManager.

MemoryManager * Burger::GlobalMemoryManager::GetInstance ( void   )  const [inline]

Return the instance of the active global memory manager.

Returns:
Pointer to the current global memory manager, NULL if none is active.
void BURGER_API Burger::GlobalMemoryManager::Init ( MemoryManager pInstance  )  [static]

Initialize the global memory allocator.

This function copies the pointer to the MemoryManager class and will use this instance for all memory operations.

Parameters:
pInstance Pointer to an initialized MemoryManager.
Note:
The MemoryManager is not copied, so the class should not go out of scope without a call to either shutdown this class or reset to another implementation
void *BURGER_API Burger::GlobalMemoryManager::Realloc ( const void *  pInput,
WordPtr  uSize 
) [static]

Reallocate previously allocated memory.

Using the redirection values, reallocate the memory.

Parameters:
pInput Pointer to memory to reallocate. NULL assumes no memory to copy from.
uSize New size of the buffer.
Returns:
Pointer to the memory allocated with the data copied from the previous pointer. NULL if out of memory or no memory requested.

Reimplemented from Burger::MemoryManager.

void BURGER_API Burger::GlobalMemoryManager::Shutdown ( void   )  [static]

Shut down the global memory allocator.

Shut down the memory allocator through the m_pShutdown pointer.

Note:
This call will zero out all entries in this class, so all future use of this class except for a call to void Burger::GlobalMemoryManager::Init(MemoryManager *pInstance) will result in an assert or page fault.

Reimplemented from Burger::MemoryManager.


Member Data Documentation

Global singleton data for the GlobalMemoryManager class.

Pointer to the original derived class.