From e1d4a32b103161e3b3ec51c0895f2ca88413078f Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Sun, 29 Jul 2007 19:40:54 +0100 Subject: [PATCH] further horrid memory debugging stuff Organization: Straylight/Edgeware From: rjk@greenend.org.uk <> --- disobedience/choose.c | 4 ++ disobedience/disobedience.c | 76 ++++++++++++++++++++++++++++++++++++- disobedience/disobedience.h | 11 ++++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/disobedience/choose.c b/disobedience/choose.c index 565902d..1cd4500 100644 --- a/disobedience/choose.c +++ b/disobedience/choose.c @@ -596,7 +596,9 @@ static void redisplay_tree(void) { files_selected = 0; files_visible = 0; /* Correct the layout and find out how much space it uses */ + MTAG_PUSH("display_tree"); d = display_tree(root, 0, 0); + MTAG_POP(); /* We must set the total size or scrolling will not work (it wouldn't be hard * for GtkLayout to figure it out for itself but presumably you're supposed * to be able to have widgets off the edge of the layuot.) @@ -630,6 +632,7 @@ static struct displaydata display_tree(struct choosenode *cn, int x, int y) { * A non-expandable item has just a text label and no arrow. */ if(!cn->container) { + MTAG_PUSH("make_widgets"); /* Widgets need to be created */ NW(hbox); cn->hbox = gtk_hbox_new(FALSE, 1); @@ -665,6 +668,7 @@ static struct displaydata display_tree(struct choosenode *cn, int x, int y) { gtk_widget_set_name(cn->container, "choose"); /* Show everything by default */ gtk_widget_show_all(cn->container); + MTAG_POP(); } assert(cn->container); /* Make sure the icon is right */ diff --git a/disobedience/disobedience.c b/disobedience/disobedience.c index cb5bb33..5f57f20 100644 --- a/disobedience/disobedience.c +++ b/disobedience/disobedience.c @@ -273,6 +273,72 @@ static void count_widgets(void) { } #endif +#if MTRACK +const char *mtag = "init"; +static hash *mtrack_hash; + +static int *mthfind(const char *tag) { + static const int zero = 0; + int *cp = hash_find(mtrack_hash, tag); + if(!cp) { + hash_add(mtrack_hash, tag, &zero, HASH_INSERT); + cp = hash_find(mtrack_hash, tag); + } + return cp; +} + +static void *trap_malloc(size_t n) { + void *ptr = malloc(n + sizeof(char *)); + + *(const char **)ptr = mtag; + ++*mthfind(mtag); + return (char *)ptr + sizeof(char *); +} + +static void trap_free(void *ptr) { + const char *tag; + if(!ptr) + return; + ptr = (char *)ptr - sizeof(char *); + tag = *(const char **)ptr; + --*mthfind(tag); + free(ptr); +} + +static void *trap_realloc(void *ptr, size_t n) { + if(!ptr) + return trap_malloc(n); + if(!n) { + trap_free(ptr); + return 0; + } + ptr = (char *)ptr - sizeof(char *); + ptr = realloc(ptr, n + sizeof(char *)); + *(const char **)ptr = mtag; + return (char *)ptr + sizeof(char *); +} + +static int report_tags_callback(const char *key, void *value, + void attribute((unused)) *u) { + fprintf(stderr, "%16s: %d\n", key, *(int *)value); + return 0; +} + +static void report_tags(void) { + hash_foreach(mtrack_hash, report_tags_callback, 0); + fprintf(stderr, "\n"); +} + +static const GMemVTable glib_memvtable = { + trap_malloc, + trap_realloc, + trap_free, + 0, + 0, + 0 +}; +#endif + /* Called once every 10 minutes */ static gboolean periodic(gpointer attribute((unused)) data) { D(("periodic")); @@ -284,6 +350,9 @@ static gboolean periodic(gpointer attribute((unused)) data) { #if MDEBUG count_widgets(); fprintf(stderr, "cache size: %zu\n", cache_count()); +#endif +#if MTRACK + report_tags(); #endif return TRUE; /* don't remove me */ } @@ -350,6 +419,10 @@ int main(int argc, char **argv) { /* garbage-collect PCRE's memory */ pcre_malloc = xmalloc; pcre_free = xfree; +#if MTRACK + mtrack_hash = hash_new(sizeof (int)); + g_mem_set_vtable((GMemVTable *)&glib_memvtable); +#endif if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); gtk_init(&argc, &argv); gtk_rc_parse_string(style); @@ -375,7 +448,7 @@ int main(int argc, char **argv) { return 1; /* already reported an error */ disorder_eclient_log(logclient, &gdisorder_log_callbacks, 0); /* periodic operations (e.g. expiring the cache) */ -#if MDEBUG +#if MDEBUG || MTRACK g_timeout_add(5000/*milliseconds*/, periodic, 0); #else g_timeout_add(600000/*milliseconds*/, periodic, 0); @@ -389,6 +462,7 @@ int main(int argc, char **argv) { gtk_rc_reset_styles(gtk_settings_get_for_screen(gdk_screen_get_default())); gtk_widget_show_all(toplevel); D(("enter main loop")); + MTAG("misc"); g_main_loop_run(mainloop); return 0; } diff --git a/disobedience/disobedience.h b/disobedience/disobedience.h index 1745764..21d589d 100644 --- a/disobedience/disobedience.h +++ b/disobedience/disobedience.h @@ -185,6 +185,17 @@ void choose_update(void); #define WT(what) struct neverused #endif +#if MTRACK +extern const char *mtag; +#define MTAG(x) do { mtag = x; } while(0) +#define MTAG_PUSH(x) do { const char *save_mtag = mtag; mtag = x; (void)0 +#define MTAG_POP() mtag = save_mtag; } while(0) +#else +#define MTAG(x) do { } while(0) +#define MTAG_PUSH(x) do {} while(0) +#define MTAG_POP() do {} while(0) +#endif + #endif /* DISOBEDIENCE_H */ /* -- [mdw]