Dynamic Memory Tracker (DMT) Library
A small C library for detecting memory leaks, tracking memory usage and detecting invalid frees or reallocs. The library consists of two files (dmt.c and dmt.h) and provides wrappers for the 4 main dynamic memory functions in C (malloc, calloc, realloc and free). The library is currently thread unsafe.
The library should be dropped inside a C project and compiled along with it. The library provides the following macros and functions:
void* dmt_malloc(size_t size)A wrapper for
void* dmt_calloc(size_t num, size_t size)A wrapper for
void* dmt_realloc(void* ptr, size_t size)A wrapper for
void dmt_free(void *ptr)A wrapper for
size_t dmt_size(void* ptr)Returns the size in bytes of a pointer's allocation
void dmt_dump(FILE* fp)Dumps information about the currently unfreed allocations
size_t dmt_usage(void)Returns the total number of bytes currently allocated
int dmt_has(void* ptr)Returns non-zero if the pointer is allocated by dmt
The wrapper macros (
dmt_free) should be used in place of the functions which they wrap (
free). Their arguments and return values match those of the functions they wrap.
Additional options can be set by defining the following macros:
DMT_UNSAFEAvoids checking whether a pointer passed to
dmt_reallocis valid -- this may offer a noticable speed increase in programs with many allocations
DMT_ABORT_NULLIf malloc, calloc or realloc return with
NULL, the program will abort with an error
DMT_STACK_TRACE(Linux only) When using
dmt_dumpeach unfreed allocation is also accompanied with a stack trace from when the allocation was made. Additionally, if the program aborts with an error it will dump a stack trace to stderr
This library is free software; you can redistribute it and/or modify it under the terms of the MIT license. See LICENSE for details.