Debug.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated January 1, 2020. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2020, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #ifndef SPINE_DEBUG_H
  30. #define SPINE_DEBUG_H
  31. #include <spine/extension.h>
  32. #include <map>
  33. namespace spine {
  34. class SP_API DebugExtension : public SpineExtension {
  35. struct Allocation {
  36. void *address;
  37. size_t size;
  38. const char *fileName;
  39. int line;
  40. Allocation() : address(NULL), size(0), fileName(NULL), line(0) {
  41. }
  42. Allocation(void *a, size_t s, const char *f, int l) : address(a), size(s), fileName(f), line(l) {
  43. }
  44. };
  45. public:
  46. DebugExtension(SpineExtension* extension): _extension(extension), _allocations(0), _reallocations(0), _frees(0) {
  47. }
  48. void reportLeaks() {
  49. for (std::map<void*, Allocation>::iterator it = _allocated.begin(); it != _allocated.end(); it++) {
  50. printf("\"%s:%i (%zu bytes at %p)\n", it->second.fileName, it->second.line, it->second.size, it->second.address);
  51. }
  52. printf("allocations: %zu, reallocations: %zu, frees: %zu\n", _allocations, _reallocations, _frees);
  53. if (_allocated.empty()) printf("No leaks detected");
  54. }
  55. void clearAllocations() {
  56. _allocated.clear();
  57. _usedMemory = 0;
  58. }
  59. virtual void *_alloc(size_t size, const char *file, int line) {
  60. void *result = _extension->_alloc(size, file, line);
  61. _allocated[result] = Allocation(result, size, file, line);
  62. _allocations++;
  63. _usedMemory += size;
  64. return result;
  65. }
  66. virtual void *_calloc(size_t size, const char *file, int line) {
  67. void *result = _extension->_calloc(size, file, line);
  68. _allocated[result] = Allocation(result, size, file, line);
  69. _allocations++;
  70. _usedMemory += size;
  71. return result;
  72. }
  73. virtual void *_realloc(void *ptr, size_t size, const char *file, int line) {
  74. if (_allocated.count(ptr)) _usedMemory -= _allocated[ptr].size;
  75. _allocated.erase(ptr);
  76. void *result = _extension->_realloc(ptr, size, file, line);
  77. _reallocations++;
  78. _allocated[result] = Allocation(result, size, file, line);
  79. _usedMemory += size;
  80. return result;
  81. }
  82. virtual void _free(void *mem, const char *file, int line) {
  83. if (_allocated.count(mem)) {
  84. _extension->_free(mem, file, line);
  85. _frees++;
  86. _usedMemory -= _allocated[mem].size;
  87. _allocated.erase(mem);
  88. return;
  89. }
  90. printf("%s:%i (address %p): Double free or not allocated through SpineExtension\n", file, line, mem);
  91. _extension->_free(mem, file, line);
  92. }
  93. virtual char *_readFile(const String &path, int *length) {
  94. return _extension->_readFile(path, length);
  95. }
  96. size_t getUsedMemory() {
  97. return _usedMemory;
  98. }
  99. private:
  100. SpineExtension* _extension;
  101. std::map<void*, Allocation> _allocated;
  102. size_t _allocations;
  103. size_t _reallocations;
  104. size_t _frees;
  105. size_t _usedMemory;
  106. };
  107. }
  108. #endif //SPINE_DEBUG_H