stb_leakcheck.h 7.0 KB

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