ML Reference
MLMemoryManager Class Reference

The memory manager. More...

#include <mlMemoryManager.h>

Public Member Functions

 ~MLMemoryManager ()
 
unsigned int registerLibrary (const std::string &libraryName)
 Registers a library and returns an id.
 
void setErrorHandlingCallback (MLMemoryManagerErrorHandler errorHandler)
 Sets the error handler callback.
 
void clearMemoryBlockHandles (MLMemoryBlockHandle **handles, size_t handleCount)
 Clears a list of memory block handles.
 
void clearMemoryBlockHandles (MLWeakMemoryBlockHandle **handles, size_t handleCount)
 Clears a list of weak memory block handles.
 
MLMemoryBlockHandle allocate (unsigned int id, size_t size)
 Allocates a memory block with the given size in bytes.
 
MLMemoryBlockHandle addAllocatedMemory (unsigned int id, void *data, size_t size, MLDeleteMemoryBlockCallback deleteMemoryBlockCallback, void *deleteMemoryBlockCallbackUserData)
 Adds a pre-allocated memory block of the given size in bytes, which is later deleted by the given callback.
 
void setCacheAndLockedMemorySizeLimit (const size_t cacheSizeLimit)
 The memory manager deletes memory blocks in the cache list automatically if the cache and locked memory size limit is reached or exceeded.
 
size_t clearCachedMemory (size_t numBytes)
 Frees the given amount of memory by deleting cached memory blocks.
 
void clearCache ()
 Deletes all cached memory blocks.
 
void clearCacheToLimit ()
 Deletes cached memory blocks list until the cache and locked memory size is not greater than the limit.
 
size_t cacheAndLockedMemorySizeLimit () const
 Returns the limit of the accumulated sizes in bytes of the cache and the locked memory.
 
size_t cacheSize () const
 Returns the current size in bytes of the cache list.
 
size_t lockedMemorySize () const
 Returns the current size in bytes of the locked memory.
 
size_t cacheAndLockedMemorySize () const
 Returns the sum of the cache and locked memory size in bytes.
 
void printCache (std::ostream &out) const
 Prints some cache information to the ostream.
 
void setStatisticsEnabled (bool enabled)
 Sets if collecting statistic data is enabled.
 
void printStatistics (std::ostream &out)
 Prints the current statistics.
 

Static Public Member Functions

static void initialize ()
 Initializes the memory manager with a default limit of one gigabyte for the joint size of the cache and the locked memory.
 
static void deinitialize ()
 Deinitializes the memory manager.
 
static MLMemoryManagersingleton ()
 The singleton is the only way to communicate with the memory manager.
 
enum class  Ownership { UserDataOwnershipStaysWithCaller , UserDataOwnershipGoesToMemoryBlock }
 
template<typename T >
static bool registerDeletionCallbackUnsafe (const T &handle, MLDeleteMemoryBlockCallback callback, void *userData)
 
template<typename T >
static Ownership deregisterDeletionCallbackUnsafe (const T &handle, MLDeleteMemoryBlockCallback callback, const void *userData)
 
boost::mutex & getCacheAccessMutex ()
 Advanced memory manager handling.
 
template<typename T >
void replaceDeletionCallback (const T &handle, MLDeleteMemoryBlockCallback newCallback, void *newUserData, MLDeleteMemoryBlockCallback *oldCallback=nullptr, void **oldUserData=nullptr) const
 Replaces the deletion callback and user data that are registered for the memory block.
 

Detailed Description

The memory manager.

Definition at line 86 of file mlMemoryManager.h.

Member Enumeration Documentation

◆ Ownership

enum class MLMemoryManager::Ownership
strong
Enumerator
UserDataOwnershipStaysWithCaller 
UserDataOwnershipGoesToMemoryBlock 

Definition at line 212 of file mlMemoryManager.h.

Constructor & Destructor Documentation

◆ ~MLMemoryManager()

MLMemoryManager::~MLMemoryManager ( )

Member Function Documentation

◆ clearMemoryBlockHandles() [1/2]

void MLMemoryManager::clearMemoryBlockHandles ( MLMemoryBlockHandle ** handles,
size_t handleCount )

Clears a list of memory block handles.

The cache access mutex will be locked only once. NULL pointers are allowed in handles, they will be ignored.

◆ clearMemoryBlockHandles() [2/2]

void MLMemoryManager::clearMemoryBlockHandles ( MLWeakMemoryBlockHandle ** handles,
size_t handleCount )

Clears a list of weak memory block handles.

The cache access mutex will be locked only once. NULL pointers are allowed in handles, they will be ignored.

◆ deinitialize()

static void MLMemoryManager::deinitialize ( )
static

Deinitializes the memory manager.

This function should be called at program termination, when no library accesses the memory manager anymore. This also means that no strong or weak handle may exist at this time, otherwise they would be destroyed after the memory manager and try to communicate with it, causing a crash. Calling deinitialize() multiple times is allowed.

◆ deregisterDeletionCallbackUnsafe()

template<typename T >
template MLMemoryManager::Ownership MLMEMORYMANAGER_EXPORT MLMemoryManager::deregisterDeletionCallbackUnsafe< MLMemoryBlockHandle > ( const T & handle,
MLDeleteMemoryBlockCallback callback,
const void * userData )
static

◆ getCacheAccessMutex()

boost::mutex & MLMemoryManager::getCacheAccessMutex ( )

Advanced memory manager handling.

Returns the cache access mutex. Make sure you know what you are doing when using this mutex!

◆ initialize()

static void MLMemoryManager::initialize ( )
static

Initializes the memory manager with a default limit of one gigabyte for the joint size of the cache and the locked memory.

Calling this function more than once is allowed and has no effect. Deinitialization can be done by calling deinitialize(), which is also automatically called when the memory manager library is unloaded. The default cache and locked memory size limit can be adjusted with setCacheAndLockedMemorySizeLimit.

◆ registerDeletionCallbackUnsafe()

template<typename T >
template bool MLMEMORYMANAGER_EXPORT MLMemoryManager::registerDeletionCallbackUnsafe< MLMemoryBlockHandle > ( const T & handle,
MLDeleteMemoryBlockCallback callback,
void * userData )
static

◆ registerLibrary()

unsigned int MLMemoryManager::registerLibrary ( const std::string & libraryName)

Registers a library and returns an id.

This id must be passed to allocate() and addAllocatedMemory() and is used for generating statistics about cache and locked memory usage of different libraries.

◆ replaceDeletionCallback()

template<typename T >
template void MLMEMORYMANAGER_EXPORT MLMemoryManager::replaceDeletionCallback< MLMemoryBlockHandle > ( const T & handle,
MLDeleteMemoryBlockCallback newCallback,
void * newUserData,
MLDeleteMemoryBlockCallback * oldCallback = nullptr,
void ** oldUserData = nullptr ) const

Replaces the deletion callback and user data that are registered for the memory block.

IMPORTANT: The cache access mutex must be locked before calling this function! handles that reference the same memory block.

Deprecated
use registerDeletionCallbackUnsafe/deregisterDeletionCallbackUnsafe instead

◆ setErrorHandlingCallback()

void MLMemoryManager::setErrorHandlingCallback ( MLMemoryManagerErrorHandler errorHandler)

Sets the error handler callback.

See also
MLMemoryManagerErrorHandler

◆ singleton()

static MLMemoryManager * MLMemoryManager::singleton ( )
static

The singleton is the only way to communicate with the memory manager.

The pointer may never be deleted, this is handled in the deinitialization.


The documentation for this class was generated from the following file: