]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
From: Henrik Nordstrom <hno@hem.passagen.se>
authorwessels <>
Wed, 18 Feb 1998 08:00:43 +0000 (08:00 +0000)
committerwessels <>
Wed, 18 Feb 1998 08:00:43 +0000 (08:00 +0000)
Squid-1.2.beta15: Show memory map as a tree
      Part of my beta14 memory map patch did not make it into beta15.
      Here are the missing pieces.

lib/util.c
src/main.cc

index 6e037ea97675e70b2b8973318823d2c588111254..c9a4e26df1cd22e65c109fee57c4ee6236757b39 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: util.c,v 1.42 1998/02/13 18:26:57 wessels Exp $
+ * $Id: util.c,v 1.43 1998/02/18 01:00:48 wessels Exp $
  *
  * DEBUG: 
  * AUTHOR: Harvest Derived
@@ -355,55 +355,61 @@ xmalloc_show_trace(void *p, int sign)
     last_accounted = accounted;
     last_mallinfo = mi;
 }
-short (*malloc_refs)[DBG_ARRY_SZ];
-char **xmalloc_leak_test;
-#define XMALLOC_LEAK_CHECKED (1<<15)
+
+short malloc_refs[DBG_ARRY_BKTS][DBG_ARRY_SZ];
 #define XMALLOC_LEAK_ALIGN (4)
-static int
-xmalloc_scan_region(void *start, int size)
+static void
+xmalloc_scan_region(void *start, int size, int depth)
 {
     int B, I;
     char *ptr = start;
     char *end = ptr + size - XMALLOC_LEAK_ALIGN;
-    int found = 0;
+    static int sum = 0;
     while (ptr <= end) {
        void *p = *(void **) ptr;
        if (p && p != start) {
            B = (((int) p) >> 4) & 0xFF;
            for (I = 0; I < DBG_ARRY_SZ; I++) {
                if (malloc_ptrs[B][I] == p) {
-                   if (!malloc_refs[B][I])
-                       found++;
-                   malloc_refs[B][I]++;
+                   if (!malloc_refs[B][I]++) {
+                       /* A new reference */
+                       fprintf(stderr, "%*s%p %s:%d size %d allocation %d\n",
+                           depth, "",
+                           malloc_ptrs[B][I], malloc_file[B][I],
+                           malloc_line[B][I], malloc_size[B][I],
+                           malloc_count[B][I]);
+                       sum += malloc_size[B][I];
+                       xmalloc_scan_region(malloc_ptrs[B][I], malloc_size[B][I], depth + 1);
+                       if (depth == 0) {
+                           if (sum != malloc_size[B][I])
+                               fprintf(stderr, "=== %d bytes\n", sum);
+                           sum = 0;
+                       }
+#if XMALLOC_SHOW_ALL_REFERENCES
+                   } else {
+                       /* We have already scanned this pointer... */
+                       fprintf(stderr, "%*s%p %s:%d size %d allocation %d ... (%d)\n",
+                           depth * 2, "",
+                           malloc_ptrs[B][I], malloc_file[B][I],
+                           malloc_line[B][I], malloc_size[B][I],
+                           malloc_count[B][I], malloc_refs[B][I]);
+#endif
+                   }
                }
            }
        }
        ptr += XMALLOC_LEAK_ALIGN;
     }
-    return found;
 }
-extern void _etext;
+
 void
 xmalloc_find_leaks(void)
 {
     int B, I;
-    int found;
     int leak_sum = 0;
-    fprintf(stderr, "Searching for memory references...\n");
-    malloc_refs = xcalloc(DBG_ARRY_BKTS, sizeof(*malloc_refs));
-    found = xmalloc_scan_region(&_etext, (void *) sbrk(0) - (void *) &_etext);
-    while (found) {
-       found = 0;
-       for (I = 0; I < DBG_ARRY_SZ && !found; I++) {
-           for (B = 0; B < DBG_ARRY_BKTS; B++) {
-               if (malloc_refs[B][I] > 0) {
-                   malloc_refs[B][I] |= XMALLOC_LEAK_CHECKED;
-                   found += xmalloc_scan_region(malloc_ptrs[B][I],
-                       malloc_size[B][I]);
-               }
-           }
-       }
-    }
+    extern void _etext;
+    fprintf(stderr, "----- Memory map ----\n");
+    xmalloc_scan_region(&_etext, (void *) sbrk(0) - (void *) &_etext, 0);
     for (B = 0; B < DBG_ARRY_BKTS; B++) {
        for (I = 0; I < DBG_ARRY_SZ; I++) {
            if (malloc_ptrs[B][I] && malloc_refs[B][I] == 0) {
@@ -411,8 +417,8 @@ xmalloc_find_leaks(void)
                fprintf(stderr, "Leak found: %p", malloc_ptrs[B][I]);
                fprintf(stderr, " %s", malloc_file[B][I]);
                fprintf(stderr, ":%d", malloc_line[B][I]);
-               fprintf(stderr, " size %d\n", malloc_size[B][I]);
-               fprintf(stderr, " allocation %d", malloc_count[B][I]);
+               fprintf(stderr, " size %d", malloc_size[B][I]);
+               fprintf(stderr, " allocation %d\n", malloc_count[B][I]);
                leak_sum += malloc_size[B][I];
            }
        }
@@ -422,22 +428,6 @@ xmalloc_find_leaks(void)
     } else {
        fprintf(stderr, "No memory leaks detected\n");
     }
-}
-void
-xmalloc_dump_map(void)
-{
-    int B, I;
-    fprintf(stderr, "----- Memory map ----\n");
-    for (B = 0; B < DBG_ARRY_BKTS; B++) {
-       for (I = 0; I < DBG_ARRY_SZ; I++) {
-           if (malloc_ptrs[B][I]) {
-               printf("%p %s:%d size %d allocation %d references %d\n",
-                   malloc_ptrs[B][I], malloc_file[B][I], malloc_line[B][I],
-                   malloc_size[B][I], malloc_count[B][I],
-                   malloc_refs[B][I] & (XMALLOC_LEAK_CHECKED - 1));
-           }
-       }
-    }
     fprintf(stderr, "----------------------\n");
 }
 #endif /* XMALLOC_TRACE */
index 62f108b23023eb2c0ce79dd76821e4fc1b372290..006038182560c70b3d7fdd4d766b8d11ddbf09fc 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: main.cc,v 1.221 1998/02/17 23:04:04 wessels Exp $
+ * $Id: main.cc,v 1.222 1998/02/18 01:00:43 wessels Exp $
  *
  * DEBUG: section 1     Startup and Main Loop
  * AUTHOR: Harvest Derived
@@ -749,9 +749,7 @@ normal_shutdown(void)
     {
        extern int xmalloc_total;
        extern void xmalloc_find_leaks(void);
-       extern void xmalloc_dump_map(void);
        xmalloc_find_leaks();
-       xmalloc_dump_map();
        debug(1, 0) ("Memory used after shutdown: %d\n", xmalloc_total);
     }
 #endif