MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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)
46 // End of group Macros
47
51
52
53#ifndef DOXYGEN_SHOULD_SKIP_THIS
54
57
58#endif // DOXYGEN_SHOULD_SKIP_THIS
59
61namespace boost
62{
63 class mutex;
64}
65
66
77
78
80typedef void (*MLMemoryManagerErrorHandler)(const std::string& message, const char* file, int line,
82
83
86{
87public:
94 static void initialize();
95
101 static void deinitialize();
102
106
108
120 MLMemoryBlockHandle allocate(unsigned int id, size_t size);
121
133 MLMemoryBlockHandle addAllocatedMemory(unsigned int id, void* data, size_t size,
134 MLDeleteMemoryBlockCallback deleteMemoryBlockCallback,
136
142
145 size_t clearCachedMemory(size_t numBytes);
146
150
155
159
162 size_t cacheSize() const;
163
166 size_t lockedMemorySize() const;
167
171
174 void printCache(std::ostream& out) const;
175
178 void setStatisticsEnabled(bool enabled);
179
183 void printStatistics(std::ostream& out);
184
185 // End of group ThreadSafe
186
190 unsigned int registerLibrary(const std::string& libraryName);
191
195
199 boost::mutex& getCacheAccessMutex();
200
201 /*
202 * Returns \c true if registration was successful.
203 * Returns \c false if memoryBlock is empty or registration fails, as deletion is already
204 * ongoing.
205 */
206 template <typename T>
207 static bool registerDeletionCallbackUnsafe(const T& handle, MLDeleteMemoryBlockCallback callback, void* userData);
208
209 enum class Ownership {
210 UserDataOwnershipStaysWithCaller,
211 UserDataOwnershipGoesToMemoryBlock
212 };
213
214 template <typename T> static Ownership deregisterDeletionCallbackUnsafe(const T& handle,
216 const void* userData);
217
218
223 template <typename T> [[deprecated("use registerDeletionCallbackUnsafe/deregisterDeletionCallbackUnsafe instead")]]
225 MLDeleteMemoryBlockCallback* oldCallback = nullptr, void** oldUserData = nullptr) const;
226
228
232
236
237
238private:
239#ifndef DOXYGEN_SHOULD_SKIP_THIS
242 MLMemoryManager& operator=(const MLMemoryManager&);
243
244
245private:
246 MLMemoryManagerPrivate* _private;
247
248 friend class MLMemoryBlock;
249 friend class MLMemoryBlockHandle;
250 friend class MLWeakMemoryBlockHandle;
251#endif
252};
253
254extern template bool MLMEMORYMANAGER_EXPORT MLMemoryManager::registerDeletionCallbackUnsafe<MLWeakMemoryBlockHandle>(
255 const MLWeakMemoryBlockHandle& handle, MLDeleteMemoryBlockCallback callback, void* userData);
256
257extern template bool MLMEMORYMANAGER_EXPORT MLMemoryManager::registerDeletionCallbackUnsafe<MLMemoryBlockHandle>(
258 const MLMemoryBlockHandle& handle, MLDeleteMemoryBlockCallback callback, void* userData);
259
261MLMemoryManager::deregisterDeletionCallbackUnsafe<MLWeakMemoryBlockHandle>(const MLWeakMemoryBlockHandle& handle,
263 const void* userData);
264
266MLMemoryManager::deregisterDeletionCallbackUnsafe<MLMemoryBlockHandle>(const MLMemoryBlockHandle& handle,
268 const void* userData);
269
270
271extern template [[deprecated("use registerDeletionCallbackUnsafe/deregisterDeletionCallbackUnsafe instead")]]
272void MLMEMORYMANAGER_EXPORT MLMemoryManager::replaceDeletionCallback<MLWeakMemoryBlockHandle>(
275
276extern template [[deprecated("use registerDeletionCallbackUnsafe/deregisterDeletionCallbackUnsafe instead")]]
277void MLMEMORYMANAGER_EXPORT MLMemoryManager::replaceDeletionCallback<MLMemoryBlockHandle>(
280
281
282#endif // MLMEMORYMANAGER_H_INCLUDED
@ T
The strong handle of a MLMemoryBlock.
The memory manager.
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 whether 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 that is later deleted by the given callb...
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, as 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.
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
Forward declaration for the boost::mutex class.