summaryrefslogtreecommitdiff
path: root/libc
diff options
context:
space:
mode:
Diffstat (limited to 'libc')
-rw-r--r--libc/stdlib/malloc/heap.h39
-rw-r--r--libc/stdlib/malloc/malloc.c4
-rw-r--r--libc/stdlib/malloc/malloc.h7
3 files changed, 20 insertions, 30 deletions
diff --git a/libc/stdlib/malloc/heap.h b/libc/stdlib/malloc/heap.h
index 6b370fd5e..d8e8335b6 100644
--- a/libc/stdlib/malloc/heap.h
+++ b/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. */
diff --git a/libc/stdlib/malloc/malloc.c b/libc/stdlib/malloc/malloc.c
index 4b4cc7d56..021ed3a24 100644
--- a/libc/stdlib/malloc/malloc.c
+++ b/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)
diff --git a/libc/stdlib/malloc/malloc.h b/libc/stdlib/malloc/malloc.h
index 30a28b776..4071a4ffb 100644
--- a/libc/stdlib/malloc/malloc.h
+++ b/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