Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

186 lines
6.8 KiB

  1. // stb_leakcheck.h - v0.4 - quick & dirty malloc leak-checking - public domain
  2. // LICENSE
  3. //
  4. // See end of file.
  5. #ifdef STB_LEAKCHECK_IMPLEMENTATION
  6. #undef STB_LEAKCHECK_IMPLEMENTATION // don't implement more than once
  7. // if we've already included leakcheck before, undefine the macros
  8. #ifdef malloc
  9. #undef malloc
  10. #undef free
  11. #undef realloc
  12. #endif
  13. #include <assert.h>
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <stddef.h>
  18. typedef struct malloc_info stb_leakcheck_malloc_info;
  19. struct malloc_info
  20. {
  21. const char *file;
  22. int line;
  23. size_t size;
  24. stb_leakcheck_malloc_info *next,*prev;
  25. };
  26. static stb_leakcheck_malloc_info *mi_head;
  27. void *stb_leakcheck_malloc(size_t sz, const char *file, int line)
  28. {
  29. stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) malloc(sz + sizeof(*mi));
  30. if (mi == NULL) return mi;
  31. mi->file = file;
  32. mi->line = line;
  33. mi->next = mi_head;
  34. if (mi_head)
  35. mi->next->prev = mi;
  36. mi->prev = NULL;
  37. mi->size = (int) sz;
  38. mi_head = mi;
  39. return mi+1;
  40. }
  41. void stb_leakcheck_free(void *ptr)
  42. {
  43. if (ptr != NULL) {
  44. stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) ptr - 1;
  45. mi->size = ~mi->size;
  46. #ifndef STB_LEAKCHECK_SHOWALL
  47. if (mi->prev == NULL) {
  48. assert(mi_head == mi);
  49. mi_head = mi->next;
  50. } else
  51. mi->prev->next = mi->next;
  52. if (mi->next)
  53. mi->next->prev = mi->prev;
  54. #endif
  55. free(mi);
  56. }
  57. }
  58. void *stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line)
  59. {
  60. if (ptr == NULL) {
  61. return stb_leakcheck_malloc(sz, file, line);
  62. } else if (sz == 0) {
  63. stb_leakcheck_free(ptr);
  64. return NULL;
  65. } else {
  66. stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) ptr - 1;
  67. if (sz <= mi->size)
  68. return ptr;
  69. else {
  70. #ifdef STB_LEAKCHECK_REALLOC_PRESERVE_MALLOC_FILELINE
  71. void *q = stb_leakcheck_malloc(sz, mi->file, mi->line);
  72. #else
  73. void *q = stb_leakcheck_malloc(sz, file, line);
  74. #endif
  75. if (q) {
  76. memcpy(q, ptr, mi->size);
  77. stb_leakcheck_free(ptr);
  78. }
  79. return q;
  80. }
  81. }
  82. }
  83. static void stblkck_internal_print(const char *reason, const char *file, int line, size_t size, void *ptr)
  84. {
  85. #if (defined(_MSC_VER) && _MSC_VER < 1900) /* 1900=VS 2015 */ || defined(__MINGW32__)
  86. // Compilers that use the old MS C runtime library don't have %zd
  87. // and the older ones don't even have %lld either... however, the old compilers
  88. // without "long long" don't support 64-bit targets either, so here's the
  89. // compromise:
  90. #if defined(_MSC_VER) && _MSC_VER < 1400 // before VS 2005
  91. printf("%-6s: %s (%4d): %8d bytes at %p\n", reason, file, line, (int)size, ptr);
  92. #else
  93. printf("%-6s: %s (%4d): %8lld bytes at %p\n", reason, file, line, (long long)size, ptr);
  94. #endif
  95. #else
  96. // Assume we have %zd on other targets.
  97. printf("%-6s: %s (%4d): %zd bytes at %p\n", reason, file, line, size, ptr);
  98. #endif
  99. }
  100. void stb_leakcheck_dumpmem(void)
  101. {
  102. stb_leakcheck_malloc_info *mi = mi_head;
  103. while (mi) {
  104. if ((ptrdiff_t) mi->size >= 0)
  105. stblkck_internal_print("LEAKED", mi->file, mi->line, mi->size, mi+1);
  106. printf("LEAKED: %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) mi->size, mi+1);
  107. mi = mi->next;
  108. }
  109. #ifdef STB_LEAKCHECK_SHOWALL
  110. mi = mi_head;
  111. while (mi) {
  112. if ((ptrdiff_t) mi->size < 0)
  113. stblkck_internal_print("FREED", mi->file, mi->line, ~mi->size, mi+1);
  114. printf("FREED : %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) ~mi->size, mi+1);
  115. mi = mi->next;
  116. }
  117. #endif
  118. }
  119. #endif // STB_LEAKCHECK_IMPLEMENTATION
  120. #ifndef INCLUDE_STB_LEAKCHECK_H
  121. #define INCLUDE_STB_LEAKCHECK_H
  122. #define malloc(sz) stb_leakcheck_malloc(sz, __FILE__, __LINE__)
  123. #define free(p) stb_leakcheck_free(p)
  124. #define realloc(p,sz) stb_leakcheck_realloc(p,sz, __FILE__, __LINE__)
  125. extern void * stb_leakcheck_malloc(size_t sz, const char *file, int line);
  126. extern void * stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line);
  127. extern void stb_leakcheck_free(void *ptr);
  128. extern void stb_leakcheck_dumpmem(void);
  129. #endif // INCLUDE_STB_LEAKCHECK_H
  130. /*
  131. ------------------------------------------------------------------------------
  132. This software is available under 2 licenses -- choose whichever you prefer.
  133. ------------------------------------------------------------------------------
  134. ALTERNATIVE A - MIT License
  135. Copyright (c) 2017 Sean Barrett
  136. Permission is hereby granted, free of charge, to any person obtaining a copy of
  137. this software and associated documentation files (the "Software"), to deal in
  138. the Software without restriction, including without limitation the rights to
  139. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  140. of the Software, and to permit persons to whom the Software is furnished to do
  141. so, subject to the following conditions:
  142. The above copyright notice and this permission notice shall be included in all
  143. copies or substantial portions of the Software.
  144. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  145. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  146. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  147. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  148. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  149. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  150. SOFTWARE.
  151. ------------------------------------------------------------------------------
  152. ALTERNATIVE B - Public Domain (www.unlicense.org)
  153. This is free and unencumbered software released into the public domain.
  154. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  155. software, either in source code form or as a compiled binary, for any purpose,
  156. commercial or non-commercial, and by any means.
  157. In jurisdictions that recognize copyright laws, the author or authors of this
  158. software dedicate any and all copyright interest in the software to the public
  159. domain. We make this dedication for the benefit of the public at large and to
  160. the detriment of our heirs and successors. We intend this dedication to be an
  161. overt act of relinquishment in perpetuity of all present and future rights to
  162. this software under copyright law.
  163. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  164. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  165. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  166. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  167. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  168. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  169. ------------------------------------------------------------------------------
  170. */