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-2008 Torus Knot Software Ltd
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 
00024 You may alternatively use this source under the terms of a specific version of
00025 the OGRE Unrestricted License provided you have obtained such a license from
00026 Torus Knot Software Ltd
00027 ---------------------------------------------------------------------------
00028 */
00029 
00030 #ifndef _MemoryTracker_H__
00031 #define _MemoryTracker_H__
00032 
00033 #include "OgrePrerequisites.h"
00034 
00035 // If we're using the GCC 3.1 C++ Std lib
00036 #if OGRE_COMPILER == OGRE_COMPILER_GNUC && OGRE_COMP_VER >= 310 && !defined(STLPORT)
00037 // We need to define a hash function for void*
00038 // For gcc 4.3 see http://gcc.gnu.org/gcc-4.3/changes.html
00039 #   if OGRE_COMP_VER >= 430
00040 #       include <backward/hash_map>
00041 #   else
00042 #       include <ext/hash_map>
00043 #   endif
00044 namespace __gnu_cxx
00045 {
00046     template <> struct hash< void* >
00047     {
00048         size_t operator()( void* const & ptr ) const
00049         {
00050             return (size_t)ptr;
00051         }
00052     };
00053 }
00054 
00055 #endif
00056 
00057 namespace Ogre
00058 {
00059 
00060 #if OGRE_MEMORY_TRACKER
00061 
00067     class _OgreExport MemoryTracker
00068     {
00069     protected:
00070         OGRE_AUTO_MUTEX
00071 
00072         // Allocation record
00073         struct Alloc
00074         {
00075             size_t bytes;
00076             unsigned int pool;
00077             std::string filename;
00078             size_t line;
00079             std::string function;
00080 
00081             Alloc() :line(0), bytes(0) {}
00082             Alloc(size_t sz, unsigned int p, const char* file, size_t ln, const char* func)
00083                 :bytes(sz), pool(p), line(ln)
00084             {
00085                 if (file)
00086                     filename = file;
00087                 if (func)
00088                     function = func;
00089             }
00090         };
00091         
00092         std::string mLeakFileName;
00093         bool mDumpToStdOut;
00094         typedef HashMap<void*, Alloc> AllocationMap;
00095         AllocationMap mAllocations;
00096         
00097         size_t mTotalAllocations;
00098         typedef std::vector<size_t> AllocationsByPool;
00099         AllocationsByPool mAllocationsByPool;
00100 
00101         void reportLeaks();
00102 
00103         // protected ctor
00104         MemoryTracker()
00105             : mLeakFileName("OgreLeaks.log"), mDumpToStdOut(true),
00106             mTotalAllocations(0)
00107         {
00108         }
00109     public:
00110 
00112         void setReportFileName(const std::string& name)
00113         {
00114             mLeakFileName = name;
00115         }
00117         const std::string& getReportFileName() const
00118         {
00119             return mLeakFileName;
00120         }
00122         void setReportToStdOut(bool rep)
00123         {
00124             mDumpToStdOut = rep;
00125         }
00127         bool getReportToStdOut() const
00128         {
00129             return mDumpToStdOut;
00130         }
00131         
00133         size_t getTotalMemoryAllocated() const;
00135         size_t getMemoryAllocatedForPool(unsigned int pool) const;
00136         
00137 
00147         void _recordAlloc(void* ptr, size_t sz, unsigned int pool = 0, 
00148                           const char* file = 0, size_t ln = 0, const char* func = 0);
00150         void _recordDealloc(void* ptr);
00151 
00152         ~MemoryTracker()
00153         {
00154             reportLeaks();
00155         }
00156         
00158         static MemoryTracker& get();
00159 
00160         
00161     };
00162     
00163 
00164     
00165 #endif
00166 }
00167 
00168 #endif
00169 

Copyright © 2008 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Tue Nov 4 11:02:30 2008