stb_leakcheck.h 7.0 KB
Newer Older
S
Sean Barrett 已提交
1
// stb_leakcheck.h - v0.6 - quick & dirty malloc leak-checking - public domain
2 3
// LICENSE
//
4
//   See end of file.
S
Sean Barrett 已提交
5 6

#ifdef STB_LEAKCHECK_IMPLEMENTATION
L
Lukas Meller 已提交
7
#undef STB_LEAKCHECK_IMPLEMENTATION // don't implement more than once
S
Sean Barrett 已提交
8 9 10 11 12 13 14 15

// if we've already included leakcheck before, undefine the macros
#ifdef malloc
#undef malloc
#undef free
#undef realloc
#endif

16 17 18 19
#ifndef STB_LEAKCHECK_OUTPUT_PIPE
#define STB_LEAKCHECK_OUTPUT_PIPE stdout
#endif

20 21
#include <assert.h>
#include <string.h>
S
Sean Barrett 已提交
22 23 24 25 26 27 28
#include <stdlib.h>
#include <stdio.h>
#include <stddef.h>
typedef struct malloc_info stb_leakcheck_malloc_info;

struct malloc_info
{
F
Fabian Giesen 已提交
29
   const char *file;
S
Sean Barrett 已提交
30 31 32 33 34 35 36
   int line;
   size_t size;
   stb_leakcheck_malloc_info *next,*prev;
};

static stb_leakcheck_malloc_info *mi_head;

F
Fabian Giesen 已提交
37
void *stb_leakcheck_malloc(size_t sz, const char *file, int line)
S
Sean Barrett 已提交
38
{
S
Sean Barrett 已提交
39
   stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) malloc(sz + sizeof(*mi));
S
Sean Barrett 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
   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;
65
      free(mi);
C
Clownacy 已提交
66
      #endif
S
Sean Barrett 已提交
67 68 69
   }
}

F
Fabian Giesen 已提交
70
void *stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line)
S
Sean Barrett 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
{
   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;
      }
   }
}

96
static void stblkck_internal_print(const char *reason, stb_leakcheck_malloc_info *mi)
F
Fabian Giesen 已提交
97
{
C
Clownacy 已提交
98
#if defined(_MSC_VER) && _MSC_VER < 1900 // 1900=VS 2015
F
Fabian Giesen 已提交
99 100 101 102
   // 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:
C
Clownacy 已提交
103
   #if _MSC_VER < 1400 // before VS 2005
104
      fprintf(STB_LEAKCHECK_OUTPUT_PIPE, "%s: %s (%4d): %8d bytes at %p\n", reason, mi->file, mi->line, (int)mi->size, (void*)(mi+1));
F
Fabian Giesen 已提交
105
   #else
106
      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));
F
Fabian Giesen 已提交
107 108 109
   #endif
#else
   // Assume we have %zd on other targets.
C
Clownacy 已提交
110
   #ifdef __MINGW32__
111
      __mingw_fprintf(STB_LEAKCHECK_OUTPUT_PIPE, "%s: %s (%4d): %zd bytes at %p\n", reason, mi->file, mi->line, mi->size, (void*)(mi+1));
C
Clownacy 已提交
112
   #else
113
      fprintf(STB_LEAKCHECK_OUTPUT_PIPE, "%s: %s (%4d): %zd bytes at %p\n", reason, mi->file, mi->line, mi->size, (void*)(mi+1));
C
Clownacy 已提交
114
   #endif
F
Fabian Giesen 已提交
115 116 117
#endif
}

S
Sean Barrett 已提交
118 119 120 121 122
void stb_leakcheck_dumpmem(void)
{
   stb_leakcheck_malloc_info *mi = mi_head;
   while (mi) {
      if ((ptrdiff_t) mi->size >= 0)
123
         stblkck_internal_print("LEAKED", mi);
S
Sean Barrett 已提交
124 125 126 127 128 129
      mi = mi->next;
   }
   #ifdef STB_LEAKCHECK_SHOWALL
   mi = mi_head;
   while (mi) {
      if ((ptrdiff_t) mi->size < 0)
130
         stblkck_internal_print("FREED ", mi);
S
Sean Barrett 已提交
131 132 133 134 135 136
      mi = mi->next;
   }
   #endif
}
#endif // STB_LEAKCHECK_IMPLEMENTATION

S
Sean Barrett 已提交
137
#if !defined(INCLUDE_STB_LEAKCHECK_H) || !defined(malloc)
S
Sean Barrett 已提交
138 139
#define INCLUDE_STB_LEAKCHECK_H

C
Clownacy 已提交
140 141
#include <stdlib.h> // we want to define the macros *after* stdlib to avoid a slew of errors

S
Sean Barrett 已提交
142 143 144 145
#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__)

S
Sean Barrett 已提交
146 147
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);
S
Sean Barrett 已提交
148 149 150 151
extern void   stb_leakcheck_free(void *ptr);
extern void   stb_leakcheck_dumpmem(void);

#endif // INCLUDE_STB_LEAKCHECK_H
152 153 154 155 156 157 158 159


/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Sean Barrett
S
Sean Barrett 已提交
160 161 162 163 164
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
165
so, subject to the following conditions:
S
Sean Barrett 已提交
166
The above copyright notice and this permission notice shall be included in all
167
copies or substantial portions of the Software.
S
Sean Barrett 已提交
168 169 170 171 172 173
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
174 175 176 177
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
S
Sean Barrett 已提交
178 179
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
180
commercial or non-commercial, and by any means.
S
Sean Barrett 已提交
181 182 183 184 185
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
186
this software under copyright law.
S
Sean Barrett 已提交
187 188 189 190 191
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
192 193 194
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/