/* * malloc-dbg.c * * Debug memory allocation. Hacky code, used only * for debugging. * * Copyright (c) 2008 Linas Vepstas */ #ifndef _MSC_VER #include #include #include #include /* ======================================================== */ static void *(*old_malloc_hook)(size_t, const void *); static void (*old_free_hook)(void *, const void *); static void *(*old_realloc_hook)(void *, size_t, const void *); static void my_free_hook(void * mem, const void * caller); static void * my_malloc_hook(size_t n_bytes, const void * caller); #define TBLSZ 366000 typedef struct { void * mem; const void * caller; int cnt; size_t sz; } loc_t; static loc_t loc[TBLSZ]; static int mcnt = 0; static FILE *fh = NULL; static void init_io(void) { if (fh) return; /* fh = fopen("/tmp/m", "w"); */ fh = stdout; } static void * my_realloc_hook(void * mem, size_t n_bytes, const void *caller) { __realloc_hook = old_realloc_hook; void * nm = realloc(mem, n_bytes); old_realloc_hook = __realloc_hook; __realloc_hook = my_realloc_hook; int i; for (i=0; i