5 #include "cMemoryTracker.h" 7 #if CAUDIO_USE_MEMORYTRACKER == 1 12 cMemoryTracker::cMemoryTracker()
14 #if CAUDIO_MEMORYTRACKER_LOG_ALL_ALLOCATIONS == 1 15 outMemLog.open(
"cAudioMemoryLog.log");
19 cMemoryTracker::~cMemoryTracker()
21 #if CAUDIO_MEMORYTRACKER_LOG_ALL_ALLOCATIONS == 1 27 void cMemoryTracker::AddAllocation(
void* pointer,
size_t size,
const char* filename,
int line,
const char*
function)
29 cAudioMutexBasicLock lock(Mutex);
30 cTrackedMemoryBlock block;
32 block.filename = filename;
34 block.function =
function;
35 TrackedBlocks[pointer] = block;
37 #if CAUDIO_MEMORYTRACKER_GENERATE_STATISTICS == 1 38 MemStats.CurrentAllocationBytes += size;
39 MemStats.TotalAllocationBytes += size;
41 MemStats.CurrentNumAllocations += 1;
42 MemStats.TotalNumAllocations += 1;
44 MemStats.AllocationHighWaterMark = MemStats.CurrentAllocationBytes > MemStats.AllocationHighWaterMark ? MemStats.CurrentAllocationBytes : MemStats.AllocationHighWaterMark;
45 MemStats.MaxNumAllocations = MemStats.CurrentNumAllocations > MemStats.MaxNumAllocations ? MemStats.CurrentNumAllocations : MemStats.MaxNumAllocations;
48 #if CAUDIO_MEMORYTRACKER_LOG_ALL_ALLOCATIONS == 1 51 outMemLog <<
"Allocation at " << pointer <<
" with size " << size <<
" bytes in " << filename <<
" (Line: " << line <<
") function: " <<
function << std::endl;
57 void cMemoryTracker::RemoveAllocation(
void* pointer)
59 cAudioMutexBasicLock lock(Mutex);
60 std::map<void*, cTrackedMemoryBlock>::iterator it = TrackedBlocks.find(pointer);
61 if(it != TrackedBlocks.end())
63 #if CAUDIO_MEMORYTRACKER_LOG_ALL_ALLOCATIONS == 1 66 outMemLog <<
"Deallocation of address " << pointer <<
" with size " << it->second.size <<
" bytes in " << it->second.filename <<
" (Line: " << it->second.line <<
") function: " << it->second.function << std::endl;
71 #if CAUDIO_MEMORYTRACKER_GENERATE_STATISTICS == 1 72 size_t size = it->second.size;
73 MemStats.CurrentAllocationBytes -= size;
74 MemStats.CurrentNumAllocations -= 1;
76 TrackedBlocks.erase(pointer);
80 void cMemoryTracker::DumpLeaks()
82 cAudioMutexBasicLock lock(Mutex);
84 std::ofstream leakFile(
"cAudioMemoryLeaks.log");
86 #if CAUDIO_MEMORYTRACKER_GENERATE_STATISTICS == 1 88 leakFile <<
"Highest Amount of Allocated Memory: " << MemStats.AllocationHighWaterMark <<
" bytes." << std::endl;
89 leakFile <<
"Memory Allocated at Shutdown: " << MemStats.CurrentAllocationBytes <<
" bytes." << std::endl;
90 leakFile <<
"Total Allocated Memory: " << MemStats.TotalAllocationBytes <<
" bytes." << std::endl;
92 leakFile <<
"Highest Number of Allocated Objects: " << MemStats.MaxNumAllocations << std::endl;
93 leakFile <<
"Objects Allocated at Shutdown: " << MemStats.CurrentNumAllocations << std::endl;
94 leakFile <<
"Total Objects Allocated: " << MemStats.TotalNumAllocations << std::endl;
95 leakFile << std::endl;
98 std::map<void*, cTrackedMemoryBlock>::iterator it;
99 for(it = TrackedBlocks.begin(); it != TrackedBlocks.end(); it++)
103 leakFile <<
"Address:" << it->first <<
" Size:" << it->second.size <<
" in " << it->second.filename <<
" (Line: " << it->second.line <<
") function: " << it->second.function << std::endl;
108 if(TrackedBlocks.size() == 0)
110 leakFile <<
"No Leaks Detected!" << std::endl;
Main namespace for the entire cAudio library.