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

1// stb_leakcheck.h - v0.6 - quick & dirty malloc leak-checking - public domain2// LICENSE3//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#endif1516#ifndef STB_LEAKCHECK_OUTPUT_PIPE17#define STB_LEAKCHECK_OUTPUT_PIPE stdout18#endif1920#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 2005104      fprintf(STB_LEAKCHECK_OUTPUT_PIPE, "%s: %s (%4d): %8d bytes at %p\n", reason, mi->file, mi->line, (int)mi->size, (void*)(mi+1));105   #else106      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));107   #endif108#else109   // Assume we have %zd on other targets.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));112   #else113      fprintf(STB_LEAKCHECK_OUTPUT_PIPE, "%s: %s (%4d): %zd bytes at %p\n", reason, mi->file, mi->line, mi->size, (void*)(mi+1));114   #endif115#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_IMPLEMENTATION136137#if !defined(INCLUDE_STB_LEAKCHECK_H) || !defined(malloc)138#define INCLUDE_STB_LEAKCHECK_H139140#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 Barrett160Permission is hereby granted, free of charge, to any person obtaining a copy of161this 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 do165so, subject to the following conditions:166The above copyright notice and this permission notice shall be included in all167copies or substantial portions of the Software.168THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR169IMPLIED, 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 THE174SOFTWARE.175------------------------------------------------------------------------------176ALTERNATIVE B - Public Domain (www.unlicense.org)177This is free and unencumbered software released into the public domain.178Anyone is free to copy, modify, publish, use, compile, sell, or distribute this179software, either in source code form or as a compiled binary, for any purpose,180commercial or non-commercial, and by any means.181In jurisdictions that recognize copyright laws, the author or authors of this182software 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 to186this software under copyright law.187THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR188IMPLIED, 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 CONNECTION192WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.193------------------------------------------------------------------------------194*/