1 | /*
|
---|
2 | * \file memoryusageobserver.cpp
|
---|
3 | *
|
---|
4 | * This class represents a Singleton for observing memory usage.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "log.hpp"
|
---|
8 | #include "memoryusageobserver.hpp"
|
---|
9 | #include "verbose.hpp"
|
---|
10 |
|
---|
11 | MemoryUsageObserver* MemoryUsageObserver::instance = NULL;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Constructor. Do not use this function. Use getInstance() instead.
|
---|
15 | *
|
---|
16 | * \return memory usage observer instance
|
---|
17 | */
|
---|
18 | MemoryUsageObserver::MemoryUsageObserver() {
|
---|
19 | instance = NULL;
|
---|
20 | maximumSize = 0;
|
---|
21 | totalSize = 0;
|
---|
22 | }
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Destructor. Better use purgeInstance().
|
---|
26 | */
|
---|
27 | MemoryUsageObserver::~MemoryUsageObserver() {
|
---|
28 | while (!memoryUsers.empty()) {
|
---|
29 | map<void*, size_t>::iterator current = memoryUsers.begin();
|
---|
30 | free(current->first);
|
---|
31 | memoryUsers.erase(current);
|
---|
32 | }
|
---|
33 |
|
---|
34 | maximumSize = 0;
|
---|
35 | totalSize = 0;
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Returns the singleton memory usage observer instance.
|
---|
40 | *
|
---|
41 | * \return memory usage observer instance
|
---|
42 | */
|
---|
43 | MemoryUsageObserver* MemoryUsageObserver::getInstance() {
|
---|
44 | if (instance == NULL) {
|
---|
45 | instance = new MemoryUsageObserver;
|
---|
46 | }
|
---|
47 |
|
---|
48 | return instance;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Purges the current memory usage observer instance.
|
---|
53 | */
|
---|
54 | void MemoryUsageObserver::purgeInstance() {
|
---|
55 | if (instance != NULL) {
|
---|
56 | delete instance;
|
---|
57 | }
|
---|
58 |
|
---|
59 | instance = NULL;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Adds memory.
|
---|
64 | *
|
---|
65 | * \param pointer to the allocated piece of memory
|
---|
66 | * \param size of the allocated memory
|
---|
67 | */
|
---|
68 | void MemoryUsageObserver::addMemory(void* pointer, size_t size) {
|
---|
69 | // Memory might become reseized so we need to check whether the provided pointer is already tracked.
|
---|
70 | map<void*, size_t>::iterator current = memoryUsers.find(pointer);
|
---|
71 | if (current != memoryUsers.end()) {
|
---|
72 | totalSize -= current->second;
|
---|
73 | }
|
---|
74 |
|
---|
75 | memoryUsers[pointer] = size;
|
---|
76 | totalSize += size;
|
---|
77 | maximumSize = (totalSize > maximumSize) ? totalSize : maximumSize;
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Removes tracked memory. Prints a warning if untracked memory is to be released.
|
---|
82 | *
|
---|
83 | * \param pointer to the allocated piece of memory
|
---|
84 | * \param *msg optional error message
|
---|
85 | */
|
---|
86 | void MemoryUsageObserver::removeMemory(void* pointer, const char *msg) {
|
---|
87 | map<void*, size_t>::iterator current = memoryUsers.find(pointer);
|
---|
88 |
|
---|
89 | if (current == memoryUsers.end()) {
|
---|
90 | Log() << Verbose(0) << "WARNING: There is non-tracked memory to be freed. Pointer "
|
---|
91 | << pointer << " is not registered by MemoryUsageObserver: ";
|
---|
92 | if (msg != NULL)
|
---|
93 | Log() << Verbose(0) << *msg;
|
---|
94 | Log() << Verbose(0) << endl;
|
---|
95 | return;
|
---|
96 | }
|
---|
97 |
|
---|
98 | totalSize -= current->second;
|
---|
99 | memoryUsers.erase(current);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Gets the size of currently allocated memory.
|
---|
104 | */
|
---|
105 | size_t MemoryUsageObserver::getUsedMemorySize() {
|
---|
106 | return totalSize;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Gets the maximum size of allocated memory until now.
|
---|
111 | */
|
---|
112 | size_t MemoryUsageObserver::getMaximumUsedMemory() {
|
---|
113 | return maximumSize;
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Gets a map with pointers to the currently allocated memory ranges as keys and
|
---|
118 | * the allocated size as value.
|
---|
119 | */
|
---|
120 | map<void*, size_t> MemoryUsageObserver::getPointersToAllocatedMemory() {
|
---|
121 | return memoryUsers;
|
---|
122 | }
|
---|