From 501812f307cd4b7609a1c6226f26bb7780cf08b8 Mon Sep 17 00:00:00 2001 From: Fabian Giesen Date: Sat, 22 Jul 2017 18:03:52 -0700 Subject: [PATCH] stb_leakcheck: Fix warnings. 1. const char* for __FILE__ (string literals are const) 2. Use %zd to print size_t where available; the only real problem here is Visual C++. Use long long on the VC++ vers that support 64-bit targets but not %zd, int on the even older 32-bit-only VC++ vers that don't support "long long" either. Fixes #459. I think. (It's hard to be sure since the issue doesn't state the exact warning message.) --- stb_leakcheck.h | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/stb_leakcheck.h b/stb_leakcheck.h index 587c5e0..7322ab5 100644 --- a/stb_leakcheck.h +++ b/stb_leakcheck.h @@ -22,7 +22,7 @@ typedef struct malloc_info stb_leakcheck_malloc_info; struct malloc_info { - char *file; + const char *file; int line; size_t size; stb_leakcheck_malloc_info *next,*prev; @@ -30,7 +30,7 @@ struct malloc_info static stb_leakcheck_malloc_info *mi_head; -void *stb_leakcheck_malloc(size_t sz, char *file, int line) +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; @@ -62,7 +62,7 @@ void stb_leakcheck_free(void *ptr) } } -void *stb_leakcheck_realloc(void *ptr, size_t sz, char *file, int line) +void *stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line) { if (ptr == NULL) { return stb_leakcheck_malloc(sz, file, line); @@ -88,11 +88,30 @@ void *stb_leakcheck_realloc(void *ptr, size_t sz, char *file, int line) } } +static void stblkck_internal_print(const char *reason, const char *file, int line, size_t size, void *ptr) +{ +#if (defined(_MSC_VER) && _MSC_VER < 1900) /* 1900=VS 2015 */ || defined(__MINGW32__) + // Compilers that use the old MS C runtime library don't have %zd + // and the older ones don't even have %lld either... however, the old compilers + // without "long long" don't support 64-bit targets either, so here's the + // compromise: + #if defined(_MSC_VER) && _MSC_VER < 1400 // before VS 2005 + printf("%-6s: %s (%4d): %8d bytes at %p\n", reason, file, line, (int)size, ptr); + #else + printf("%-6s: %s (%4d): %8lld bytes at %p\n", reason, file, line, (long long)size, ptr); + #endif +#else + // Assume we have %zd on other targets. + printf("%-6s: %s (%4d): %zd bytes at %p\n", reason, file, line, size, ptr); +#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->file, mi->line, mi->size, mi+1); printf("LEAKED: %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) mi->size, mi+1); mi = mi->next; } @@ -100,6 +119,7 @@ void stb_leakcheck_dumpmem(void) mi = mi_head; while (mi) { if ((ptrdiff_t) mi->size < 0) + stblkck_internal_print("FREED", mi->file, mi->line, ~mi->size, mi+1); printf("FREED : %s (%4d): %8d bytes at %p\n", mi->file, mi->line, (int) ~mi->size, mi+1); mi = mi->next; }