quick & dirty leak checker
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parent ec21583 · Browse files · 4 files·+150 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+150 added−0 removed
- tests:+26 added−0 removed
- other:+124 added−0 removed
Provenance
- Commit
0d840ab- Tree
e2354b3- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
ec21583
4 files changed+150 added−0 removed
modified:tests/stb.dsp+8 added
120 unmodified lines121 SOURCE=..\stb_image_write.h122 # End Source File123 # Begin Source File124 125+SOURCE=..\stb_leakcheck.h126+# End Source File127+# Begin Source File128+129 SOURCE=..\stb_perlin.h130 # End Source File131 # Begin Source File132 32 unmodified lines165 SOURCE=.\test_truetype.c166 # End Source File167 # Begin Source File168 169+SOURCE=.\test_vorbis.c170+# End Source File171+# Begin Source File172+173 SOURCE=.\textedit_sample.c174 # End Source File175 # End Target176 # End Projectadded:tests/test_vorbis.c+18 added
1+#define STB_VORBIS_HEADER_ONLY2+#include "stb_vorbis.c"3+#include "stb.h"4+5+extern void stb_vorbis_dumpmem(void);6+7+#ifdef VORBIS_TEST8+int main(int argc, char **argv)9+{10+ size_t memlen;11+ unsigned char *mem = stb_fileu("c:/x/01.ogg", &memlen);12+ int chan, samplerate;13+ short *output;14+ int samples = stb_vorbis_decode_memory(mem, memlen, &chan, &samplerate, &output);15+ stb_vorbis_dumpmem();16+ return 0;17+}18+#endifadded:stb_leakcheck.h+117 added
1+// stb_leakcheck.h - v0.1 - quick & dirty malloc leak-checking - public domain2+3+#ifdef STB_LEAKCHECK_IMPLEMENTATION4+#undef STB_LEAKCHECK_IMPLEMENTATION // don't implenment more than once5+6+// if we've already included leakcheck before, undefine the macros7+#ifdef malloc8+#undef malloc9+#undef free10+#undef realloc11+#endif12+13+#include <stdlib.h>14+#include <stdio.h>15+#include <stddef.h>16+typedef struct malloc_info stb_leakcheck_malloc_info;17+18+struct malloc_info19+{20+ char *file;21+ int line;22+ size_t size;23+ stb_leakcheck_malloc_info *next,*prev;24+};25+26+static stb_leakcheck_malloc_info *mi_head;27+28+void *stb_leakcheck_malloc(size_t sz, char *file, int line)29+{30+ stb_leakcheck_malloc_info *mi = malloc(sz + sizeof(*mi));31+ if (mi == NULL) return mi;32+ mi->file = file;33+ mi->line = line;34+ mi->next = mi_head;35+ if (mi_head)36+ mi->next->prev = mi;37+ mi->prev = NULL;38+ mi->size = (int) sz;39+ mi_head = mi;40+ return mi+1;41+}42+43+void stb_leakcheck_free(void *ptr)44+{45+ if (ptr != NULL) {46+ stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) ptr - 1;47+ mi->size = ~mi->size;48+ #ifndef STB_LEAKCHECK_SHOWALL49+ if (mi->prev == NULL) {50+ assert(mi_head == mi);51+ mi_head = mi->next;52+ } else53+ mi->prev->next = mi->next;54+ if (mi->next)55+ mi->next->prev = mi->prev;56+ #endif57+ }58+}59+60+void *stb_leakcheck_realloc(void *ptr, size_t sz, char *file, int line)61+{62+ if (ptr == NULL) {63+ return stb_leakcheck_malloc(sz, file, line);64+ } else if (sz == 0) {65+ stb_leakcheck_free(ptr);66+ return NULL;67+ } else {68+ stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) ptr - 1;69+ if (sz <= mi->size)70+ return ptr;71+ else {72+ #ifdef STB_LEAKCHECK_REALLOC_PRESERVE_MALLOC_FILELINE73+ void *q = stb_leakcheck_malloc(sz, mi->file, mi->line);74+ #else75+ void *q = stb_leakcheck_malloc(sz, file, line);76+ #endif77+ if (q) {78+ memcpy(q, ptr, mi->size);79+ stb_leakcheck_free(ptr);80+ }81+ return q;82+ }83+ }84+}85+86+void stb_leakcheck_dumpmem(void)87+{88+ stb_leakcheck_malloc_info *mi = mi_head;89+ while (mi) {90+ if ((ptrdiff_t) mi->size >= 0)91+ printf("LEAKED: %s (%4d): %8z bytes at %p\n", mi->file, mi->line, mi->size, mi+1);92+ mi = mi->next;93+ }94+ #ifdef STB_LEAKCHECK_SHOWALL95+ mi = mi_head;96+ while (mi) {97+ if ((ptrdiff_t) mi->size < 0)98+ printf("FREED : %s (%4d): %8z bytes at %p\n", mi->file, mi->line, ~mi->size, mi+1);99+ mi = mi->next;100+ }101+ #endif102+}103+#endif // STB_LEAKCHECK_IMPLEMENTATION104+105+#ifndef INCLUDE_STB_LEAKCHECK_H106+#define INCLUDE_STB_LEAKCHECK_H107+108+#define malloc(sz) stb_leakcheck_malloc(sz, __FILE__, __LINE__)109+#define free(p) stb_leakcheck_free(p)110+#define realloc(p,sz) stb_leakcheck_realloc(p,sz, __FILE__, __LINE__)111+112+extern void * stb_leakcheck_malloc(size_t sz, char *file, int line);113+extern void * stb_leakcheck_realloc(void *ptr, size_t sz, char *file, int line);114+extern void stb_leakcheck_free(void *ptr);115+extern void stb_leakcheck_dumpmem(void);116+117+#endif // INCLUDE_STB_LEAKCHECK_Hmodified:stb_vorbis.c+7 added
325 unmodified lines326 // samples in the file, returns 0.327 328 #endif329 330+extern void stb_vorbis_free(void *p);331+332 //////// ERROR CODES333 334 enum STBVorbisError335 {257 unmodified lines593 #else594 typedef uint16 codetype;595 #endif596 597+//#define STB_LEAKCHECK_IMPLEMENTATION598+//#define STB_LEAKCHECK_SHOWALL599+//#include "stb_leakcheck.h"600+//void stb_vorbis_dumpmem(void) { stb_leakcheck_dumpmem(); }601+602 // @NOTE603 //604 // Some arrays below are tagged "//varies", which means it's actually605 // a variable-sized piece of data, but rather than malloc I assume it's4845 unmodified lines