# 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 // Don't include prerequisites, can cause a circular dependency
    00034 // This file must be included within another file which already has the prerequisites in it
    00035 //#include "OgrePrerequisites.h"
    00036 #ifndef OGRE_COMPILER
    00037 #   pragma message "MemoryTracker included somewhere OgrePrerequisites.h wasn't!"
    00038 #endif
    00039
    00040 // If we're using the GCC 3.1 C++ Std lib
    00041 #if OGRE_COMPILER == OGRE_COMPILER_GNUC && OGRE_COMP_VER >= 310 && !defined(STLPORT)
    00042 // We need to define a hash function for void*
    00043 // For gcc 4.3 see http://gcc.gnu.org/gcc-4.3/changes.html
    00044 #   if OGRE_COMP_VER >= 430
    00045 #       include <tr1/unordered_map>
    00046 #   else
    00047 #       include <ext/hash_map>
    00048 namespace __gnu_cxx
    00049 {
    00050     template <> struct hash< void* >
    00051     {
    00052         size_t operator()( void* const & ptr ) const
    00053         {
    00054             return (size_t)ptr;
    00055         }
    00056     };
    00057 }
    00058
    00059 #   endif
    00060 #endif
    00061
    00062 namespace Ogre
    00063 {
    00064
    00065 #if OGRE_MEMORY_TRACKER
    00066
    00072     class _OgreExport MemoryTracker
    00073     {
    00074     protected:
    00075         OGRE_AUTO_MUTEX
    00076
    00077         // Allocation record
    00078         struct Alloc
    00079         {
    00080             size_t bytes;
    00081             unsigned int pool;
    00082             std::string filename;
    00083             size_t line;
    00084             std::string function;
    00085
    00086             Alloc() :line(0), bytes(0) {}
    00087             Alloc(size_t sz, unsigned int p, const char* file, size_t ln, const char* func)
    00088                 :bytes(sz), pool(p), line(ln)
    00089             {
    00090                 if (file)
    00091                     filename = file;
    00092                 if (func)
    00093                     function = func;
    00094             }
    00095         };
    00096
    00097         std::string mLeakFileName;
    00098         bool mDumpToStdOut;
    00099         typedef HashMap<void*, Alloc> AllocationMap;
    00100         AllocationMap mAllocations;
    00101
    00102         size_t mTotalAllocations;
    00103         typedef std::vector<size_t> AllocationsByPool;
    00104         AllocationsByPool mAllocationsByPool;
    00105
    00106         void reportLeaks();
    00107
    00108         // protected ctor
    00109         MemoryTracker()
    00110             : mLeakFileName("OgreLeaks.log"), mDumpToStdOut(true),
    00111             mTotalAllocations(0)
    00112         {
    00113         }
    00114     public:
    00115
    00117         void setReportFileName(const std::string& name)
    00118         {
    00119             mLeakFileName = name;
    00120         }
    00122         const std::string& getReportFileName() const
    00123         {
    00124             return mLeakFileName;
    00125         }
    00127         void setReportToStdOut(bool rep)
    00128         {
    00129             mDumpToStdOut = rep;
    00130         }
    00132         bool getReportToStdOut() const
    00133         {
    00134             return mDumpToStdOut;
    00135         }
    00136
    00138         size_t getTotalMemoryAllocated() const;
    00140         size_t getMemoryAllocatedForPool(unsigned int pool) const;
    00141
    00142
    00152         void _recordAlloc(void* ptr, size_t sz, unsigned int pool = 0,
    00153                           const char* file = 0, size_t ln = 0, const char* func = 0);
    00155         void _recordDealloc(void* ptr);
    00156
    00157         ~MemoryTracker()
    00158         {
    00159             reportLeaks();
    00160         }
    00161
    00163         static MemoryTracker& get();
    00164
    00165
    00166     };
    00167
    00168
    00169
    00170 #endif
    00171 }
    00172
    00173 #endif
    00174

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 2.5 License.
Last modified Sun Jul 26 11:32:18 2009