From c545d3cd4bf91a9af7e198f7f9641d94efce61f4 Mon Sep 17 00:00:00 2001 From: badlogic Date: Thu, 24 May 2018 15:48:11 +0200 Subject: [PATCH] Using std::map in debug memory tracker. --- spine-cpp/spine-cpp/include/spine/Debug.h | 40 ++++++++-------------- spine-cpp/spine-cpp/include/spine/Vector.h | 20 ----------- 2 files changed, 14 insertions(+), 46 deletions(-) diff --git a/spine-cpp/spine-cpp/include/spine/Debug.h b/spine-cpp/spine-cpp/include/spine/Debug.h index a444a40c7..7c5a63d5e 100644 --- a/spine-cpp/spine-cpp/include/spine/Debug.h +++ b/spine-cpp/spine-cpp/include/spine/Debug.h @@ -33,7 +33,7 @@ #include -#include +#include namespace Spine { class DebugExtension : public DefaultSpineExtension { @@ -55,58 +55,46 @@ public: } void reportLeaks() { - for (std::vector::iterator it = _allocated.begin(); it != _allocated.end(); it++) { - printf("\"%s:%i (%zu bytes at %p)\n", it->fileName, it->line, it->size, it->address); + for (auto it = _allocated.begin(); it != _allocated.end(); it++) { + 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); if (_allocated.empty()) printf("No leaks detected"); } void clearAllocations() { - _allocated.resize(0); + _allocated.clear(); } protected: virtual void *_alloc(size_t size, const char *file, int line) { void *result = DefaultSpineExtension::_alloc(size, file, line); - _allocated.push_back(Allocation(result, size, file, line)); + _allocated[result] = Allocation(result, size, file, line); _allocations++; return result; } virtual void *_calloc(size_t size, const char *file, int line) { void *result = DefaultSpineExtension::_calloc(size, file, line); - _allocated.push_back(Allocation(result, size, file, line)); + _allocated[result] = Allocation(result, size, file, line); _allocations++; return result; } virtual void *_realloc(void *ptr, size_t size, const char *file, int line) { + _allocated.erase(ptr); void *result = DefaultSpineExtension::_realloc(ptr, size, file, line); _reallocations++; - - for (std::vector::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)); + _allocated[result] = Allocation(result, size, file, line); return result; } virtual void _free(void *mem, const char *file, int line) { - for (std::vector::iterator it = _allocated.begin(); it != _allocated.end(); it++) { - if (it->address == mem) { - DefaultSpineExtension::_free(mem, file, line); - _frees++; - _allocated.erase(it); - return; - } + if (_allocated.count(mem)) { + DefaultSpineExtension::_free(mem, file, line); + _frees++; + _allocated.erase(mem); + return; } printf("%s:%i (address %p): Double free or not allocated through SpineExtension\n", file, line, mem); @@ -114,7 +102,7 @@ protected: } private: - std::vector _allocated; + std::map _allocated; size_t _allocations; size_t _reallocations; size_t _frees; diff --git a/spine-cpp/spine-cpp/include/spine/Vector.h b/spine-cpp/spine-cpp/include/spine/Vector.h index 53a8343d2..cec380478 100644 --- a/spine-cpp/spine-cpp/include/spine/Vector.h +++ b/spine-cpp/spine-cpp/include/spine/Vector.h @@ -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() { clear(); deallocate(_buffer);