1 | /*
|
---|
2 | * \file memoryusageobserver.cpp
|
---|
3 | *
|
---|
4 | * This class represents a Singleton for observing memory usage.
|
---|
5 | */
|
---|
6 | #include "memoryusageobserver.hpp"
|
---|
7 |
|
---|
8 | MemoryUsageObserver* MemoryUsageObserver::instance = NULL;
|
---|
9 |
|
---|
10 | /**
|
---|
11 | * Constructor. Do not use this function. Use getInstance() instead.
|
---|
12 | *
|
---|
13 | * \return memory usage observer instance
|
---|
14 | */
|
---|
15 | MemoryUsageObserver::MemoryUsageObserver() {
|
---|
16 | instance = NULL;
|
---|
17 | }
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * Destructor. Better use purgeInstance().
|
---|
21 | */
|
---|
22 | MemoryUsageObserver::~MemoryUsageObserver() {
|
---|
23 | for (map<void*, size_t>::iterator current = memoryUsers.begin(); current != memoryUsers.end(); current++) {
|
---|
24 | memoryUsers.erase(current);
|
---|
25 | }
|
---|
26 | }
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Returns the singleton memory usage observer instance.
|
---|
30 | *
|
---|
31 | * \return memory usage observer instance
|
---|
32 | */
|
---|
33 | MemoryUsageObserver* MemoryUsageObserver::getInstance() {
|
---|
34 | if (instance == NULL) {
|
---|
35 | instance = new MemoryUsageObserver;
|
---|
36 | }
|
---|
37 |
|
---|
38 | return instance;
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Purges the current memory usage observer instance.
|
---|
43 | */
|
---|
44 | void MemoryUsageObserver::purgeInstance() {
|
---|
45 | if (instance != NULL) {
|
---|
46 | delete instance;
|
---|
47 | }
|
---|
48 |
|
---|
49 | instance = NULL;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Adds memory.
|
---|
54 | *
|
---|
55 | * \param pointer to the allocated piece of memory
|
---|
56 | * \param size of the allocated memory
|
---|
57 | */
|
---|
58 | void MemoryUsageObserver::addMemory(void* pointer, size_t size) {
|
---|
59 | memoryUsers[pointer] = size;
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Removes tracked memory. Prints a warning if untracked memory is to be released.
|
---|
64 | *
|
---|
65 | * \param pointer to the allocated piece of memory
|
---|
66 | */
|
---|
67 | void MemoryUsageObserver::removeMemory(void* pointer) {
|
---|
68 | map<void*, size_t>::iterator current = memoryUsers.find(pointer);
|
---|
69 |
|
---|
70 | if (current == memoryUsers.end()) {
|
---|
71 | cout << "WARNING: There is non-tracked memory to be freed. Pointer "
|
---|
72 | << pointer << " is not registered by MemoryUsageObserver." << endl;
|
---|
73 | return;
|
---|
74 | }
|
---|
75 |
|
---|
76 | memoryUsers.erase(current);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Gets the size of currently allocated memory.
|
---|
81 | */
|
---|
82 | size_t MemoryUsageObserver::getUsedMemorySize() {
|
---|
83 | size_t totalSize = 0;
|
---|
84 |
|
---|
85 | for (map<void*, size_t>::iterator current = memoryUsers.begin(); current != memoryUsers.end(); current++) {
|
---|
86 | totalSize += current->second;
|
---|
87 | }
|
---|
88 |
|
---|
89 | return totalSize;
|
---|
90 | }
|
---|