1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2013 Torus Knot Software Ltd |
---|
8 | |
---|
9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
10 | of this software and associated documentation files (the "Software"), to deal |
---|
11 | in the Software without restriction, including without limitation the rights |
---|
12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
13 | copies of the Software, and to permit persons to whom the Software is |
---|
14 | furnished to do so, subject to the following conditions: |
---|
15 | |
---|
16 | The above copyright notice and this permission notice shall be included in |
---|
17 | all copies or substantial portions of the Software. |
---|
18 | |
---|
19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
25 | THE SOFTWARE. |
---|
26 | ----------------------------------------------------------------------------- |
---|
27 | */ |
---|
28 | |
---|
29 | #ifndef _MemoryTracker_H__ |
---|
30 | #define _MemoryTracker_H__ |
---|
31 | |
---|
32 | #include "OgreHeaderPrefix.h" |
---|
33 | |
---|
34 | // Don't include prerequisites, can cause a circular dependency |
---|
35 | // This file must be included within another file which already has the prerequisites in it |
---|
36 | //#include "OgrePrerequisites.h" |
---|
37 | #ifndef OGRE_COMPILER |
---|
38 | # pragma message "MemoryTracker included somewhere OgrePrerequisites.h wasn't!" |
---|
39 | #endif |
---|
40 | |
---|
41 | // If we're using the GCC 3.1 C++ Std lib |
---|
42 | #if OGRE_COMPILER == OGRE_COMPILER_GNUC && OGRE_COMP_VER >= 310 && !defined(STLPORT) |
---|
43 | // We need to define a hash function for void* |
---|
44 | // For gcc 4.3 see http://gcc.gnu.org/gcc-4.3/changes.html |
---|
45 | # if OGRE_COMP_VER >= 430 |
---|
46 | # include <tr1/unordered_map> |
---|
47 | # else |
---|
48 | # include <ext/hash_map> |
---|
49 | namespace __gnu_cxx |
---|
50 | { |
---|
51 | template <> struct hash< void* > |
---|
52 | { |
---|
53 | size_t operator()( void* const & ptr ) const |
---|
54 | { |
---|
55 | return (size_t)ptr; |
---|
56 | } |
---|
57 | }; |
---|
58 | } |
---|
59 | |
---|
60 | # endif |
---|
61 | #endif |
---|
62 | |
---|
63 | #if OGRE_MEMORY_TRACKER |
---|
64 | # include "Threading/OgreThreadHeaders.h" |
---|
65 | #endif |
---|
66 | |
---|
67 | namespace Ogre |
---|
68 | { |
---|
69 | /** \addtogroup Core |
---|
70 | * @{ |
---|
71 | */ |
---|
72 | /** \addtogroup Memory |
---|
73 | * @{ |
---|
74 | */ |
---|
75 | |
---|
76 | #if OGRE_MEMORY_TRACKER |
---|
77 | |
---|
78 | /** This class tracks the allocations and deallocations made, and |
---|
79 | is able to report memory statistics and leaks. |
---|
80 | @note |
---|
81 | This class is only available in debug builds. |
---|
82 | */ |
---|
83 | class _OgreExport MemoryTracker |
---|
84 | { |
---|
85 | protected: |
---|
86 | OGRE_AUTO_MUTEX; |
---|
87 | |
---|
88 | // Allocation record |
---|
89 | struct Alloc |
---|
90 | { |
---|
91 | size_t bytes; |
---|
92 | unsigned int pool; |
---|
93 | std::string filename; |
---|
94 | size_t line; |
---|
95 | std::string function; |
---|
96 | |
---|
97 | Alloc() :bytes(0), line(0) {} |
---|
98 | Alloc(size_t sz, unsigned int p, const char* file, size_t ln, const char* func) |
---|
99 | :bytes(sz), pool(p), line(ln) |
---|
100 | { |
---|
101 | if (file) |
---|
102 | filename = file; |
---|
103 | if (func) |
---|
104 | function = func; |
---|
105 | } |
---|
106 | }; |
---|
107 | |
---|
108 | std::string mLeakFileName; |
---|
109 | bool mDumpToStdOut; |
---|
110 | typedef HashMap<void*, Alloc> AllocationMap; |
---|
111 | AllocationMap mAllocations; |
---|
112 | |
---|
113 | size_t mTotalAllocations; |
---|
114 | typedef std::vector<size_t> AllocationsByPool; |
---|
115 | AllocationsByPool mAllocationsByPool; |
---|
116 | bool mRecordEnable; |
---|
117 | |
---|
118 | void reportLeaks(); |
---|
119 | |
---|
120 | // protected ctor |
---|
121 | MemoryTracker() |
---|
122 | : mLeakFileName("OgreLeaks.log"), mDumpToStdOut(true), |
---|
123 | mTotalAllocations(0), mRecordEnable(true) |
---|
124 | { |
---|
125 | } |
---|
126 | public: |
---|
127 | |
---|
128 | /** Set the name of the report file that will be produced on exit. */ |
---|
129 | void setReportFileName(const std::string& name) |
---|
130 | { |
---|
131 | mLeakFileName = name; |
---|
132 | } |
---|
133 | /// Return the name of the file which will contain the report at exit |
---|
134 | const std::string& getReportFileName() const |
---|
135 | { |
---|
136 | return mLeakFileName; |
---|
137 | } |
---|
138 | /// Sets whether the memory report should be sent to stdout |
---|
139 | void setReportToStdOut(bool rep) |
---|
140 | { |
---|
141 | mDumpToStdOut = rep; |
---|
142 | } |
---|
143 | /// Gets whether the memory report should be sent to stdout |
---|
144 | bool getReportToStdOut() const |
---|
145 | { |
---|
146 | return mDumpToStdOut; |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | |
---|
151 | /// Get the total amount of memory allocated currently. |
---|
152 | size_t getTotalMemoryAllocated() const; |
---|
153 | /// Get the amount of memory allocated in a given pool |
---|
154 | size_t getMemoryAllocatedForPool(unsigned int pool) const; |
---|
155 | |
---|
156 | |
---|
157 | /** Record an allocation that has been made. Only to be called by |
---|
158 | the memory management subsystem. |
---|
159 | @param ptr The pointer to the memory |
---|
160 | @param sz The size of the memory in bytes |
---|
161 | @param pool The memory pool this allocation is occurring from |
---|
162 | @param file The file in which the allocation is being made |
---|
163 | @param ln The line on which the allocation is being made |
---|
164 | @param func The function in which the allocation is being made |
---|
165 | */ |
---|
166 | void _recordAlloc(void* ptr, size_t sz, unsigned int pool = 0, |
---|
167 | const char* file = 0, size_t ln = 0, const char* func = 0); |
---|
168 | /** Record the deallocation of memory. */ |
---|
169 | void _recordDealloc(void* ptr); |
---|
170 | |
---|
171 | /// Sets whether the record alloc/dealloc enabled. |
---|
172 | void setRecordEnable(bool recordEnable) |
---|
173 | { |
---|
174 | mRecordEnable = recordEnable; |
---|
175 | } |
---|
176 | |
---|
177 | /// Gets whether the record alloc/dealloc enabled. |
---|
178 | bool getRecordEnable() const |
---|
179 | { |
---|
180 | return mRecordEnable; |
---|
181 | } |
---|
182 | |
---|
183 | ~MemoryTracker() |
---|
184 | { |
---|
185 | reportLeaks(); |
---|
186 | } |
---|
187 | |
---|
188 | /// Static utility method to get the memory tracker instance |
---|
189 | static MemoryTracker& get(); |
---|
190 | |
---|
191 | |
---|
192 | }; |
---|
193 | |
---|
194 | |
---|
195 | |
---|
196 | #endif |
---|
197 | /** @} */ |
---|
198 | /** @} */ |
---|
199 | |
---|
200 | } |
---|
201 | |
---|
202 | #include "OgreHeaderSuffix.h" |
---|
203 | |
---|
204 | #endif |
---|
205 | |
---|