diff --git a/libc/malloc/_malloc.h b/libc/malloc/_malloc.h index 2c20fb854..ffdfd85c4 100644 --- a/libc/malloc/_malloc.h +++ b/libc/malloc/_malloc.h @@ -18,7 +18,7 @@ extern int __debug_level; #define dprintf(...) #define debug(str,ptr) #elif VERBOSE == 1 -#define dprintf __dprintf +#define dprintf(...) do { if (__debug_level) __dprintf(__VA_ARGS__); } while (0) #define debug(str,ptr) #else #define dprintf(...) diff --git a/libc/malloc/dprintf.c b/libc/malloc/dprintf.c index 9520be41f..3beb8b45e 100644 --- a/libc/malloc/dprintf.c +++ b/libc/malloc/dprintf.c @@ -1,30 +1,39 @@ -#if VERBOSE #include #include #include - -int __debug_level = 1; +#include +#include /* - * Very tiny printf to stderr. + * Very tiny printf to console. * Supports: * %u unsigned int + * %p unsigned int (displays as unsigned int!) + * %d int (positive numbers only) + * %s char * */ int __dprintf(const char *fmt, ...) { - unsigned int n = 0; + unsigned int n; char *p; va_list va; - char b[128]; + char b[80]; + static int fd = -1; - if (__debug_level == 0) - return 0; + if (fd < 0) + fd = open(_PATH_CONSOLE, O_WRONLY); va_start(va, fmt); - for (; *fmt; fmt++) { + for (n = 0; *fmt; fmt++) { if (*fmt == '%') { switch (*++fmt) { + case 's': + p = va_arg(va, char *); + goto outstr; + case 'p': /* displays as unsigned int! */ + case 'd': case 'u': p = uitoa(va_arg(va, unsigned int)); + outstr: while (*p && n < sizeof(b)) b[n++] = *p++; break; @@ -36,6 +45,5 @@ int __dprintf(const char *fmt, ...) } } va_end(va); - return write(2, b, n); + return write(fd, b, n); } -#endif diff --git a/libc/malloc/malloc.c b/libc/malloc/malloc.c index 7bf128f96..6d79938ae 100644 --- a/libc/malloc/malloc.c +++ b/libc/malloc/malloc.c @@ -18,6 +18,10 @@ #define MAX_INT ((int)(((unsigned)-1)>>1)) +#if VERBOSE == 1 +int __debug_level = 1; +#endif + /* * The chunk_list pointer is either NULL or points to a chunk in a * circular list of all the free blocks in memory @@ -192,9 +196,7 @@ malloc(size_t size) #if VERBOSE == 1 if (chunk_list == 0) - { sysctl(CTL_GET, "kern.debug", &__debug_level); - } #endif errno = 0; diff --git a/libc/malloc/v7malloc.c b/libc/malloc/v7malloc.c index 4f4e17fab..a7442f4d8 100644 --- a/libc/malloc/v7malloc.c +++ b/libc/malloc/v7malloc.c @@ -57,14 +57,9 @@ static union store __wcnear *alloct; /*arena top*/ static union store __wcnear *allocx; /*for benefit of realloc*/ #if DEBUG -#include #include -#include -#include #include #define ASSERT(p) if(!(p))malloc_assert_fail(#p);else {} -#define errmsg(str) write(STDERR_FILENO, str, sizeof(str) - 1) -#define errstr(str) write(STDERR_FILENO, str, strlen(str)) static void malloc_assert_fail(char *s); static int malloc_check_heap(void); #else @@ -72,25 +67,11 @@ static int malloc_check_heap(void); #endif #if DEBUG > 1 -#define debug(...) do { if (debug_level > 1) fprintf(dbgout, __VA_ARGS__); } while (0) -#define debug2(...) do { if (debug_level > 2) fprintf(dbgout, __VA_ARGS__); } while (0) +#define debug(...) do { if (debug_level > 1) __dprintf(__VA_ARGS__); } while (0) +#define debug2(...) do { if (debug_level > 2) __dprintf(__VA_ARGS__); } while (0) +int __dprintf(const char *fmt, ...); static void malloc_show_heap(void); static int debug_level = DEBUG; -static unsigned char bufdbg[64]; -static FILE dbgout[1] = -{ - { - bufdbg, - bufdbg, - bufdbg, - bufdbg, - bufdbg + sizeof(bufdbg), - -1, - _IONBF | __MODE_WRITE | __MODE_IOTRAN, - { 0,0,0,0,0,0,0,0 }, - 0 - } -}; #else #define debug(...) #define malloc_show_heap() @@ -102,10 +83,6 @@ malloc(size_t nbytes) union store __wcnear *p, __wcnear *q; unsigned int nw, temp; -#if DEBUG > 1 - if (dbgout->fd < 0) - dbgout->fd = open(_PATH_CONSOLE, O_WRONLY); -#endif #if DEBUG == 2 sysctl(CTL_GET, "malloc.debug", &debug_level); #endif @@ -277,8 +254,7 @@ realloc(void *ptr, size_t nbytes) #if DEBUG static void malloc_assert_fail(char *s) { - errmsg("malloc assert fail: "); - errstr(s); + __dprintf("malloc assert fail: %s\n", s); abort(); }