Преглед на файлове

Update debugging hooks.

Miles Bader преди 22 години
родител
ревизия
73f88c8f7e
променени са 3 файла, в които са добавени 20 реда и са изтрити 30 реда
  1. 11 28
      libc/stdlib/malloc/heap.h
  2. 4 0
      libc/stdlib/malloc/malloc.c
  3. 5 2
      libc/stdlib/malloc/malloc.h

+ 11 - 28
libc/stdlib/malloc/heap.h

@@ -81,39 +81,21 @@ struct heap_free_area
 
 
 /* Define HEAP_DEBUGGING to cause the heap routines to emit debugging info
-   to stderr.  */
+   to stderr when the variable __heap_debug is set to true.  */
 #ifdef HEAP_DEBUGGING
-#include <stdio.h>
-static void HEAP_DEBUG (struct heap *heap, const char *str)
-{
-  static int recursed = 0;
-  if (! recursed)
-    {
-      struct heap_free_area *fa, *prev;
-      recursed = 1;
-      fprintf (stderr, "  %s: heap @0x%lx:\n", str, (long)heap);
-      for (prev = 0, fa = heap->free_areas; fa; prev = 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);
-	  if (fa->prev != prev)
-	    fprintf (stderr,
-		     "      PREV POINTER CORRUPTED!!!!  P=0x%lx should be 0x%lx\n",
-		     (long)fa->prev, (long)prev);
-	}
-      recursed = 0;
-    }
-}
+extern int __heap_debug;
+#define HEAP_DEBUG(heap, str) (__heap_debug ? __heap_dump (heap, str) : 0)
 #else
 #define HEAP_DEBUG(heap, str) (void)0
 #endif
 
+/* Output a text representation of HEAP to stderr, labelling it with STR.  */
+extern void __heap_dump (struct heap *heap, const char *str);
+
+/* Do some consistency checks on HEAP.  If they fail, output an error
+   message to stderr, and exit.  STR is printed with the failure message.  */
+extern void __heap_check (struct heap *heap, const char *str);
+
 
 /* Delete the free-area FA from HEAP.  */
 extern inline void
@@ -127,6 +109,7 @@ __heap_delete (struct heap *heap, struct heap_free_area *fa)
     heap->free_areas = fa->next;
 }
 
+
 /* Link the free-area FA between the existing free-area's PREV and NEXT in
    HEAP.  PREV and NEXT may be 0; if PREV is 0, FA is installed as the
    first free-area.  */

+ 4 - 0
libc/stdlib/malloc/malloc.c

@@ -31,6 +31,10 @@ malloc_mutex_t __malloc_sbrk_lock;
 # endif /* MALLOC_USE_SBRK */
 #endif /* MALLOC_USE_LOCKING */
 
+#ifdef MALLOC_DEBUGGING
+int __malloc_debug = 0;
+#endif
+
 
 void *
 malloc (size_t size)

+ 5 - 2
libc/stdlib/malloc/malloc.h

@@ -122,10 +122,13 @@ extern malloc_mutex_t __malloc_sbrk_lock;
 #endif /* !likely */
 
 
-/* Define MALLOC_DEBUGGING to cause malloc to emit debugging info to stderr.  */
+/* Define MALLOC_DEBUGGING to cause malloc to emit debugging info to stderr
+   when the variable __malloc_debug is set to true. */
 #ifdef MALLOC_DEBUGGING
 #include <stdio.h>
-#define MALLOC_DEBUG(fmt, args...) fprintf (stderr, fmt , ##args)
+extern int __malloc_debug;
+#define MALLOC_DEBUG(fmt, args...) \
+  (__malloc_debug ? fprintf (stderr, fmt , ##args) : 0)
 #else
 #define MALLOC_DEBUG(fmt, args...) (void)0
 #endif