Patch 2793067: fix trunk with OGRE_THREAD_SUPPORT=1 on non-Windows platforms (don...
[ogre3d.git] / OgreMain / src / OgreMemoryTracker.cpp
blobb75466972e2384e25d91b78d889d99dba4848fba
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
7 Copyright (c) 2000-2008 Torus Knot Software Ltd
8 Also see acknowledgements in Readme.html
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of the GNU Lesser General Public License as published by the Free Software
12 Foundation; either version 2 of the License, or (at your option) any later
13 version.
15 This program is distributed in the hope that it will be useful, but WITHOUT
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
19 You should have received a copy of the GNU Lesser General Public License along with
20 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22 http://www.gnu.org/copyleft/lesser.txt.
24 You may alternatively use this source under the terms of a specific version of
25 the OGRE Unrestricted License provided you have obtained such a license from
26 Torus Knot Software Ltd
27 ---------------------------------------------------------------------------
29 #include "OgrePlatform.h"
30 #include "OgreStableHeaders.h"
31 #include "OgrePrerequisites.h"
32 #include "OgreMemoryTracker.h"
33 #include "OgreString.h"
36 namespace Ogre
39 #if OGRE_MEMORY_TRACKER
40 //--------------------------------------------------------------------------
41 MemoryTracker& MemoryTracker::get()
43 static MemoryTracker tracker;
44 return tracker;
46 //--------------------------------------------------------------------------
47 void MemoryTracker::_recordAlloc(void* ptr, size_t sz, unsigned int pool,
48 const char* file, size_t ln, const char* func)
50 OGRE_LOCK_AUTO_MUTEX
52 assert(mAllocations.find(ptr) == mAllocations.end() && "Double allocation with same address - "
53 "this probably means you have a mismatched allocation / deallocation style, "
54 "check if you're are using OGRE_ALLOC_T / OGRE_FREE and OGRE_NEW_T / OGRE_DELETE_T consistently");
56 mAllocations[ptr] = Alloc(sz, pool, file, ln, func);
57 if(pool >= mAllocationsByPool.size())
58 mAllocationsByPool.resize(pool+1, 0);
59 mAllocationsByPool[pool] += sz;
60 mTotalAllocations += sz;
62 //--------------------------------------------------------------------------
63 void MemoryTracker::_recordDealloc(void* ptr)
65 // deal cleanly with null pointers
66 if (!ptr)
67 return;
69 OGRE_LOCK_AUTO_MUTEX
71 AllocationMap::iterator i = mAllocations.find(ptr);
72 assert(i != mAllocations.end() && "Unable to locate allocation unit - "
73 "this probably means you have a mismatched allocation / deallocation style, "
74 "check if you're are using OGRE_ALLOC_T / OGRE_FREE and OGRE_NEW_T / OGRE_DELETE_T consistently");
75 // update category stats
76 mAllocationsByPool[i->second.pool] -= i->second.bytes;
77 // global stats
78 mTotalAllocations -= i->second.bytes;
79 mAllocations.erase(i);
81 //--------------------------------------------------------------------------
82 size_t MemoryTracker::getTotalMemoryAllocated() const
84 return mTotalAllocations;
86 //--------------------------------------------------------------------------
87 size_t MemoryTracker::getMemoryAllocatedForPool(unsigned int pool) const
89 return mAllocationsByPool[pool];
91 //--------------------------------------------------------------------------
92 void MemoryTracker::reportLeaks()
94 StringUtil::StrStreamType os;
96 if (mAllocations.empty())
97 os << "No leaks!";
98 else
100 size_t totalMem = 0;
101 os << "Leaks detected:" << std::endl << std::endl;
102 for (AllocationMap::const_iterator i = mAllocations.begin(); i != mAllocations.end(); ++i)
104 const Alloc& alloc = i->second;
105 if (!alloc.filename.empty())
106 os << alloc.filename << "(" << alloc.line << ", " << alloc.function << "): ";
107 else
108 os << "(unknown source): ";
109 os << alloc.bytes << " bytes";
110 os << std::endl;
111 totalMem += alloc.bytes;
114 os << std::endl;
115 os << mAllocations.size() << " leaks detected, " << totalMem << " bytes total";
118 if (mDumpToStdOut)
119 std::cout << os.str();
121 std::ofstream of;
122 of.open(mLeakFileName.c_str());
123 of << os.str();
124 of.close();
126 #endif // OGRE_DEBUG_MODE