OGRE  1.7
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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-2011 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 // Don't include prerequisites, can cause a circular dependency
33 // This file must be included within another file which already has the prerequisites in it
34 //#include "OgrePrerequisites.h"
35 #ifndef OGRE_COMPILER
36 # pragma message "MemoryTracker included somewhere OgrePrerequisites.h wasn't!"
37 #endif
38 
39 // If we're using the GCC 3.1 C++ Std lib
40 #if OGRE_COMPILER == OGRE_COMPILER_GNUC && OGRE_COMP_VER >= 310 && !defined(STLPORT)
41 // We need to define a hash function for void*
42 // For gcc 4.3 see http://gcc.gnu.org/gcc-4.3/changes.html
43 # if OGRE_COMP_VER >= 430
44 # include <tr1/unordered_map>
45 # else
46 # include <ext/hash_map>
47 namespace __gnu_cxx
48 {
49  template <> struct hash< void* >
50  {
51  size_t operator()( void* const & ptr ) const
52  {
53  return (size_t)ptr;
54  }
55  };
56 }
57 
58 # endif
59 #endif
60 
61 namespace Ogre
62 {
70 #if OGRE_MEMORY_TRACKER
71 
77  class _OgreExport MemoryTracker
78  {
79  protected:
81 
82  // Allocation record
83  struct Alloc
84  {
85  size_t bytes;
86  unsigned int pool;
87  std::string filename;
88  size_t line;
89  std::string function;
90 
91  Alloc() :bytes(0), line(0) {}
92  Alloc(size_t sz, unsigned int p, const char* file, size_t ln, const char* func)
93  :bytes(sz), pool(p), line(ln)
94  {
95  if (file)
96  filename = file;
97  if (func)
98  function = func;
99  }
100  };
101 
102  std::string mLeakFileName;
103  bool mDumpToStdOut;
104  typedef HashMap<void*, Alloc> AllocationMap;
105  AllocationMap mAllocations;
106 
107  size_t mTotalAllocations;
108  typedef std::vector<size_t> AllocationsByPool;
109  AllocationsByPool mAllocationsByPool;
110 
111  void reportLeaks();
112 
113  // protected ctor
114  MemoryTracker()
115  : mLeakFileName("OgreLeaks.log"), mDumpToStdOut(true),
116  mTotalAllocations(0)
117  {
118  }
119  public:
120 
122  void setReportFileName(const std::string& name)
123  {
124  mLeakFileName = name;
125  }
127  const std::string& getReportFileName() const
128  {
129  return mLeakFileName;
130  }
132  void setReportToStdOut(bool rep)
133  {
134  mDumpToStdOut = rep;
135  }
137  bool getReportToStdOut() const
138  {
139  return mDumpToStdOut;
140  }
141 
143  size_t getTotalMemoryAllocated() const;
145  size_t getMemoryAllocatedForPool(unsigned int pool) const;
146 
147 
157  void _recordAlloc(void* ptr, size_t sz, unsigned int pool = 0,
158  const char* file = 0, size_t ln = 0, const char* func = 0);
160  void _recordDealloc(void* ptr);
161 
162  ~MemoryTracker()
163  {
164  reportLeaks();
165  }
166 
168  static MemoryTracker& get();
169 
170 
171  };
172 
173 
174 
175 #endif
176 
179 }
180 
181 #endif
182 
#define OGRE_AUTO_MUTEX
#define _OgreExport
Definition: OgrePlatform.h:203