summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMiles Bader <miles@lsi.nec.co.jp>2002-09-06 04:59:34 +0000
committerMiles Bader <miles@lsi.nec.co.jp>2002-09-06 04:59:34 +0000
commit87cc9a97c5c7e4e736c2a2f3abc4d7563308ef65 (patch)
tree6bbf318c0854facca8523ffc3131aa8a53d6de1e
parent73f88c8f7e9733dd3252b464f5ca0108ff7297c4 (diff)
Initial checkin.
-rw-r--r--libc/stdlib/malloc/heap_debug.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/libc/stdlib/malloc/heap_debug.c b/libc/stdlib/malloc/heap_debug.c
new file mode 100644
index 000000000..1453a5a39
--- /dev/null
+++ b/libc/stdlib/malloc/heap_debug.c
@@ -0,0 +1,86 @@
+/*
+ * libc/stdlib/malloc/heap_debug.c -- optional heap debugging routines
+ *
+ * Copyright (C) 2002 NEC Corporation
+ * Copyright (C) 2002 Miles Bader <miles@gnu.org>
+ *
+ * This file is subject to the terms and conditions of the GNU Lesser
+ * General Public License. See the file COPYING.LIB in the main
+ * directory of this archive for more details.
+ *
+ * Written by Miles Bader <miles@gnu.org>
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+
+#include "heap.h"
+
+
+#ifdef HEAP_DEBUGGING
+int __heap_debug = 0;
+#endif
+
+
+static void
+__heap_dump_freelist (struct heap *heap)
+{
+ struct heap_free_area *fa;
+ for (fa = heap->free_areas; fa; fa = fa->next)
+ fprintf (stderr,
+ " 0x%lx: 0x%lx - 0x%lx (%d)\tP=0x%lx, N=0x%lx\n",
+ (long)fa,
+ (long)HEAP_FREE_AREA_START (fa),
+ (long)HEAP_FREE_AREA_END (fa),
+ fa->size,
+ (long)fa->prev,
+ (long)fa->next);
+}
+
+/* Output a text representation of HEAP to stderr, labelling it with STR. */
+void
+__heap_dump (struct heap *heap, const char *str)
+{
+ static int recursed = 0;
+
+ if (! recursed)
+ {
+ __heap_check (heap, str);
+
+ recursed = 1;
+
+ fprintf (stderr, " %s: heap @0x%lx:\n", str, (long)heap);
+ __heap_dump_freelist (heap);
+
+ recursed = 0;
+ }
+}
+
+
+/* Do some consistency checks on HEAP. If they fail, output an error
+ message to stderr, and exit. STR is printed with the failure message. */
+void
+__heap_check (struct heap *heap, const char *str)
+{
+ struct heap_free_area *fa, *prev;
+
+ for (prev = 0, fa = heap->free_areas; fa; prev = fa, fa = fa->next)
+ if (fa->prev != prev)
+ {
+ if (str)
+ fprintf (stderr, "\nHEAP CHECK FAILURE %s: ", str);
+ else
+ fprintf (stderr, "\nHEAP CHECK FAILURE: ");
+
+ fprintf (stderr,
+ " prev pointer corrupted: P=0x%lx should be 0x%lx\n",
+ (long)fa->prev, (long)prev);
+ fprintf (stderr, "\nheap:\n");
+
+ __heap_dump_freelist (heap);
+
+ exit (22);
+ }
+}