ML Reference
mlMemoryManager.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2008, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13#ifndef ML_MEMORY_MANAGER_H
14#define ML_MEMORY_MANAGER_H
15
16#include <iosfwd>
17#include <string>
18
19#include "../mlMemoryManagerDllExport.h"
20
22#include "mlMemoryBlockHandle.h"
23
24// Included for convenience, i.e., a user needs only to
25// include this header file.
29
30
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) // End of group Macros
47
50MLMEMORYMANAGER_EXPORT std::string mlByteToHumanReadable(size_t byte);
51
52
53#ifndef DOXYGEN_SHOULD_SKIP_THIS
54
56class MLMemoryManagerPrivate;
57
58#endif // DOXYGEN_SHOULD_SKIP_THIS
59
61namespace boost
62{
63 class mutex;
64}
65
66
78
79
81typedef void (*MLMemoryManagerErrorHandler)(const std::string& message, const char* file, int line,
83
84
86class MLMEMORYMANAGER_EXPORT MLMemoryManager
87{
88public:
95 static void initialize();
96
102 static void deinitialize();
103
107
109
113
121 MLMemoryBlockHandle allocate(unsigned int id, size_t size);
122
134 void* data,
135 size_t size,
136 MLDeleteMemoryBlockCallback deleteMemoryBlockCallback,
137 void* deleteMemoryBlockCallbackUserData);
138
143 void setCacheAndLockedMemorySizeLimit(const size_t cacheSizeLimit);
144
147 size_t clearCachedMemory(size_t numBytes);
148
152
157
161
164 size_t cacheSize() const;
165
168 size_t lockedMemorySize() const;
169
173
176 void printCache(std::ostream& out) const;
177
180 void setStatisticsEnabled(bool enabled);
181
185 void printStatistics(std::ostream& out);
186 // End of group ThreadSafe
188
192 unsigned int registerLibrary(const std::string& libraryName);
193
197
201 boost::mutex& getCacheAccessMutex();
202
203 /*
204 * Returns \c true if registration was successful.
205 * Returns \c false if memoryBlock is empty or registration fails, as deletion is already
206 * ongoing.
207 */
208 template <typename T>
209 static bool registerDeletionCallbackUnsafe(const T &handle, MLDeleteMemoryBlockCallback callback,
210 void *userData);
211
212 enum class Ownership{UserDataOwnershipStaysWithCaller,
213 UserDataOwnershipGoesToMemoryBlock};
214
215 template <typename T>
218 const void *userData);
219
220
225 template <typename T>
226 void replaceDeletionCallback(const T &handle,
227 MLDeleteMemoryBlockCallback newCallback,
228 void *newUserData,
229 MLDeleteMemoryBlockCallback *oldCallback = nullptr,
230 void **oldUserData = nullptr) const;
231
233
236 void clearMemoryBlockHandles(MLMemoryBlockHandle** handles, size_t handleCount);
237
240 void clearMemoryBlockHandles(MLWeakMemoryBlockHandle** handles, size_t handleCount);
241
242
243private:
244#ifndef DOXYGEN_SHOULD_SKIP_THIS
245 MLMemoryManager(size_t cacheSizeLimit);
247 MLMemoryManager& operator=(const MLMemoryManager&);
248
249
250private:
251 MLMemoryManagerPrivate* _private;
252
253 friend class MLMemoryBlock;
254 friend class MLMemoryBlockHandle;
255 friend class MLWeakMemoryBlockHandle;
256#endif
257};
258
259extern template bool MLMEMORYMANAGER_EXPORT MLMemoryManager::registerDeletionCallbackUnsafe<MLWeakMemoryBlockHandle>(
260 const MLWeakMemoryBlockHandle &handle, MLDeleteMemoryBlockCallback callback, void *userData);
261
262extern template bool MLMEMORYMANAGER_EXPORT MLMemoryManager::registerDeletionCallbackUnsafe<MLMemoryBlockHandle>(
263 const MLMemoryBlockHandle &handle, MLDeleteMemoryBlockCallback callback, void *userData);
264
265extern template MLMemoryManager::Ownership MLMEMORYMANAGER_EXPORT
268 const void *userData);
269
270extern template MLMemoryManager::Ownership MLMEMORYMANAGER_EXPORT
272 const MLMemoryBlockHandle &handle, MLDeleteMemoryBlockCallback callback, const void *userData);
273
274
275extern template void MLMEMORYMANAGER_EXPORT MLMemoryManager::replaceDeletionCallback<MLWeakMemoryBlockHandle>(
276 const MLWeakMemoryBlockHandle &handle, MLDeleteMemoryBlockCallback newCallback,
277 void *newUserData, MLDeleteMemoryBlockCallback *oldCallback, void **oldUserData) const;
278
279extern template void MLMEMORYMANAGER_EXPORT MLMemoryManager::replaceDeletionCallback<MLMemoryBlockHandle>(
280 const MLMemoryBlockHandle &handle, MLDeleteMemoryBlockCallback newCallback, void *newUserData,
281 MLDeleteMemoryBlockCallback *oldCallback, void **oldUserData) const;
282
283
284#endif // MLMEMORYMANAGER_H_INCLUDED
The memory manager.
static MLMemoryManager * singleton()
void clearMemoryBlockHandles(MLMemoryBlockHandle **handles, size_t handleCount)
void setErrorHandlingCallback(MLMemoryManagerErrorHandler errorHandler)
unsigned int registerLibrary(const std::string &libraryName)
void clearMemoryBlockHandles(MLWeakMemoryBlockHandle **handles, size_t handleCount)
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
static void deinitialize()
boost::mutex & getCacheAccessMutex()
static void initialize()
static bool registerDeletionCallbackUnsafe(const T &handle, MLDeleteMemoryBlockCallback callback, void *userData)
void setCacheAndLockedMemorySizeLimit(const size_t cacheSizeLimit)
size_t cacheSize() const
size_t lockedMemorySize() const
size_t clearCachedMemory(size_t numBytes)
MLMemoryBlockHandle allocate(unsigned int id, size_t size)
void setStatisticsEnabled(bool enabled)
size_t cacheAndLockedMemorySizeLimit() const
MLMemoryBlockHandle addAllocatedMemory(unsigned int id, void *data, size_t size, MLDeleteMemoryBlockCallback deleteMemoryBlockCallback, void *deleteMemoryBlockCallbackUserData)
void printStatistics(std::ostream &out)
void printCache(std::ostream &out) const
void clearCacheToLimit()
size_t cacheAndLockedMemorySize() const
void(* MLDeleteMemoryBlockCallback)(void *data, size_t size, void *userData)
MLMemoryManagerErrorHandlingType
@ NotifyUser
An error occurred and the user should be notified about it.
@ Abort
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)
Forward declaration for the boost::mutex class.