OgreMemoryTracker.h
Go to the documentation of this file.
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2013 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#ifndef _MemoryTracker_H__
#define _MemoryTracker_H__
#include "OgreHeaderPrefix.h"
// Don't include prerequisites, can cause a circular dependency
// This file must be included within another file which already has the prerequisites in it
//#include "OgrePrerequisites.h"
#ifndef OGRE_COMPILER
# pragma message "MemoryTracker included somewhere OgrePrerequisites.h wasn't!"
#endif
// If we're using the GCC 3.1 C++ Std lib
#if OGRE_COMPILER == OGRE_COMPILER_GNUC && OGRE_COMP_VER >= 310 && !defined(STLPORT)
// We need to define a hash function for void*
// For gcc 4.3 see http://gcc.gnu.org/gcc-4.3/changes.html
# if OGRE_COMP_VER >= 430
# include <tr1/unordered_map>
# else
# include <ext/hash_map>
namespace __gnu_cxx
{
template <> struct hash< void* >
{
size_t operator()( void* const & ptr ) const
{
return (size_t)ptr;
}
};
}
# endif
#endif
#if OGRE_MEMORY_TRACKER
# include "Threading/OgreThreadHeaders.h"
#endif
namespace Ogre
{
#if OGRE_MEMORY_TRACKER
class _OgreExport MemoryTracker
{
protected:
OGRE_AUTO_MUTEX;
// Allocation record
struct Alloc
{
size_t bytes;
unsigned int pool;
std::string filename;
size_t line;
std::string function;
Alloc() :bytes(0), line(0) {}
Alloc(size_t sz, unsigned int p, const char* file, size_t ln, const char* func)
:bytes(sz), pool(p), line(ln)
{
if (file)
filename = file;
if (func)
function = func;
}
};
std::string mLeakFileName;
bool mDumpToStdOut;
typedef HashMap<void*, Alloc> AllocationMap;
AllocationMap mAllocations;
size_t mTotalAllocations;
typedef std::vector<size_t> AllocationsByPool;
AllocationsByPool mAllocationsByPool;
bool mRecordEnable;
void reportLeaks();
// protected ctor
MemoryTracker()
: mLeakFileName("OgreLeaks.log"), mDumpToStdOut(true),
mTotalAllocations(0), mRecordEnable(true)
{
}
public:
void setReportFileName(const std::string& name)
{
mLeakFileName = name;
}
const std::string& getReportFileName() const
{
return mLeakFileName;
}
void setReportToStdOut(bool rep)
{
mDumpToStdOut = rep;
}
bool getReportToStdOut() const
{
return mDumpToStdOut;
}
size_t getTotalMemoryAllocated() const;
size_t getMemoryAllocatedForPool(unsigned int pool) const;
void _recordAlloc(void* ptr, size_t sz, unsigned int pool = 0,
const char* file = 0, size_t ln = 0, const char* func = 0);
void _recordDealloc(void* ptr);
void setRecordEnable(bool recordEnable)
{
mRecordEnable = recordEnable;
}
bool getRecordEnable() const
{
return mRecordEnable;
}
~MemoryTracker()
{
reportLeaks();
}
static MemoryTracker& get();
};
#endif
}
#include "OgreHeaderSuffix.h"
#endif
_OgreExport
#define _OgreExport
Definition: OgrePlatform.h:257
OGRE_AUTO_MUTEX
#define OGRE_AUTO_MUTEX
Definition: OgreThreadDefinesBoost.h:46
OgreHeaderSuffix.h
OgreThreadHeaders.h
Ogre
Definition: OgreAndroidLogListener.h:34
OgreHeaderPrefix.h

Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.