chiark / gitweb /
further horrid memory debugging stuff
authorrjk@greenend.org.uk <>
Sun, 29 Jul 2007 18:40:54 +0000 (19:40 +0100)
committerrjk@greenend.org.uk <>
Sun, 29 Jul 2007 18:40:54 +0000 (19:40 +0100)
disobedience/choose.c
disobedience/disobedience.c
disobedience/disobedience.h

index 565902d6539f0923381aa5e4448d069f7fb82529..1cd4500c7e9ba46570ac4c19765cfdc5af6db1b1 100644 (file)
@@ -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 */
index cb5bb33edd35da0ec289ee7f5e8f078ec076f91d..5f57f2043e781aa96dfec068e0f4e0eb204efbd5 100644 (file)
@@ -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;
 }
index 1745764f76d073ba1bcef64176c2f9c877b650fe..21d589dac74d1a4b5878e30a55e5198c969277f3 100644 (file)
@@ -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 */
 
 /*