13#ifndef ML_MEMORY_MANAGER_H
14#define ML_MEMORY_MANAGER_H
19#include "../mlMemoryManagerDllExport.h"
35#define ML_BYTE_TO_KB(x) ((x) >> 10)
37#define ML_BYTE_TO_MB(x) ((x) >> 20)
39#define ML_BYTE_TO_GB(x) ((x) >> 30)
41#define ML_KB_TO_BYTE(x) ((x) << 10)
43#define ML_MB_TO_BYTE(x) ((x) << 20)
45#define ML_GB_TO_BYTE(x) ((x) << 30)
53#ifndef DOXYGEN_SHOULD_SKIP_THIS
56class MLMemoryManagerPrivate;
137 void* deleteMemoryBlockCallbackUserData);
208 template <
typename T>
213 UserDataOwnershipGoesToMemoryBlock};
215 template <
typename T>
218 const void *userData);
225 template <
typename T>
230 void **oldUserData =
nullptr)
const;
244#ifndef DOXYGEN_SHOULD_SKIP_THIS
251 MLMemoryManagerPrivate* _private;
253 friend class MLMemoryBlock;
259extern template bool MLMEMORYMANAGER_EXPORT MLMemoryManager::registerDeletionCallbackUnsafe<MLWeakMemoryBlockHandle>(
262extern template bool MLMEMORYMANAGER_EXPORT MLMemoryManager::registerDeletionCallbackUnsafe<MLMemoryBlockHandle>(
266MLMemoryManager::deregisterDeletionCallbackUnsafe<MLWeakMemoryBlockHandle>(
268 const void *userData);
271MLMemoryManager::deregisterDeletionCallbackUnsafe<MLMemoryBlockHandle>(
275extern template void MLMEMORYMANAGER_EXPORT MLMemoryManager::replaceDeletionCallback<MLWeakMemoryBlockHandle>(
279extern template void MLMEMORYMANAGER_EXPORT MLMemoryManager::replaceDeletionCallback<MLMemoryBlockHandle>(
The strong handle of a MLMemoryBlock.
static MLMemoryManager * singleton()
The singleton is the only way to communicate with the memory manager.
void clearMemoryBlockHandles(MLMemoryBlockHandle **handles, size_t handleCount)
Clears a list of memory block handles.
void setErrorHandlingCallback(MLMemoryManagerErrorHandler errorHandler)
Sets the error handler callback.
unsigned int registerLibrary(const std::string &libraryName)
Registers a library and returns an id.
void clearMemoryBlockHandles(MLWeakMemoryBlockHandle **handles, size_t handleCount)
Clears a list of weak memory block handles.
static Ownership deregisterDeletionCallbackUnsafe(const T &handle, MLDeleteMemoryBlockCallback callback, const void *userData)
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.
static void deinitialize()
Deinitializes the memory manager.
boost::mutex & getCacheAccessMutex()
Advanced memory manager handling.
static void initialize()
Initializes the memory manager with a default limit of one gigabyte for the joint size of the cache a...
static bool registerDeletionCallbackUnsafe(const T &handle, MLDeleteMemoryBlockCallback callback, void *userData)
The weak handle of a MLMemoryBlock.
void setCacheAndLockedMemorySizeLimit(const size_t cacheSizeLimit)
The memory manager deletes memory blocks in the cache list automatically if the cache and locked memo...
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 clearCachedMemory(size_t numBytes)
Frees the given amount of memory by deleting cached memory blocks.
MLMemoryBlockHandle allocate(unsigned int id, size_t size)
Allocates a memory block with the given size in bytes.
void setStatisticsEnabled(bool enabled)
Sets if collecting statistic data is enabled.
size_t cacheAndLockedMemorySizeLimit() const
Returns the limit of the accumulated sizes in bytes of the cache and the locked memory.
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 cal...
void printStatistics(std::ostream &out)
Prints the current statistics.
void printCache(std::ostream &out) const
Prints some cache information to the ostream.
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 limi...
size_t cacheAndLockedMemorySize() const
Returns the sum of the cache and locked memory size in bytes.
void(* MLDeleteMemoryBlockCallback)(void *data, size_t size, void *userData)
The callback function type for deleting a memory block.
MLMemoryManagerErrorHandlingType
This enum is passed to the memory manager error handler callback to request a certain error handling.
@ NotifyUser
An error occurred and the user should be notified about it.
@ Abort
The error handling requests programm termination, because it is in an unstable state.
void(* MLMemoryManagerErrorHandler)(const std::string &message, const char *file, int line, MLMemoryManagerErrorHandlingType errorHandling)
Function callback to handle memory manager errors.
MLMEMORYMANAGER_EXPORT std::string mlByteToHumanReadable(size_t byte)
Converts the byte value into a string with human readable format.
Forward declaration for the boost::mutex class.