# 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-2009 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
    00111         void reportLeaks();
    00112
    00113         // protected ctor
    00114         MemoryTracker()
    00115             : mLeakFileName("OgreLeaks.log"), mDumpToStdOut(true),
    00116             mTotalAllocations(0)
    00117         {
    00118         }
    00119     public:
    00120
    00122         void setReportFileName(const std::string& name)
    00123         {
    00124             mLeakFileName = name;
    00125         }
    00127         const std::string& getReportFileName() const
    00128         {
    00129             return mLeakFileName;
    00130         }
    00132         void setReportToStdOut(bool rep)
    00133         {
    00134             mDumpToStdOut = rep;
    00135         }
    00137         bool getReportToStdOut() const
    00138         {
    00139             return mDumpToStdOut;
    00140         }
    00141
    00143         size_t getTotalMemoryAllocated() const;
    00145         size_t getMemoryAllocatedForPool(unsigned int pool) const;
    00146
    00147
    00157         void _recordAlloc(void* ptr, size_t sz, unsigned int pool = 0,
    00158                           const char* file = 0, size_t ln = 0, const char* func = 0);
    00160         void _recordDealloc(void* ptr);
    00161
    00162         ~MemoryTracker()
    00163         {
    00164             reportLeaks();
    00165         }
    00166
    00168         static MemoryTracker& get();
    00169
    00170
    00171     };
    00172
    00173
    00174
    00175 #endif
    00176
    00179 }
    00180
    00181 #endif
    00182

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Sun May 8 2011 20:28:08