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