diff --git a/libc/malloc/Makefile b/libc/malloc/Makefile index 5e417add6..23a5fdc6a 100644 --- a/libc/malloc/Makefile +++ b/libc/malloc/Makefile @@ -37,7 +37,6 @@ OBJS = \ brk.o \ sbrk.o \ fmemalloc.o \ - fmemfree.o \ dprintf.o \ # default and debug mallocs available for ia16 and OWC diff --git a/libc/malloc/amalloc.c b/libc/malloc/amalloc.c index 2cd8001e9..47bab2875 100644 --- a/libc/malloc/amalloc.c +++ b/libc/malloc/amalloc.c @@ -9,7 +9,7 @@ * Enhancements: * Minimum BLOCK allocate from kernel sbrk, > BLOCK allocates requested size * Much improved size and heap overflow handling with errno returns - * Full heap integrity checking and reporting with DEBUG options + * Full heap integrity checking and reporting with debug options * Use near heap pointers to work with OpenWatcom large model * Combine free areas at heap start before allocating from free area at end of heap */ diff --git a/libc/malloc/dprintf.c b/libc/malloc/dprintf.c index eda959cff..c83fd1050 100644 --- a/libc/malloc/dprintf.c +++ b/libc/malloc/dprintf.c @@ -59,7 +59,10 @@ int __dprintf(const char *fmt, ...) width = width * 10 + *fmt - '0'; fmt++; } + again: switch (*fmt) { + case 'l': + fmt++; goto again; case 's': p = va_arg(va, char *); goto outstr; diff --git a/libc/malloc/fmemalloc.c b/libc/malloc/fmemalloc.c index 840d47787..27bb223e4 100644 --- a/libc/malloc/fmemalloc.c +++ b/libc/malloc/fmemalloc.c @@ -1,14 +1,53 @@ #include +#include +#include +#include +/* fmemalloc/fmemfree - allocate/free from main memory */ -#define _MK_FP(seg,off) ((void __far *)((((unsigned long)(seg)) << 16) | (off))) +#define DEBUG 1 /* =1 use sysctl, =2 debug output */ + +#if DEBUG +#define debug(...) do { if (debug_level > 1) __dprintf(__VA_ARGS__); } while (0) +static int debug_level = DEBUG; +static unsigned long total; +static unsigned long maxsize; +#else +#define debug(...) +#endif + +#define FP_SEG(fp) ((unsigned)((unsigned long)(void __far *)(fp) >> 16)) +#define FP_OFF(fp) ((unsigned)(unsigned long)(void __far *)(fp)) +#define MK_FP(seg,off) ((void __far *)((((unsigned long)(seg)) << 16) | \ + ((unsigned int)(off)))) -/* alloc from main memory */ void __far *fmemalloc(unsigned long size) { unsigned short seg; unsigned int paras = (unsigned int)((size + 15) >> 4); - if (_fmemalloc(paras, &seg)) +#if DEBUG == 1 + sysctl(CTL_GET, "malloc.debug", &debug_level); +#endif + debug("(%d)FMEMALLOC(%5lu) ", getpid(), size); + if (_fmemalloc(paras, &seg)) { + debug("= FAIL\n"); return 0; - return _MK_FP(seg, 0); + } +#if DEBUG + total += size; + if (size > maxsize) maxsize = size; + debug("total %lu, maxsize %lu\n", (unsigned)total, (unsigned)maxsize); +#endif + return MK_FP(seg, 0); +} + +int fmemfree(void __far *ptr) +{ + debug("(%d) fmemfree()\n", getpid()); // FIXME get size of allocation + if (FP_OFF(ptr)) { + debug(" FAIL (bad pointer)\n"); // FIXME convert to ASSERT + errno = EINVAL; + return -1; + } + return _fmemfree(FP_SEG(ptr)); } diff --git a/libc/malloc/fmemfree.c b/libc/malloc/fmemfree.c deleted file mode 100644 index 05ad3c90e..000000000 --- a/libc/malloc/fmemfree.c +++ /dev/null @@ -1,7 +0,0 @@ -#include - -/* free from main memory */ -int fmemfree(void __far *ptr) -{ - return _fmemfree((unsigned short)((unsigned long)ptr >> 16)); -} diff --git a/libc/malloc/v7malloc.c b/libc/malloc/v7malloc.c index 04842ffff..dfdfa6979 100644 --- a/libc/malloc/v7malloc.c +++ b/libc/malloc/v7malloc.c @@ -5,7 +5,7 @@ * Enhancements: * Minimum BLOCK allocate from kernel sbrk, > BLOCK allocates requested size * Much improved size and heap overflow handling with errno returns - * Full heap integrity checking and reporting with DEBUG options + * Full heap integrity checking and reporting with debug options * Use near heap pointers to work with OpenWatcom large model * Combine free areas at heap start before allocating from free area at end of heap */