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