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-2013 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 #include "OgreHeaderPrefix.h"
00033 
00034 // Don't include prerequisites, can cause a circular dependency
00035 // This file must be included within another file which already has the prerequisites in it
00036 //#include "OgrePrerequisites.h"
00037 #ifndef OGRE_COMPILER
00038 #   pragma message "MemoryTracker included somewhere OgrePrerequisites.h wasn't!"
00039 #endif
00040 
00041 // If we're using the GCC 3.1 C++ Std lib
00042 #if OGRE_COMPILER == OGRE_COMPILER_GNUC && OGRE_COMP_VER >= 310 && !defined(STLPORT)
00043 // We need to define a hash function for void*
00044 // For gcc 4.3 see http://gcc.gnu.org/gcc-4.3/changes.html
00045 #   if OGRE_COMP_VER >= 430
00046 #       include <tr1/unordered_map>
00047 #   else
00048 #       include <ext/hash_map>
00049 namespace __gnu_cxx
00050 {
00051     template <> struct hash< void* >
00052     {
00053         size_t operator()( void* const & ptr ) const
00054         {
00055             return (size_t)ptr;
00056         }
00057     };
00058 }
00059 
00060 #   endif
00061 #endif
00062 
00063 #if OGRE_MEMORY_TRACKER
00064 #   include "Threading/OgreThreadHeaders.h"
00065 #endif
00066 
00067 namespace Ogre
00068 {
00076 #if OGRE_MEMORY_TRACKER
00077 
00083     class _OgreExport MemoryTracker
00084     {
00085     protected:
00086             OGRE_AUTO_MUTEX;
00087 
00088         // Allocation record
00089         struct Alloc
00090         {
00091             size_t bytes;
00092             unsigned int pool;
00093             std::string filename;
00094             size_t line;
00095             std::string function;
00096 
00097             Alloc() :bytes(0), line(0) {}
00098             Alloc(size_t sz, unsigned int p, const char* file, size_t ln, const char* func)
00099                 :bytes(sz), pool(p), line(ln)
00100             {
00101                 if (file)
00102                     filename = file;
00103                 if (func)
00104                     function = func;
00105             }
00106         };
00107 
00108         std::string mLeakFileName;
00109         bool mDumpToStdOut;
00110         typedef HashMap<void*, Alloc> AllocationMap;
00111         AllocationMap mAllocations;
00112 
00113         size_t mTotalAllocations;
00114         typedef std::vector<size_t> AllocationsByPool;
00115         AllocationsByPool mAllocationsByPool;
00116         bool mRecordEnable;
00117 
00118         void reportLeaks();
00119 
00120         // protected ctor
00121         MemoryTracker()
00122             : mLeakFileName("OgreLeaks.log"), mDumpToStdOut(true),
00123             mTotalAllocations(0), mRecordEnable(true)
00124         {
00125         }
00126     public:
00127 
00129         void setReportFileName(const std::string& name)
00130         {
00131             mLeakFileName = name;
00132         }
00134         const std::string& getReportFileName() const
00135         {
00136             return mLeakFileName;
00137         }
00139         void setReportToStdOut(bool rep)
00140         {
00141             mDumpToStdOut = rep;
00142         }
00144         bool getReportToStdOut() const
00145         {
00146             return mDumpToStdOut;
00147         }
00148 
00149         
00150 
00152         size_t getTotalMemoryAllocated() const;
00154         size_t getMemoryAllocatedForPool(unsigned int pool) const;
00155 
00156 
00166         void _recordAlloc(void* ptr, size_t sz, unsigned int pool = 0,
00167                           const char* file = 0, size_t ln = 0, const char* func = 0);
00169         void _recordDealloc(void* ptr);
00170 
00172         void setRecordEnable(bool recordEnable)
00173         {
00174             mRecordEnable = recordEnable;
00175         }
00176 
00178         bool getRecordEnable() const
00179         {
00180             return mRecordEnable;
00181         }
00182 
00183         ~MemoryTracker()
00184         {
00185             reportLeaks();
00186         }
00187 
00189         static MemoryTracker& get();
00190 
00191 
00192     };
00193 
00194 
00195 
00196 #endif
00197 
00200 }
00201 
00202 #include "OgreHeaderSuffix.h"
00203 
00204 #endif
00205 

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 Mon Jul 27 2020 13:40:44