Skip to content
Myr

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

4 files+150 added−0 removed

modified:tests/stb.dsp+8 added
120 unmodified lines
121SOURCE=..\stb_image_write.h122# End Source File123# Begin Source File124125SOURCE=..\stb_leakcheck.h126# End Source File127# Begin Source File128129SOURCE=..\stb_perlin.h130# End Source File131# Begin Source File132
32 unmodified lines
165SOURCE=.\test_truetype.c166# End Source File167# Begin Source File168169SOURCE=.\test_vorbis.c170# End Source File171# Begin Source File172173SOURCE=.\textedit_sample.c174# End Source File175# End Target176# End Project
added:tests/test_vorbis.c+18 added
1#define STB_VORBIS_HEADER_ONLY2#include "stb_vorbis.c"3#include "stb.h"45extern void stb_vorbis_dumpmem(void);67#ifdef VORBIS_TEST8int 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#endif
added:stb_leakcheck.h+117 added
1// stb_leakcheck.h - v0.1 - quick & dirty malloc leak-checking - public domain23#ifdef STB_LEAKCHECK_IMPLEMENTATION4#undef STB_LEAKCHECK_IMPLEMENTATION // don't implenment more than once56// if we've already included leakcheck before, undefine the macros7#ifdef malloc8#undef malloc9#undef free10#undef realloc11#endif1213#include <stdlib.h>14#include <stdio.h>15#include <stddef.h>16typedef struct malloc_info stb_leakcheck_malloc_info;1718struct malloc_info19{20 char *file;21 int line;22 size_t size;23 stb_leakcheck_malloc_info *next,*prev;24};2526static stb_leakcheck_malloc_info *mi_head;2728void *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}4243void 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}5960void *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}8586void 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_IMPLEMENTATION104105#ifndef INCLUDE_STB_LEAKCHECK_H106#define INCLUDE_STB_LEAKCHECK_H107108#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__)111112extern void * stb_leakcheck_malloc(size_t sz, char *file, int line);113extern void * stb_leakcheck_realloc(void *ptr, size_t sz, char *file, int line);114extern void stb_leakcheck_free(void *ptr);115extern void stb_leakcheck_dumpmem(void);116117#endif // INCLUDE_STB_LEAKCHECK_H
modified:stb_vorbis.c+7 added
325 unmodified lines
326// samples in the file, returns 0.327328#endif329330extern void stb_vorbis_free(void *p);331332//////// ERROR CODES333334enum STBVorbisError335{
257 unmodified lines
593#else594typedef uint16 codetype;595#endif596597//#define STB_LEAKCHECK_IMPLEMENTATION598//#define STB_LEAKCHECK_SHOWALL599//#include "stb_leakcheck.h"600//void stb_vorbis_dumpmem(void) { stb_leakcheck_dumpmem(); }601602// @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's
4845 unmodified lines