Using std::map in debug memory tracker.

This commit is contained in:
badlogic 2018-05-24 15:48:11 +02:00
parent 265b18571c
commit c545d3cd4b
2 changed files with 14 additions and 46 deletions

View File

@ -33,7 +33,7 @@
#include <spine/Extension.h> #include <spine/Extension.h>
#include <vector> #include <map>
namespace Spine { namespace Spine {
class DebugExtension : public DefaultSpineExtension { class DebugExtension : public DefaultSpineExtension {
@ -55,58 +55,46 @@ public:
} }
void reportLeaks() { void reportLeaks() {
for (std::vector<Allocation>::iterator it = _allocated.begin(); it != _allocated.end(); it++) { for (auto it = _allocated.begin(); it != _allocated.end(); it++) {
printf("\"%s:%i (%zu bytes at %p)\n", it->fileName, it->line, it->size, it->address); printf("\"%s:%i (%zu bytes at %p)\n", it->second.fileName, it->second.line, it->second.size, it->second.address);
} }
printf("allocations: %lu, reallocations: %lu, frees: %lu\n", _allocations, _reallocations, _frees); printf("allocations: %lu, reallocations: %lu, frees: %lu\n", _allocations, _reallocations, _frees);
if (_allocated.empty()) printf("No leaks detected"); if (_allocated.empty()) printf("No leaks detected");
} }
void clearAllocations() { void clearAllocations() {
_allocated.resize(0); _allocated.clear();
} }
protected: protected:
virtual void *_alloc(size_t size, const char *file, int line) { virtual void *_alloc(size_t size, const char *file, int line) {
void *result = DefaultSpineExtension::_alloc(size, file, line); void *result = DefaultSpineExtension::_alloc(size, file, line);
_allocated.push_back(Allocation(result, size, file, line)); _allocated[result] = Allocation(result, size, file, line);
_allocations++; _allocations++;
return result; return result;
} }
virtual void *_calloc(size_t size, const char *file, int line) { virtual void *_calloc(size_t size, const char *file, int line) {
void *result = DefaultSpineExtension::_calloc(size, file, line); void *result = DefaultSpineExtension::_calloc(size, file, line);
_allocated.push_back(Allocation(result, size, file, line)); _allocated[result] = Allocation(result, size, file, line);
_allocations++; _allocations++;
return result; return result;
} }
virtual void *_realloc(void *ptr, size_t size, const char *file, int line) { virtual void *_realloc(void *ptr, size_t size, const char *file, int line) {
_allocated.erase(ptr);
void *result = DefaultSpineExtension::_realloc(ptr, size, file, line); void *result = DefaultSpineExtension::_realloc(ptr, size, file, line);
_reallocations++; _reallocations++;
_allocated[result] = Allocation(result, size, file, line);
for (std::vector<Allocation>::iterator it = _allocated.begin(); it != _allocated.end(); it++) {
if (it->address == ptr) {
it->address = result;
it->size = size;
it->fileName = file;
it->line = line;
return result;
}
}
_allocated.push_back(Allocation(result, size, file, line));
return result; return result;
} }
virtual void _free(void *mem, const char *file, int line) { virtual void _free(void *mem, const char *file, int line) {
for (std::vector<Allocation>::iterator it = _allocated.begin(); it != _allocated.end(); it++) { if (_allocated.count(mem)) {
if (it->address == mem) { DefaultSpineExtension::_free(mem, file, line);
DefaultSpineExtension::_free(mem, file, line); _frees++;
_frees++; _allocated.erase(mem);
_allocated.erase(it); return;
return;
}
} }
printf("%s:%i (address %p): Double free or not allocated through SpineExtension\n", file, line, mem); printf("%s:%i (address %p): Double free or not allocated through SpineExtension\n", file, line, mem);
@ -114,7 +102,7 @@ protected:
} }
private: private:
std::vector<Allocation> _allocated; std::map<void*, Allocation> _allocated;
size_t _allocations; size_t _allocations;
size_t _reallocations; size_t _reallocations;
size_t _frees; size_t _frees;

View File

@ -54,26 +54,6 @@ public:
} }
} }
/*Vector& operator=(const Vector& inVector) {
if (this != &inVector) {
clear();
deallocate(_buffer);
_buffer = NULL;
_size = inVector._size;
_capacity = inVector._capacity;
if (_capacity > 0) {
_buffer = allocate(_capacity);
for (size_t i = 0; i < _size; ++i) {
construct(_buffer + i, inVector._buffer[i]);
}
}
}
return *this;
}*/
~Vector() { ~Vector() {
clear(); clear();
deallocate(_buffer); deallocate(_buffer);