Skip to content
Myr

stb_leakcheck.h

This file

Path

stb_leakcheck.h

194 lines · 7.0 KiB · C

Last changed by

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

History of this file

Take it

The walk stopped after 200 commits: lines older than aeec66c point at it.

udpate version numbersSean Barrettbefore adoption5e4a061

line 1why, one step back
1// stb_leakcheck.h - v0.6 - quick & dirty malloc leak-checking - public domain

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 2–15why, one step back
2// LICENSE
3//4// See end of file.56#ifdef STB_LEAKCHECK_IMPLEMENTATION7#undef STB_LEAKCHECK_IMPLEMENTATION // don't implement more than once89// if we've already included leakcheck before, undefine the macros10#ifdef malloc11#undef malloc12#undef free13#undef realloc14#endif15

Merge branch 'stderr' of https://github.com/dimkr/stb into work2Sean Barrettbefore adoptiona0245ff

lines 16–19why, one step back
16#ifndef STB_LEAKCHECK_OUTPUT_PIPE
17#define STB_LEAKCHECK_OUTPUT_PIPE stdout18#endif19

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 20–103why, one step back
20#include <assert.h>
21#include <string.h>22#include <stdlib.h>23#include <stdio.h>24#include <stddef.h>25typedef struct malloc_info stb_leakcheck_malloc_info;2627struct malloc_info28{29 const char *file;30 int line;31 size_t size;32 stb_leakcheck_malloc_info *next,*prev;33};3435static stb_leakcheck_malloc_info *mi_head;3637void *stb_leakcheck_malloc(size_t sz, const char *file, int line)38{39 stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) malloc(sz + sizeof(*mi));40 if (mi == NULL) return mi;41 mi->file = file;42 mi->line = line;43 mi->next = mi_head;44 if (mi_head)45 mi->next->prev = mi;46 mi->prev = NULL;47 mi->size = (int) sz;48 mi_head = mi;49 return mi+1;50}5152void stb_leakcheck_free(void *ptr)53{54 if (ptr != NULL) {55 stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) ptr - 1;56 mi->size = ~mi->size;57 #ifndef STB_LEAKCHECK_SHOWALL58 if (mi->prev == NULL) {59 assert(mi_head == mi);60 mi_head = mi->next;61 } else62 mi->prev->next = mi->next;63 if (mi->next)64 mi->next->prev = mi->prev;65 free(mi);66 #endif67 }68}6970void *stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line)71{72 if (ptr == NULL) {73 return stb_leakcheck_malloc(sz, file, line);74 } else if (sz == 0) {75 stb_leakcheck_free(ptr);76 return NULL;77 } else {78 stb_leakcheck_malloc_info *mi = (stb_leakcheck_malloc_info *) ptr - 1;79 if (sz <= mi->size)80 return ptr;81 else {82 #ifdef STB_LEAKCHECK_REALLOC_PRESERVE_MALLOC_FILELINE83 void *q = stb_leakcheck_malloc(sz, mi->file, mi->line);84 #else85 void *q = stb_leakcheck_malloc(sz, file, line);86 #endif87 if (q) {88 memcpy(q, ptr, mi->size);89 stb_leakcheck_free(ptr);90 }91 return q;92 }93 }94}9596static void stblkck_internal_print(const char *reason, stb_leakcheck_malloc_info *mi)97{98#if defined(_MSC_VER) && _MSC_VER < 1900 // 1900=VS 201599 // Compilers that use the old MS C runtime library don't have %zd100 // and the older ones don't even have %lld either... however, the old compilers101 // without "long long" don't support 64-bit targets either, so here's the102 // compromise:103 #if _MSC_VER < 1400 // before VS 2005

Merge branch 'stderr' of https://github.com/dimkr/stb into work2Sean Barrettbefore adoptiona0245ff

line 104why, one step back
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));

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

line 105why, one step back
105 #else

Merge branch 'stderr' of https://github.com/dimkr/stb into work2Sean Barrettbefore adoptiona0245ff

line 106why, one step back
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));

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 107–110why, one step back
107 #endif
108#else109 // Assume we have %zd on other targets.110 #ifdef __MINGW32__

Merge branch 'stderr' of https://github.com/dimkr/stb into work2Sean Barrettbefore adoptiona0245ff

line 111why, one step back
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));

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

line 112why, one step back
112 #else

Merge branch 'stderr' of https://github.com/dimkr/stb into work2Sean Barrettbefore adoptiona0245ff

line 113why, one step back
113 fprintf(STB_LEAKCHECK_OUTPUT_PIPE, "%s: %s (%4d): %zd bytes at %p\n", reason, mi->file, mi->line, mi->size, (void*)(mi+1));

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 114–136why, one step back
114 #endif
115#endif116}117118void stb_leakcheck_dumpmem(void)119{120 stb_leakcheck_malloc_info *mi = mi_head;121 while (mi) {122 if ((ptrdiff_t) mi->size >= 0)123 stblkck_internal_print("LEAKED", mi);124 mi = mi->next;125 }126 #ifdef STB_LEAKCHECK_SHOWALL127 mi = mi_head;128 while (mi) {129 if ((ptrdiff_t) mi->size < 0)130 stblkck_internal_print("FREED ", mi);131 mi = mi->next;132 }133 #endif134}135#endif // STB_LEAKCHECK_IMPLEMENTATION136

udpate version numbersSean Barrettbefore adoption5e4a061

line 137why, one step back
137#if !defined(INCLUDE_STB_LEAKCHECK_H) || !defined(malloc)

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 138–159why, one step back
138#define INCLUDE_STB_LEAKCHECK_H
139140#include <stdlib.h> // we want to define the macros *after* stdlib to avoid a slew of errors141142#define malloc(sz) stb_leakcheck_malloc(sz, __FILE__, __LINE__)143#define free(p) stb_leakcheck_free(p)144#define realloc(p,sz) stb_leakcheck_realloc(p,sz, __FILE__, __LINE__)145146extern void * stb_leakcheck_malloc(size_t sz, const char *file, int line);147extern void * stb_leakcheck_realloc(void *ptr, size_t sz, const char *file, int line);148extern void stb_leakcheck_free(void *ptr);149extern void stb_leakcheck_dumpmem(void);150151#endif // INCLUDE_STB_LEAKCHECK_H152153154/*155------------------------------------------------------------------------------156This software is available under 2 licenses -- choose whichever you prefer.157------------------------------------------------------------------------------158ALTERNATIVE A - MIT License159Copyright (c) 2017 Sean Barrett

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

lines 160–164why, one step back
160Permission is hereby granted, free of charge, to any person obtaining a copy of
161this software and associated documentation files (the "Software"), to deal in162the Software without restriction, including without limitation the rights to163use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies164of the Software, and to permit persons to whom the Software is furnished to do

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

line 165why, one step back
165so, subject to the following conditions:

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

line 166why, one step back
166The above copyright notice and this permission notice shall be included in all

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

line 167why, one step back
167copies or substantial portions of the Software.

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

lines 168–173why, one step back
168THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
169IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,170FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE171AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER172LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,173OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 174–177why, one step back
174SOFTWARE.
175------------------------------------------------------------------------------176ALTERNATIVE B - Public Domain (www.unlicense.org)177This is free and unencumbered software released into the public domain.

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

lines 178–179why, one step back
178Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
179software, either in source code form or as a compiled binary, for any purpose,

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

line 180why, one step back
180commercial or non-commercial, and by any means.

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

lines 181–185why, one step back
181In jurisdictions that recognize copyright laws, the author or authors of this
182software dedicate any and all copyright interest in the software to the public183domain. We make this dedication for the benefit of the public at large and to184the detriment of our heirs and successors. We intend this dedication to be an185overt act of relinquishment in perpetuity of all present and future rights to

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

line 186why, one step back
186this software under copyright law.

Fix trailing whitespaceSean Barrettbefore adoption2bb4a0a

lines 187–191why, one step back
187THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
188IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,189FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE190AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN191ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION

Merge branch 'randlong' of https://github.com/flibitijibibo/stb into workingSean Barrettbefore adoptionaeec66c

lines 192–194why, one step back
192WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
193------------------------------------------------------------------------------194*/