123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- #ifdef STB_LEAKCHECK_IMPLEMENTATION
- #undef STB_LEAKCHECK_IMPLEMENTATION // don't implement more than once
- #ifdef malloc
- #undef malloc
- #undef free
- #undef realloc
- #endif
- #ifndef STB_LEAKCHECK_OUTPUT_PIPE
- #define STB_LEAKCHECK_OUTPUT_PIPE stdout
- #endif
- #include <assert.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <stddef.h>
- typedef struct malloc_info stb_leakcheck_malloc_info;
- struct malloc_info
- {
- const char *file;
- int line;
- size_t size;
- stb_leakcheck_malloc_info *next,*prev;
- };
- static stb_leakcheck_malloc_info *mi_head;
- void *stb_leakcheck_malloc(size_t sz, const char *file, int line)
- {
- stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) malloc(sz + sizeof(*mi));
- if (mi == NULL) return mi;
- mi->file = file;
- mi->line = line;
- mi->next = mi_head;
- if (mi_head)
- mi->next->prev = mi;
- mi->prev = NULL;
- mi->size = (int) sz;
- mi_head = mi;
- return mi+1;
- }
- void stb_leakcheck_free(void *ptr)
- {
- if (ptr != NULL) {
- stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) ptr - 1;
- mi->size = ~mi->size;
- #ifndef STB_LEAKCHECK_SHOWALL
- if (mi->prev == NULL) {
- assert(mi_head == mi);
- mi_head = mi->next;
- } else
- mi->prev->next = mi->next;
- if (mi->next)
- mi->next->prev = mi->prev;
- free(mi);
- #endif
- }
- }
- void *stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line)
- {
- if (ptr == NULL) {
- return stb_leakcheck_malloc(sz, file, line);
- } else if (sz == 0) {
- stb_leakcheck_free(ptr);
- return NULL;
- } else {
- stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) ptr - 1;
- if (sz <= mi->size)
- return ptr;
- else {
- #ifdef STB_LEAKCHECK_REALLOC_PRESERVE_MALLOC_FILELINE
- void *q = stb_leakcheck_malloc(sz, mi->file, mi->line);
- #else
- void *q = stb_leakcheck_malloc(sz, file, line);
- #endif
- if (q) {
- memcpy(q, ptr, mi->size);
- stb_leakcheck_free(ptr);
- }
- return q;
- }
- }
- }
- static void stblkck_internal_print(const char *reason, stb_leakcheck_malloc_info *mi)
- {
- #if defined(_MSC_VER) && _MSC_VER < 1900 // 1900=VS 2015
-
-
-
-
- #if _MSC_VER < 1400 // before VS 2005
- fprintf(STB_LEAKCHECK_OUTPUT_PIPE, "%s: %s (%4d): %8d bytes at %p\n", reason, mi->file, mi->line, (int)mi->size, (void*)(mi+1));
- #else
- 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));
- #endif
- #else
-
- #ifdef __MINGW32__
- __mingw_fprintf(STB_LEAKCHECK_OUTPUT_PIPE, "%s: %s (%4d): %zd bytes at %p\n", reason, mi->file, mi->line, mi->size, (void*)(mi+1));
- #else
- fprintf(STB_LEAKCHECK_OUTPUT_PIPE, "%s: %s (%4d): %zd bytes at %p\n", reason, mi->file, mi->line, mi->size, (void*)(mi+1));
- #endif
- #endif
- }
- void stb_leakcheck_dumpmem(void)
- {
- stb_leakcheck_malloc_info *mi = mi_head;
- while (mi) {
- if ((ptrdiff_t) mi->size >= 0)
- stblkck_internal_print("LEAKED", mi);
- mi = mi->next;
- }
- #ifdef STB_LEAKCHECK_SHOWALL
- mi = mi_head;
- while (mi) {
- if ((ptrdiff_t) mi->size < 0)
- stblkck_internal_print("FREED ", mi);
- mi = mi->next;
- }
- #endif
- }
- #endif // STB_LEAKCHECK_IMPLEMENTATION
- #if !defined(INCLUDE_STB_LEAKCHECK_H) || !defined(malloc)
- #define INCLUDE_STB_LEAKCHECK_H
- #include <stdlib.h> // we want to define the macros *after* stdlib to avoid a slew of errors
- #define malloc(sz) stb_leakcheck_malloc(sz, __FILE__, __LINE__)
- #define free(p) stb_leakcheck_free(p)
- #define realloc(p,sz) stb_leakcheck_realloc(p,sz, __FILE__, __LINE__)
- extern void * stb_leakcheck_malloc(size_t sz, const char *file, int line);
- extern void * stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line);
- extern void stb_leakcheck_free(void *ptr);
- extern void stb_leakcheck_dumpmem(void);
- #endif // INCLUDE_STB_LEAKCHECK_H
|