OgreMemoryTracker.h

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004 (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright (c) 2000-2012 Torus Knot Software Ltd
00008 
00009 Permission is hereby granted, free of charge, to any person obtaining a copy
00010 of this software and associated documentation files (the "Software"), to deal
00011 in the Software without restriction, including without limitation the rights
00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013 copies of the Software, and to permit persons to whom the Software is
00014 furnished to do so, subject to the following conditions:
00015 
00016 The above copyright notice and this permission notice shall be included in
00017 all copies or substantial portions of the Software.
00018 
00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025 THE SOFTWARE.
00026 -----------------------------------------------------------------------------
00027 */
00028 
00029 #ifndef _MemoryTracker_H__
00030 #define _MemoryTracker_H__
00031 
00032 // Don't include prerequisites, can cause a circular dependency
00033 // This file must be included within another file which already has the prerequisites in it
00034 //#include "OgrePrerequisites.h"
00035 #ifndef OGRE_COMPILER
00036 #   pragma message "MemoryTracker included somewhere OgrePrerequisites.h wasn't!"
00037 #endif
00038 
00039 // If we're using the GCC 3.1 C++ Std lib
00040 #if OGRE_COMPILER == OGRE_COMPILER_GNUC && OGRE_COMP_VER >= 310 && !defined(STLPORT)
00041 // We need to define a hash function for void*
00042 // For gcc 4.3 see http://gcc.gnu.org/gcc-4.3/changes.html
00043 #   if OGRE_COMP_VER >= 430
00044 #       include <tr1/unordered_map>
00045 #   else
00046 #       include <ext/hash_map>
00047 namespace __gnu_cxx
00048 {
00049     template <> struct hash< void* >
00050     {
00051         size_t operator()( void* const & ptr ) const
00052         {
00053             return (size_t)ptr;
00054         }
00055     };
00056 }
00057 
00058 #   endif
00059 #endif
00060 
00061 namespace Ogre
00062 {
00070 #if OGRE_MEMORY_TRACKER
00071 
00077     class _OgreExport MemoryTracker
00078     {
00079     protected:
00080         OGRE_AUTO_MUTEX
00081 
00082         // Allocation record
00083         struct Alloc
00084         {
00085             size_t bytes;
00086             unsigned int pool;
00087             std::string filename;
00088             size_t line;
00089             std::string function;
00090 
00091             Alloc() :bytes(0), line(0) {}
00092             Alloc(size_t sz, unsigned int p, const char* file, size_t ln, const char* func)
00093                 :bytes(sz), pool(p), line(ln)
00094             {
00095                 if (file)
00096                     filename = file;
00097                 if (func)
00098                     function = func;
00099             }
00100         };
00101 
00102         std::string mLeakFileName;
00103         bool mDumpToStdOut;
00104         typedef HashMap<void*, Alloc> AllocationMap;
00105         AllocationMap mAllocations;
00106 
00107         size_t mTotalAllocations;
00108         typedef std::vector<size_t> AllocationsByPool;
00109         AllocationsByPool mAllocationsByPool;
00110         bool mRecordEnable;
00111 
00112         void reportLeaks();
00113 
00114         // protected ctor
00115         MemoryTracker()
00116             : mLeakFileName("OgreLeaks.log"), mDumpToStdOut(true),
00117             mTotalAllocations(0), mRecordEnable(true)
00118         {
00119         }
00120     public:
00121 
00123         void setReportFileName(const std::string& name)
00124         {
00125             mLeakFileName = name;
00126         }
00128         const std::string& getReportFileName() const
00129         {
00130             return mLeakFileName;
00131         }
00133         void setReportToStdOut(bool rep)
00134         {
00135             mDumpToStdOut = rep;
00136         }
00138         bool getReportToStdOut() const
00139         {
00140             return mDumpToStdOut;
00141         }
00142 
00143         
00144 
00146         size_t getTotalMemoryAllocated() const;
00148         size_t getMemoryAllocatedForPool(unsigned int pool) const;
00149 
00150 
00160         void _recordAlloc(void* ptr, size_t sz, unsigned int pool = 0,
00161                           const char* file = 0, size_t ln = 0, const char* func = 0);
00163         void _recordDealloc(void* ptr);
00164 
00166         void setRecordEnable(bool recordEnable)
00167         {
00168             mRecordEnable = recordEnable;
00169         }
00170 
00172         bool getRecordEnable() const
00173         {
00174             return mRecordEnable;
00175         }
00176 
00177         ~MemoryTracker()
00178         {
00179             reportLeaks();
00180         }
00181 
00183         static MemoryTracker& get();
00184 
00185 
00186     };
00187 
00188 
00189 
00190 #endif
00191 
00194 }
00195 
00196 #endif
00197 

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Fri May 25 23:36:24 2012