Skip to content

Instantly share code, notes, and snippets.

@kulp
Created September 21, 2011 23:57
Show Gist options
  • Save kulp/1233671 to your computer and use it in GitHub Desktop.
Save kulp/1233671 to your computer and use it in GitHub Desktop.
Poor man's valgrind
// "Poor man's valgrind"
#include "pmvg.h"
#include <stdlib.h>
#include <search.h>
static void *track_tree;
struct track_pair {
void *addr; size_t size;
};
static long bytes_lost;
static int track_pair_finder(const void *a, const void *b)
{
return b - a;
}
void *track_malloc(size_t size)
{
void *addr = malloc(size);
struct track_pair **where = tsearch(addr, &track_tree, track_pair_finder);
*where = malloc(sizeof **where);
(*where)->addr = addr;
(*where)->size = size;
return addr;
}
void *track_realloc(void *addr, size_t size)
{
if (addr)
tdelete(addr, &track_tree, track_pair_finder);
void *ptr = realloc(addr, size);
struct track_pair **where = tsearch(ptr, &track_tree, track_pair_finder);
*where = malloc(sizeof **where);
(*where)->addr = ptr;
(*where)->size = size;
return ptr;
}
void track_free(void *addr)
{
free(addr);
//struct track_pair **where =
tdelete(addr, &track_tree, track_pair_finder);
//free(*where);
//*where = NULL;
}
static void treewalker(const void *nodep, const VISIT which, const int depth)
{
struct track_pair * const *what = nodep;
if (which == postorder)
bytes_lost += (*what)->size;
}
long track_byteslost(void)
{
bytes_lost = 0;
twalk(track_tree, treewalker);
return bytes_lost;
}
#include <stddef.h>
void *track_malloc(size_t size);
void *track_realloc(void *addr, size_t size);
void track_free(void *addr);
long track_byteslost(void);
@kulp
Copy link
Author

kulp commented Sep 21, 2011

Be aware that this implementation leaks memory of its own, which it does not track.

@kulp
Copy link
Author

kulp commented Sep 22, 2011

First revision had illegal reads and was pretty much completely wrong. This version still shouldn't be trusted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment