OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreMemoryTracker.h
Go to the documentation of this file.
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-2014 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
65 #endif
66 
67 namespace Ogre
68 {
76 #if OGRE_MEMORY_TRACKER
77 
83  class _OgreExport MemoryTracker
84  {
85  protected:
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 
129  void setReportFileName(const std::string& name)
130  {
131  mLeakFileName = name;
132  }
134  const std::string& getReportFileName() const
135  {
136  return mLeakFileName;
137  }
139  void setReportToStdOut(bool rep)
140  {
141  mDumpToStdOut = rep;
142  }
144  bool getReportToStdOut() const
145  {
146  return mDumpToStdOut;
147  }
148 
149 
150 
152  size_t getTotalMemoryAllocated() const;
154  size_t getMemoryAllocatedForPool(unsigned int pool) const;
155 
156 
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);
169  void _recordDealloc(void* ptr);
170 
172  void setRecordEnable(bool recordEnable)
173  {
174  mRecordEnable = recordEnable;
175  }
176 
178  bool getRecordEnable() const
179  {
180  return mRecordEnable;
181  }
182 
183  ~MemoryTracker()
184  {
185  reportLeaks();
186  }
187 
189  static MemoryTracker& get();
190 
191 
192  };
193 
194 
195 
196 #endif
197 
200 }
201 
202 #include "OgreHeaderSuffix.h"
203 
204 #endif
205 
#define _OgreExport
Definition: OgrePlatform.h:255
#define OGRE_AUTO_MUTEX