00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #ifndef ZenMemoryDebugH
00032 #define ZenMemoryDebugH
00033
00034
00035
00036 #if defined(ZENLIB_DEBUG)
00037
00038 #include "ZenLib/Conf.h"
00039 #include <fstream>
00040 #include <map>
00041 #include <stack>
00042 #include <string>
00043
00044
00045 namespace ZenLib
00046 {
00047
00048
00049
00050
00051
00052 class MemoryDebug
00053 {
00054 public :
00055 ~MemoryDebug();
00056 static MemoryDebug& Instance();
00057
00058 void* Allocate(std::size_t Size, const char* File, int Line, bool Array);
00059 void Free(void* Ptr, bool Array);
00060 void NextDelete(const char*, int Line);
00061
00062 private :
00063 MemoryDebug();
00064 void ReportLeaks();
00065 struct TBlock
00066 {
00067 std::size_t Size;
00068 std::string File;
00069 int Line;
00070 bool Array;
00071 };
00072 typedef std::map<void*, TBlock> TBlockMap;
00073
00074 TBlockMap m_Blocks;
00075 std::stack<TBlock> m_DeleteStack;
00076 };
00077
00078 }
00079
00080
00081
00082
00083
00084 inline void* operator new(std::size_t Size, const char* File, int Line)
00085 {
00086 return ZenLib::MemoryDebug::Instance().Allocate(Size, File, Line, false);
00087 }
00088 inline void* operator new[](std::size_t Size, const char* File, int Line)
00089 {
00090 return ZenLib::MemoryDebug::Instance().Allocate(Size, File, Line, true);
00091 }
00092
00093 inline void operator delete(void* Ptr)
00094 {
00095 ZenLib::MemoryDebug::Instance().Free(Ptr, false);
00096 }
00097
00098 inline void operator delete[](void* Ptr)
00099 {
00100 ZenLib::MemoryDebug::Instance().Free(Ptr, true);
00101 }
00102
00103 #if !defined(__BORLANDC__) // Borland does not support overloaded delete
00104 inline void operator delete(void* Ptr, const char* File, int Line)
00105 {
00106 ZenLib::MemoryDebug::Instance().NextDelete(File, Line);
00107 ZenLib::MemoryDebug::Instance().Free(Ptr, false);
00108 }
00109
00110 inline void operator delete[](void* Ptr, const char* File, int Line)
00111 {
00112 ZenLib::MemoryDebug::Instance().NextDelete(File, Line);
00113 ZenLib::MemoryDebug::Instance().Free(Ptr, true);
00114 }
00115 #endif
00116
00117 #if !defined(__MINGW32__) //TODO: Does not work on MinGW, don't know why
00118 #ifndef new
00119 #define new new(__FILE__, __LINE__)
00120 #endif
00121 #ifndef delete
00122 #define delete ZenLib::MemoryDebug::Instance().NextDelete(__FILE__, __LINE__), delete
00123 #endif
00124 #endif // __MINGW32__
00125
00126 #endif // defined(ZENLIB_DEBUG)
00127
00128 #endif // ZenMemoryDebugH
00129
00130