ML 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 MLMemoryManager * | singleton () |
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. | |
The memory manager.
Definition at line 86 of file mlMemoryManager.h.
|
strong |
Enumerator | |
---|---|
UserDataOwnershipStaysWithCaller | |
UserDataOwnershipGoesToMemoryBlock |
Definition at line 212 of file mlMemoryManager.h.
MLMemoryManager::~MLMemoryManager | ( | ) |
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.
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.
|
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.
|
static |
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!
|
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.
|
static |
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.
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.
void MLMemoryManager::setErrorHandlingCallback | ( | MLMemoryManagerErrorHandler | errorHandler | ) |
Sets the error handler callback.
|
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.