From b9b1ecafd938fc72b9c9ea1cc4fbccf84301b6a2 Mon Sep 17 00:00:00 2001 From: Greg Haerr Date: Thu, 12 Dec 2024 21:57:43 -0700 Subject: [PATCH] [libc] Fixes to v7malloc.c for OpenWatcom --- libc/malloc/dprintf.c | 50 ++++++++++++++++++++++++++++++++++++------ libc/malloc/v7malloc.c | 18 +++++++-------- 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/libc/malloc/dprintf.c b/libc/malloc/dprintf.c index 3beb8b45e..fbd7e9ba4 100644 --- a/libc/malloc/dprintf.c +++ b/libc/malloc/dprintf.c @@ -4,17 +4,41 @@ #include #include +static char *uitostr(unsigned int val, int radix, int width) +{ + static char buf[6]; + char *p = buf + sizeof(buf) - 1; + unsigned int c; + + *p = '\0'; + do { + c = val % radix; + val = val / radix; + if (c > 9) + *--p = 'a' - 10 + c; + else + *--p = '0' + c; + } while (val); + c = (radix == 16)? '0': ' '; + while (buf + sizeof(buf) - 1 - p < width) + *--p = c; + return p; +} + /* * Very tiny printf to console. * Supports: - * %u unsigned int - * %p unsigned int (displays as unsigned int!) - * %d int (positive numbers only) - * %s char * + * %{0-9} width + * %u unsigned decimal + * %d decimal (positive numbers only) + * %p zero-fill hexadecimal width 4 + * %x hexadecimal + * %s string */ int __dprintf(const char *fmt, ...) { unsigned int n; + int radix, width; char *p; va_list va; char b[80]; @@ -25,14 +49,26 @@ int __dprintf(const char *fmt, ...) va_start(va, fmt); for (n = 0; *fmt; fmt++) { if (*fmt == '%') { - switch (*++fmt) { + ++fmt; + width = 0; + while (*fmt >= '0' && *fmt <= '9') { + width = width * 10 + *fmt - '0'; + fmt++; + } + switch (*fmt) { case 's': p = va_arg(va, char *); goto outstr; - case 'p': /* displays as unsigned int! */ + case 'p': + width = 4; + case 'x': + radix = 16; + goto convert; case 'd': case 'u': - p = uitoa(va_arg(va, unsigned int)); + radix = 10; + convert: + p = uitostr(va_arg(va, unsigned int), radix, width); outstr: while (*p && n < sizeof(b)) b[n++] = *p++; diff --git a/libc/malloc/v7malloc.c b/libc/malloc/v7malloc.c index a7442f4d8..30d824a9e 100644 --- a/libc/malloc/v7malloc.c +++ b/libc/malloc/v7malloc.c @@ -113,7 +113,7 @@ malloc(size_t nbytes) ASSERT(allocp>=allocs && allocp<=alloct); ASSERT(malloc_check_heap()); allocp = (union store __wcnear *)allocs; /* experimental */ - //debug("search start %p ", allocp); + //debug("search start %p ", (unsigned)allocp); for(p=allocp; ; ) { for(temp=0; ; ) { if(!testbusy(p->ptr)) { @@ -184,7 +184,7 @@ allocp = (union store __wcnear *)allocs; /* experimental */ allocp->ptr = p->ptr; } p->ptr = setbusy(allocp); - debug("= %p\n", p); + debug("= %p\n", (unsigned)p); malloc_show_heap(); return((void *)(p+1)); } @@ -223,7 +223,7 @@ realloc(void *ptr, size_t nbytes) if (p == 0) return malloc(nbytes); - debug("(%d)realloc(%p,%u) ", getpid(), p-1, nbytes); + debug("(%d)realloc(%p,%u) ", getpid(), (unsigned)(p-1), nbytes); ASSERT(testbusy(p[-1].ptr)); if(testbusy(p[-1].ptr)) @@ -244,10 +244,10 @@ realloc(void *ptr, size_t nbytes) /* restore old data for special case of malloc link overwrite*/ if(q

=p) { - debug("allocx patch %p,%p,%d ", q, p, nw); + debug("allocx patch %p,%p,%d ", (unsigned)q, (unsigned)p, nw); (q+(q+nw-p))->ptr = allocx; } - debug("= %p\n", q); + debug("= %p\n", (unsigned)q); return((void *)q); } @@ -268,7 +268,7 @@ malloc_check_heap(void) if(p==allocp) x++; } - if (p != alloct) debug("%p %p %p\n", p, alloct, p->ptr); + if (p != alloct) debug("%p %p %p\n", (unsigned)p, (unsigned)alloct, (unsigned)p->ptr); ASSERT(p==alloct); return((x==1)|(p==allocp)); } @@ -285,8 +285,8 @@ malloc_show_heap(void) debug2("--- heap size ---\n"); malloc_check_heap(); for(p = (union store __wcnear *)&allocs[0]; clearbusy(p->ptr) > p; p=clearbusy(p->ptr)) { - size = (char *)clearbusy(p->ptr) - (char *)clearbusy(p); - debug2("%2d: %p %4u", n, p, size); + size = (clearbusy(p->ptr) - clearbusy(p)) * sizeof(union store); + debug2("%2d: %p %4u", n, (unsigned)p, size); if (!testbusy(p->ptr)) { debug2(" (free)"); free += size; @@ -299,7 +299,7 @@ malloc_show_heap(void) debug2("\n"); } alloc += 2; - debug2("%2d: %p %4u (top) ", n, alloct, 2); + debug2("%2d: %p %4u (top) ", n, (unsigned)alloct, 2); debug("alloc %u, free %u, total %u\n", alloc, free, alloc+free); } #endif