ZenLib

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

MemoryDebug.h
Go to the documentation of this file.
1 /* Copyright (c) MediaArea.net SARL. All Rights Reserved.
2 *
3 * Use of this source code is governed by a zlib-style license that can
4 * be found in the License.txt file in the root of the source tree.
5 */
6
7 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
8 //
9 // MemoryDebug
10 //
11 // Provide "new" and "delete" overloadings to be able to detect memory leaks
12 // Based on http://loulou.developpez.com/tutoriels/moteur3d/partie1/ 2.2.1
13 //
14 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
15
16 //---------------------------------------------------------------------------
17 #ifndef ZenMemoryDebugH
18 #define ZenMemoryDebugH
19 //---------------------------------------------------------------------------
20
21 //---------------------------------------------------------------------------
22 #if defined(ZENLIB_DEBUG)
23 //---------------------------------------------------------------------------
24 #include "ZenLib/Conf.h"
25 #include <fstream>
26 #include <map>
27 #include <stack>
28 #include <string>
29 //---------------------------------------------------------------------------
30
31 namespace ZenLib
32 {
33
34 //***************************************************************************
35 // Class
36 //***************************************************************************
37
38 class MemoryDebug
39 {
40 public :
41 ~MemoryDebug();
42 static MemoryDebug& Instance();
43 static bool g_IsShutdown;
44 void* Allocate(std::size_t Size, const char* File, int Line, bool Array);
45 void Free(void* Ptr, bool Array);
46 void NextDelete(const char*, int Line); //Sauvegarde les infos sur la désallocation courante
47
48 void ReportLeaks();
49
50 private :
51 MemoryDebug();
52 struct TBlock
53 {
54 std::size_t Size; // Taille allouée
55 std::string File; // Fichier contenant l'allocation
56 int Line; // Ligne de l'allocation
57 bool Array; // Est-ce un objet ou un tableau ?
58 };
59 typedef std::map<void*, TBlock> TBlockMap;
60
61 TBlockMap m_Blocks; // Blocs de mémoire alloués
62 std::stack<TBlock> m_DeleteStack; // Pile dont le sommet contient la ligne et le fichier de la prochaine désallocation
63 };
64
65 } //NameSpace
66
67 //***************************************************************************
68 // operator overloadings
69 //***************************************************************************
70
71 inline void* operator new(std::size_t Size, const char* File, int Line)
72 {
73 return ZenLib::MemoryDebug::Instance().Allocate(Size, File, Line, false);
74 }
75 inline void* operator new[](std::size_t Size, const char* File, int Line)
76 {
77 return ZenLib::MemoryDebug::Instance().Allocate(Size, File, Line, true);
78 }
79
80 inline void operator delete(void* Ptr)
81 {
82 if (ZenLib::MemoryDebug::g_IsShutdown)
83 free(Ptr);
84 else
85 ZenLib::MemoryDebug::Instance().Free(Ptr, false);
86 }
87
88 inline void operator delete[](void* Ptr)
89 {
90 if (ZenLib::MemoryDebug::g_IsShutdown)
91 free(Ptr);
92 else
93 ZenLib::MemoryDebug::Instance().Free(Ptr, true);
94 }
95
96 #if !defined(__BORLANDC__) // Borland does not support overloaded delete
97 inline void operator delete(void* Ptr, const char* File, int Line)
98 {
99 ZenLib::MemoryDebug::Instance().NextDelete(File, Line);
100 ZenLib::MemoryDebug::Instance().Free(Ptr, false);
101 }
102
103 inline void operator delete[](void* Ptr, const char* File, int Line)
104 {
105 ZenLib::MemoryDebug::Instance().NextDelete(File, Line);
106 ZenLib::MemoryDebug::Instance().Free(Ptr, true);
107 }
108 #endif
109
110 #if !defined(__MINGW32__) //TODO: Does not work on MinGW, don't know why
111 #ifndef new
112 #define new new(__FILE__, __LINE__)
113 #endif
114 #ifndef delete
115 #define delete ZenLib::MemoryDebug::Instance().NextDelete(__FILE__, __LINE__), delete
116 #endif
117 #endif // __MINGW32__
118
119 #endif // defined(ZENLIB_DEBUG)
120
121 #endif // ZenMemoryDebugH
Conf.h
ZenLib
Definition: BitStream.h:23