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 #include <ext/hash_map>
00039 namespace __gnu_cxx
00040 {
00041     template <> struct hash< void* >
00042     {
00043         size_t operator()( void* const & ptr ) const
00044         {
00045             return (size_t)ptr;
00046         }
00047     };
00048 }
00049 
00050 #endif
00051 
00052 namespace Ogre
00053 {
00054 
00055 #if OGRE_MEMORY_TRACKER
00056 
00062     class _OgreExport MemoryTracker
00063     {
00064     protected:
00065         OGRE_AUTO_MUTEX
00066 
00067         // Allocation record
00068         struct Alloc
00069         {
00070             size_t bytes;
00071             unsigned int pool;
00072             std::string filename;
00073             size_t line;
00074             std::string function;
00075 
00076             Alloc() :line(0), bytes(0) {}
00077             Alloc(size_t sz, unsigned int p, const char* file, size_t ln, const char* func)
00078                 :bytes(sz), pool(p), line(ln)
00079             {
00080                 if (file)
00081                     filename = file;
00082                 if (func)
00083                     function = func;
00084             }
00085         };
00086         
00087         std::string mLeakFileName;
00088         bool mDumpToStdOut;
00089         typedef HashMap<void*, Alloc> AllocationMap;
00090         AllocationMap mAllocations;
00091         
00092         size_t mTotalAllocations;
00093         typedef std::vector<size_t> AllocationsByPool;
00094         AllocationsByPool mAllocationsByPool;
00095 
00096         void reportLeaks();
00097 
00098         // protected ctor
00099         MemoryTracker()
00100             : mLeakFileName("OgreLeaks.log"), mDumpToStdOut(true),
00101             mTotalAllocations(0)
00102         {
00103         }
00104     public:
00105 
00107         void setReportFileName(const std::string& name)
00108         {
00109             mLeakFileName = name;
00110         }
00112         const std::string& getReportFileName() const
00113         {
00114             return mLeakFileName;
00115         }
00117         void setReportToStdOut(bool rep)
00118         {
00119             mDumpToStdOut = rep;
00120         }
00122         bool getReportToStdOut() const
00123         {
00124             return mDumpToStdOut;
00125         }
00126         
00128         size_t getTotalMemoryAllocated() const;
00130         size_t getMemoryAllocatedForPool(unsigned int pool) const;
00131         
00132 
00142         void _recordAlloc(void* ptr, size_t sz, unsigned int pool = 0, 
00143                           const char* file = 0, size_t ln = 0, const char* func = 0);
00145         void _recordDealloc(void* ptr);
00146 
00147         ~MemoryTracker()
00148         {
00149             reportLeaks();
00150         }
00151         
00153         static MemoryTracker& get();
00154 
00155         
00156     };
00157     
00158 
00159     
00160 #endif
00161 }
00162 
00163 #endif
00164 

Copyright © 2008 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Thu Aug 28 20:53:49 2008