00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include "KeosMemoryManager.h"
00049 #include "KeosException.h"
00050 #include "KeosNoMemoryMacros.h"
00051
00052 namespace Keos
00053 {
00054
00055
00056
00057
00058
00059 CMemoryManager::CMemoryManager() :
00060 m_File(KEOS_MEMLEAKS_FILE)
00061 {
00062
00063 if (!m_File)
00064 throw CLoadingFailed(KEOS_MEMLEAKS_FILE, "Impossible to write in file", "CMemoryManager::CMemoryManager");
00065
00066
00067 m_File << "=============================================" << std::endl;
00068 m_File << " Keos::Engine v" << KEOS_VERSION_MAJOR << "." << KEOS_VERSION_MINOR <<
00069 "." << KEOS_VERSION_PATCH << " - Memory leak tracker - " << std::endl;
00070 m_File << "=============================================" << std::endl << std::endl;
00071
00072 }
00073
00074
00075 CMemoryManager::~CMemoryManager()
00076 {
00077 if (m_Blocks.empty())
00078 {
00079
00080 m_File << std::endl;
00081 m_File << "==================" << std::endl;
00082 m_File << " No leak detected " << std::endl;
00083 m_File << "==================" << std::endl;
00084 }
00085 else
00086 {
00087
00088 m_File << std::endl;
00089 m_File << "===============================" << std::endl;
00090 m_File << " Some leaks have been detected " << std::endl;
00091 m_File << "===============================" << std::endl;
00092 m_File << std::endl;
00093
00094 ReportLeaks();
00095 }
00096 }
00097
00098
00099 CMemoryManager& CMemoryManager::Instance()
00100 {
00101 static CMemoryManager sInst;
00102
00103 return sInst;
00104 }
00105
00106
00107 void CMemoryManager::ReportLeaks()
00108 {
00109
00110 size_t nTotalSize = 0;
00111 for (TBlockMap::iterator i = m_Blocks.begin(); i != m_Blocks.end(); ++i)
00112 {
00113
00114 nTotalSize += i->second.nSize;
00115
00116
00117 m_File << "-> 0x" << i->first
00118 << " | " << std::setw(7) << std::setfill(' ') << static_cast<int>(i->second.nSize) << " bytes"
00119 << " | " << i->second.File.Filename() << " (" << i->second.nLine << ")" << std::endl;
00120
00121
00122 free(i->first);
00123 }
00124
00125
00126 m_File << std::endl << std::endl << "-- "
00127 << static_cast<int>(m_Blocks.size()) << " Blocks not released, "
00128 << static_cast<int>(nTotalSize) << " bytes --"
00129 << std::endl;
00130 }
00131
00132
00133 void* CMemoryManager::Allocate(size_t nSize, const CFile& File, int nLine, bool bArray)
00134 {
00135
00136 void* pPtr = malloc(nSize);
00137
00138
00139 TBlock NewBlock;
00140 NewBlock.nSize = nSize;
00141 NewBlock.File = File;
00142 NewBlock.nLine = nLine;
00143 NewBlock.bArray = bArray;
00144 m_Blocks[pPtr] = NewBlock;
00145
00146
00147 m_File << "++ Allocation | 0x" << pPtr
00148 << " | " << std::setw(7) << std::setfill(' ') << static_cast<int>(NewBlock.nSize) << " bytes"
00149 << " | " << NewBlock.File.Filename() << " (" << NewBlock.nLine << ")" << std::endl;
00150
00151 return pPtr;
00152 }
00153
00154
00155 void CMemoryManager::Free(void* pPtr, bool bArray)
00156 {
00157
00158 TBlockMap::iterator It = m_Blocks.find(pPtr);
00159
00160
00161 if (It == m_Blocks.end())
00162 {
00163
00164
00165
00166 free(pPtr);
00167 return;
00168 }
00169
00170
00171 if (It->second.bArray != bArray)
00172 {
00173 throw CBadDelete(pPtr, It->second.File.Filename(), It->second.nLine, !bArray);
00174 }
00175
00176
00177 m_File << "-- Deallocation | 0x" << pPtr
00178 << " | " << std::setw(7) << std::setfill(' ') << static_cast<int>(It->second.nSize) << " bytes"
00179 << " | " << m_DeleteStack.top().File.Filename() << " (" << m_DeleteStack.top().nLine << ")" << std::endl;
00180 m_Blocks.erase(It);
00181 m_DeleteStack.pop();
00182
00183
00184 free(pPtr);
00185 }
00186
00187
00188 void CMemoryManager::NextDelete(const CFile& File, int nLine)
00189 {
00190 TBlock Delete;
00191 Delete.File = File;
00192 Delete.nLine = nLine;
00193
00194 m_DeleteStack.push(Delete);
00195 }
00196
00197 }