cAudio 2.3.0
3d Audio Engine
Loading...
Searching...
No Matches
cMemoryTracker.cpp
1// Copyright (c) 2008-2011 Raynaldo (Wildicv) Rivera, Joshua (Dark_Kilauea) Jones, Murat (wolfmanfx) Sari
2// This file is part of the "cAudio Engine"
3// For conditions of distribution and use, see copyright notice in cAudio.h
4
5#include "cMemoryTracker.h"
6
7#if CAUDIO_USE_MEMORYTRACKER == 1
8
9namespace cAudio
10{
11
12cMemoryTracker::cMemoryTracker()
13{
14#if CAUDIO_MEMORYTRACKER_LOG_ALL_ALLOCATIONS == 1
15 outMemLog.open("cAudioMemoryLog.log");
16#endif
17}
18
19cMemoryTracker::~cMemoryTracker()
20{
21#if CAUDIO_MEMORYTRACKER_LOG_ALL_ALLOCATIONS == 1
22 outMemLog.close();
23#endif
24 DumpLeaks();
25}
26
27void cMemoryTracker::AddAllocation(void* pointer, size_t size, const char* filename, int line, const char* function)
28{
29 cAudioMutexBasicLock lock(Mutex);
30 cTrackedMemoryBlock block;
31 block.size = size;
32 block.filename = filename;
33 block.line = line;
34 block.function = function;
35 TrackedBlocks[pointer] = block;
36
37#if CAUDIO_MEMORYTRACKER_GENERATE_STATISTICS == 1
38 MemStats.CurrentAllocationBytes += size;
39 MemStats.TotalAllocationBytes += size;
40
41 MemStats.CurrentNumAllocations += 1;
42 MemStats.TotalNumAllocations += 1;
43
44 MemStats.AllocationHighWaterMark = MemStats.CurrentAllocationBytes > MemStats.AllocationHighWaterMark ? MemStats.CurrentAllocationBytes : MemStats.AllocationHighWaterMark;
45 MemStats.MaxNumAllocations = MemStats.CurrentNumAllocations > MemStats.MaxNumAllocations ? MemStats.CurrentNumAllocations : MemStats.MaxNumAllocations;
46#endif
47
48#if CAUDIO_MEMORYTRACKER_LOG_ALL_ALLOCATIONS == 1
49 if(outMemLog.good())
50 {
51 outMemLog << "Allocation at " << pointer << " with size " << size << " bytes in " << filename << " (Line: " << line << ") function: " << function << std::endl;
52 outMemLog.flush();
53 }
54#endif
55}
56
57void cMemoryTracker::RemoveAllocation(void* pointer)
58{
59 cAudioMutexBasicLock lock(Mutex);
60 std::map<void*, cTrackedMemoryBlock>::iterator it = TrackedBlocks.find(pointer);
61 if(it != TrackedBlocks.end())
62 {
63#if CAUDIO_MEMORYTRACKER_LOG_ALL_ALLOCATIONS == 1
64 if(outMemLog.good())
65 {
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;
67 outMemLog.flush();
68 }
69#endif
70
71#if CAUDIO_MEMORYTRACKER_GENERATE_STATISTICS == 1
72 size_t size = it->second.size;
73 MemStats.CurrentAllocationBytes -= size;
74 MemStats.CurrentNumAllocations -= 1;
75#endif
76 TrackedBlocks.erase(pointer);
77 }
78}
79
80void cMemoryTracker::DumpLeaks()
81{
82 cAudioMutexBasicLock lock(Mutex);
83
84 std::ofstream leakFile("cAudioMemoryLeaks.log");
85
86#if CAUDIO_MEMORYTRACKER_GENERATE_STATISTICS == 1
87 //Dump Statistics
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;
91
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;
96#endif
97
98 std::map<void*, cTrackedMemoryBlock>::iterator it;
99 for(it = TrackedBlocks.begin(); it != TrackedBlocks.end(); it++)
100 {
101 if(leakFile.good())
102 {
103 leakFile << "Address:" << it->first << " Size:" << it->second.size << " in " << it->second.filename << " (Line: " << it->second.line << ") function: " << it->second.function << std::endl;
104 leakFile.flush();
105 }
106 }
107
108 if(TrackedBlocks.size() == 0)
109 {
110 leakFile << "No Leaks Detected!" << std::endl;
111 leakFile.flush();
112 }
113
114 leakFile.close();
115}
116
117};
118
119#endif
120
Main namespace for the entire cAudio library.
Definition: cAudioCapture.h:16