ML Reference
MeVis/Foundation/Sources/MLMemoryManager/Include/mlMemoryManager.h
Go to the documentation of this file.
00001 // **InsertLicense** code
00002 //-----------------------------------------------------------------------------
00003 // The memory manager.
00004 //
00005 // \file    mlMemoryManager.h
00006 // \author  Marcus Barann
00007 // \date    03/2008
00008 //
00009 //-----------------------------------------------------------------------------
00010 
00011 #ifndef MLMEMORYMANAGER_H_INCLUDED
00012 #define MLMEMORYMANAGER_H_INCLUDED
00013 
00014 #include <iosfwd>
00015 #include <string>
00016 
00017 #include "../mlMemoryManagerDllExport.h"
00018 #include "mlDeleteMemoryBlockCallback.h"
00019 #include "mlMemoryBlockHandle.h"
00020 
00021 // Included for convenience, i.e. a user needs only to
00022 // include this header file.
00024 #include "mlWeakMemoryBlockHandle.h"
00026 
00027 
00031 
00032 #define ML_BYTE_TO_KB(x) ((x) >> 10)
00033 
00034 #define ML_BYTE_TO_MB(x) ((x) >> 20)
00035 
00036 #define ML_BYTE_TO_GB(x) ((x) >> 30)
00037 
00038 #define ML_KB_TO_BYTE(x) ((x) << 10)
00039 
00040 #define ML_MB_TO_BYTE(x) ((x) << 20)
00041 
00042 #define ML_GB_TO_BYTE(x) ((x) << 30)
00043  // End of group Macros
00044 
00047 MLMEMORYMANAGER_EXPORT std::string mlByteToHumanReadable(size_t byte);
00048 
00049 
00050 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00051 
00053 class MLMemoryManagerPrivate;
00054 
00055 #endif // DOXYGEN_SHOULD_SKIP_THIS
00056 
00058 namespace boost
00059 {
00060   class mutex;
00061 }
00062 
00063 
00066 enum MLMemoryManagerErrorHandlingType
00067 {
00069   NotifyUser,
00070 
00073   Abort
00074 };
00075 
00076 
00078 typedef void (*MLMemoryManagerErrorHandler)(const std::string& message, const char* file, int line,
00079                                             MLMemoryManagerErrorHandlingType errorHandling);
00080 
00081 
00083 class MLMEMORYMANAGER_EXPORT MLMemoryManager
00084 {
00085 public:
00092   static void initialize();
00093 
00099   static void deinitialize();
00100 
00103   static MLMemoryManager* singleton();
00104 
00105   ~MLMemoryManager();
00106 
00111 
00112 
00113 
00114 
00115 
00116 
00117 
00118   MLMemoryBlockHandle allocate(unsigned int id, size_t size);
00119 
00130   MLMemoryBlockHandle addAllocatedMemory(unsigned int                id,
00131                                          void*                       data,
00132                                          size_t                      size,
00133                                          MLDeleteMemoryBlockCallback deleteMemoryBlockCallback,
00134                                          void*                       deleteMemoryBlockCallbackUserData);
00135 
00138   void deleteMemoryBlockData(void* data);
00139 
00144   void setCacheAndLockedMemorySizeLimit(const size_t cacheSizeLimit);
00145 
00148   size_t clearCachedMemory(size_t numBytes);
00149 
00152   void clearCache();
00153 
00157   void clearCacheToLimit();
00158 
00161   size_t cacheAndLockedMemorySizeLimit() const;
00162 
00165   size_t cacheSize() const;
00166 
00169   size_t lockedMemorySize() const;
00170 
00173   size_t cacheAndLockedMemorySize() const;
00174 
00177   void printCache(std::ostream& out) const;
00178 
00181   void setStatisticsEnabled(bool enabled);
00182 
00186   void printStatistics(std::ostream& out);
00187  // End of group ThreadSafe
00189 
00193   unsigned int registerLibrary(const std::string& libraryName);
00194 
00197   void setErrorHandlingCallback(MLMemoryManagerErrorHandler errorHandler);
00198 
00200 
00201 
00202   boost::mutex& getCacheAccessMutex();
00203 
00207   void replaceDeletionCallback(const MLMemoryBlockHandle& handle,
00208                                MLDeleteMemoryBlockCallback  newCallback, void*  newUserData,
00209                                MLDeleteMemoryBlockCallback* oldCallback = NULL, void** oldUserData = NULL) const;
00210 
00213   void replaceDeletionCallback(const MLWeakMemoryBlockHandle& handle,
00214                                MLDeleteMemoryBlockCallback  newCallback, void*  newUserData,
00215                                MLDeleteMemoryBlockCallback* oldCallback = NULL, void** oldUserData = NULL) const;
00217 
00220   void clearMemoryBlockHandles(MLMemoryBlockHandle** handles, size_t handleCount);
00221 
00224   void clearMemoryBlockHandles(MLWeakMemoryBlockHandle** handles, size_t handleCount);
00225 
00226 
00227 private:
00228 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00229   MLMemoryManager(size_t cacheSizeLimit);
00230   MLMemoryManager(const MLMemoryManager&);
00231   MLMemoryManager& operator=(const MLMemoryManager&);
00232 
00233 
00234 private:
00235   MLMemoryManagerPrivate* _private;
00236 
00237   friend class MLMemoryBlock;
00238   friend class MLMemoryBlockHandle;
00239   friend class MLWeakMemoryBlockHandle;
00240 #endif
00241 };
00242 
00243 #endif // MLMEMORYMANAGER_H_INCLUDED