Skip to content

Commit

Permalink
[libc] Fixes to v7malloc.c for OpenWatcom
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Dec 13, 2024
1 parent 896b0b6 commit b9b1eca
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
50 changes: 43 additions & 7 deletions libc/malloc/dprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,41 @@
#include <paths.h>
#include <fcntl.h>

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];
Expand All @@ -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++;
Expand Down
18 changes: 9 additions & 9 deletions libc/malloc/v7malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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))
Expand All @@ -244,10 +244,10 @@ realloc(void *ptr, size_t nbytes)

/* restore old data for special case of malloc link overwrite*/
if(q<p && q+nw>=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);
}

Expand All @@ -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));
}
Expand All @@ -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;
Expand All @@ -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

0 comments on commit b9b1eca

Please sign in to comment.