1 // stb_leakcheck.h - v0.5 - quick & dirty malloc leak-checking - public domain
2 // LICENSE
3 //
4 //   See end of file.
5 
6 #ifdef STB_LEAKCHECK_IMPLEMENTATION
7 #undef STB_LEAKCHECK_IMPLEMENTATION // don't implement more than once
8 
9 // if we've already included leakcheck before, undefine the macros
10 #ifdef malloc
11 #undef malloc
12 #undef free
13 #undef realloc
14 #endif
15 
16 #include <assert.h>
17 #include <string.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <stddef.h>
21 typedef struct malloc_info stb_leakcheck_malloc_info;
22 
23 struct malloc_info
24 {
25    const char *file;
26    int line;
27    size_t size;
28    stb_leakcheck_malloc_info *next,*prev;
29 };
30 
31 static stb_leakcheck_malloc_info *mi_head;
32 
stb_leakcheck_malloc(size_t sz,const char * file,int line)33 void *stb_leakcheck_malloc(size_t sz, const char *file, int line)
34 {
35    stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) malloc(sz + sizeof(*mi));
36    if (mi == NULL) return mi;
37    mi->file = file;
38    mi->line = line;
39    mi->next = mi_head;
40    if (mi_head)
41       mi->next->prev = mi;
42    mi->prev = NULL;
43    mi->size = (int) sz;
44    mi_head = mi;
45    return mi+1;
46 }
47 
stb_leakcheck_free(void * ptr)48 void stb_leakcheck_free(void *ptr)
49 {
50    if (ptr != NULL) {
51       stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) ptr - 1;
52       mi->size = ~mi->size;
53       #ifndef STB_LEAKCHECK_SHOWALL
54       if (mi->prev == NULL) {
55          assert(mi_head == mi);
56          mi_head = mi->next;
57       } else
58          mi->prev->next = mi->next;
59       if (mi->next)
60          mi->next->prev = mi->prev;
61       free(mi);
62       #endif
63    }
64 }
65 
stb_leakcheck_realloc(void * ptr,size_t sz,const char * file,int line)66 void *stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line)
67 {
68    if (ptr == NULL) {
69       return stb_leakcheck_malloc(sz, file, line);
70    } else if (sz == 0) {
71       stb_leakcheck_free(ptr);
72       return NULL;
73    } else {
74       stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) ptr - 1;
75       if (sz <= mi->size)
76          return ptr;
77       else {
78          #ifdef STB_LEAKCHECK_REALLOC_PRESERVE_MALLOC_FILELINE
79          void *q = stb_leakcheck_malloc(sz, mi->file, mi->line);
80          #else
81          void *q = stb_leakcheck_malloc(sz, file, line);
82          #endif
83          if (q) {
84             memcpy(q, ptr, mi->size);
85             stb_leakcheck_free(ptr);
86          }
87          return q;
88       }
89    }
90 }
91 
stblkck_internal_print(const char * reason,stb_leakcheck_malloc_info * mi)92 static void stblkck_internal_print(const char *reason, stb_leakcheck_malloc_info *mi)
93 {
94 #if defined(_MSC_VER) && _MSC_VER < 1900 // 1900=VS 2015
95    // Compilers that use the old MS C runtime library don't have %zd
96    // and the older ones don't even have %lld either... however, the old compilers
97    // without "long long" don't support 64-bit targets either, so here's the
98    // compromise:
99    #if _MSC_VER < 1400 // before VS 2005
100       printf("%s: %s (%4d): %8d bytes at %p\n", reason, mi->file, mi->line, (int)mi->size, (void*)(mi+1));
101    #else
102       printf("%s: %s (%4d): %16lld bytes at %p\n", reason, mi->file, mi->line, (long long)mi->size, (void*)(mi+1));
103    #endif
104 #else
105    // Assume we have %zd on other targets.
106    #ifdef __MINGW32__
107       __mingw_printf("%s: %s (%4d): %zd bytes at %p\n", reason, mi->file, mi->line, mi->size, (void*)(mi+1));
108    #else
109       printf("%s: %s (%4d): %zd bytes at %p\n", reason, mi->file, mi->line, mi->size, (void*)(mi+1));
110    #endif
111 #endif
112 }
113 
stb_leakcheck_dumpmem(void)114 void stb_leakcheck_dumpmem(void)
115 {
116    stb_leakcheck_malloc_info *mi = mi_head;
117    while (mi) {
118       if ((ptrdiff_t) mi->size >= 0)
119          stblkck_internal_print("LEAKED", mi);
120       mi = mi->next;
121    }
122    #ifdef STB_LEAKCHECK_SHOWALL
123    mi = mi_head;
124    while (mi) {
125       if ((ptrdiff_t) mi->size < 0)
126          stblkck_internal_print("FREED ", mi);
127       mi = mi->next;
128    }
129    #endif
130 }
131 #endif // STB_LEAKCHECK_IMPLEMENTATION
132 
133 #ifndef INCLUDE_STB_LEAKCHECK_H
134 #define INCLUDE_STB_LEAKCHECK_H
135 
136 #include <stdlib.h> // we want to define the macros *after* stdlib to avoid a slew of errors
137 
138 #define malloc(sz)    stb_leakcheck_malloc(sz, __FILE__, __LINE__)
139 #define free(p)       stb_leakcheck_free(p)
140 #define realloc(p,sz) stb_leakcheck_realloc(p,sz, __FILE__, __LINE__)
141 
142 extern void * stb_leakcheck_malloc(size_t sz, const char *file, int line);
143 extern void * stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line);
144 extern void   stb_leakcheck_free(void *ptr);
145 extern void   stb_leakcheck_dumpmem(void);
146 
147 #endif // INCLUDE_STB_LEAKCHECK_H
148 
149 
150 /*
151 ------------------------------------------------------------------------------
152 This software is available under 2 licenses -- choose whichever you prefer.
153 ------------------------------------------------------------------------------
154 ALTERNATIVE A - MIT License
155 Copyright (c) 2017 Sean Barrett
156 Permission is hereby granted, free of charge, to any person obtaining a copy of
157 this software and associated documentation files (the "Software"), to deal in
158 the Software without restriction, including without limitation the rights to
159 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
160 of the Software, and to permit persons to whom the Software is furnished to do
161 so, subject to the following conditions:
162 The above copyright notice and this permission notice shall be included in all
163 copies or substantial portions of the Software.
164 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
165 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
166 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
167 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
168 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
169 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
170 SOFTWARE.
171 ------------------------------------------------------------------------------
172 ALTERNATIVE B - Public Domain (www.unlicense.org)
173 This is free and unencumbered software released into the public domain.
174 Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
175 software, either in source code form or as a compiled binary, for any purpose,
176 commercial or non-commercial, and by any means.
177 In jurisdictions that recognize copyright laws, the author or authors of this
178 software dedicate any and all copyright interest in the software to the public
179 domain. We make this dedication for the benefit of the public at large and to
180 the detriment of our heirs and successors. We intend this dedication to be an
181 overt act of relinquishment in perpetuity of all present and future rights to
182 this software under copyright law.
183 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
184 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
185 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
186 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
187 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
188 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
189 ------------------------------------------------------------------------------
190 */
191