diff --git a/elkscmd/tui/Makefile b/elkscmd/tui/Makefile index 22b1ad2bb..de0cf5c85 100644 --- a/elkscmd/tui/Makefile +++ b/elkscmd/tui/Makefile @@ -13,7 +13,7 @@ PRGS = fm matrix cons ttyinfo sl ttyclock ttypong ttytetris invaders #PRGS_HOST = TUILIB = tty.o runes.o unikey.o -MALLOC=../../libc/malloc/v7malloc.o +MALLOC=v7stub.o all: $(PRGS) $(PRGS_HOST) diff --git a/elkscmd/tui/v7stub.c b/elkscmd/tui/v7stub.c new file mode 100644 index 000000000..ef580f950 --- /dev/null +++ b/elkscmd/tui/v7stub.c @@ -0,0 +1,17 @@ +/* stub to force inclusion of debug malloc (v7) */ +#include + +void *malloc(size_t size) +{ + return __dmalloc(size); +} + +void free(void *ptr) +{ + __dfree(ptr); +} + +void *realloc(void *ptr, size_t size) +{ + return __drealloc(ptr, size); +} diff --git a/libc/include/malloc.h b/libc/include/malloc.h index 769819ad2..2c98a3499 100644 --- a/libc/include/malloc.h +++ b/libc/include/malloc.h @@ -4,26 +4,26 @@ #include /* default malloc (dev86) */ -void *malloc(size_t); -void free(void *); -size_t malloc_usable_size(void *); +void *malloc(size_t); +void *realloc(void *, size_t); +void free(void *); +size_t malloc_usable_size(void *); /* debug malloc (v7 malloc) */ -void *__dmalloc(size_t); -void *__drealloc(void *, size_t); -void __dfree(void *); -size_t __dmalloc_usable_size(void *); +void *__dmalloc(size_t); +void *__drealloc(void *, size_t); +void __dfree(void *); +size_t __dmalloc_usable_size(void *); /* arena malloc (64k near/unlimited far heap) */ -void *__amalloc(size_t); -int __amalloc_add_heap(char __far *start, size_t size); -void *__arealloc(void *, size_t); /* NYI */ -void __afree(void *); -size_t __dmalloc_usable_size(void *); +void *__amalloc(size_t); +int __amalloc_add_heap(char __far *start, size_t size); +void *__arealloc(void *, size_t); /* not implemented */ +void __afree(void *); +size_t __amalloc_usable_size(void *); /* usable with all mallocs */ -void *realloc(void *, size_t); -void *calloc(size_t elm, size_t sz); +void *calloc(size_t elm, size_t sz); /* alloc/free from main memory */ void __far *fmemalloc(unsigned long size); @@ -32,4 +32,7 @@ int fmemfree(void __far *ptr); int _fmemalloc(int paras, unsigned short *pseg); /* syscall */ int _fmemfree(unsigned short seg); /* syscall */ +extern unsigned int malloc_arena_size; +extern unsigned int malloc_arena_thresh; + #endif diff --git a/libc/malloc/Makefile b/libc/malloc/Makefile index 523cb76a1..5e417add6 100644 --- a/libc/malloc/Makefile +++ b/libc/malloc/Makefile @@ -17,6 +17,7 @@ CFLAGS += -DMCHUNK=16 # default malloc (dev86) DEFAULT_MALLOC_OBJS = \ malloc.o \ + realloc.o \ free.o \ __mini_malloc.o \ __alloca_alloc.o \ @@ -32,7 +33,6 @@ ARENA_MALLOC_OBJS = amalloc.o # these objects work with any malloc OBJS = \ - realloc.o \ calloc.o \ brk.o \ sbrk.o \ diff --git a/libc/malloc/amalloc.c b/libc/malloc/amalloc.c index 3a50c1ba3..e10a358dc 100644 --- a/libc/malloc/amalloc.c +++ b/libc/malloc/amalloc.c @@ -238,7 +238,8 @@ __afree(void *ptr) if (p == NULL) return; - debug("(%d) free(%d) = %04x\n", getpid(), (unsigned)(p[-1].ptr - p) << 1, p-1); + debug("(%d) free(%d) = %04x\n", getpid(), + (unsigned)(next(p-1) - p) * sizeof(union store), p-1); ASSERT(FP_SEG(ptr)==allocseg); ASSERT(p>clearbusy(allocs[allocsize-1].ptr)&&p<=alloct); ASSERT(malloc_check_heap()); @@ -253,7 +254,7 @@ size_t __amalloc_usable_size(void *ptr) { NPTR p = (NPTR)ptr; - if (p == NULL) + if (p == NULL) /* NOTE this allows fmemalloc pointers to return 0 here */ return 0; ASSERT(FP_SEG(ptr)==allocseg); ASSERT(p>clearbusy(allocs[allocsize-1].ptr)&&p<=alloct); @@ -280,10 +281,10 @@ __arealloc(void *ptr, size_t nbytes) return __amalloc(nbytes); debug("(%d)realloc(%04x,%u) ", getpid(), (unsigned)(p-1), nbytes); - ASSERT(testbusy(p[-1].ptr)); - if(testbusy(p[-1].ptr)) - free(p); - onw = p[-1].ptr - p; + ASSERT(testbusy(next(p-1))); + if(testbusy(next(p-1))) + __afree(p); + onw = next(p-1) - p; q = (NPTR)__amalloc(nbytes); // FIXME and also use memcpy if(q==NULL || q==p) return((void *)q); diff --git a/libc/malloc/realloc.c b/libc/malloc/realloc.c index 04b5a0eee..77dd8378c 100644 --- a/libc/malloc/realloc.c +++ b/libc/malloc/realloc.c @@ -1,7 +1,6 @@ #include #include -/* this realloc usable with all malloc allocators */ void *realloc(void *ptr, size_t size) { void *nptr; @@ -11,15 +10,17 @@ void *realloc(void *ptr, size_t size) return malloc(size); osize = malloc_usable_size(ptr); -#if LATER +#if 0 if (size <= osize) - return ptr; + return ptr; /* don't reallocate, do nothing */ +#else + if (size <= osize) + osize = size; /* copy less bytes in memcpy below */ #endif nptr = malloc(size); if (nptr == 0) return 0; - memcpy(nptr, ptr, osize); free(ptr); diff --git a/libc/malloc/v7malloc.c b/libc/malloc/v7malloc.c index 08662534e..4430c3d5c 100644 --- a/libc/malloc/v7malloc.c +++ b/libc/malloc/v7malloc.c @@ -217,7 +217,8 @@ __dfree(void *ptr) if (p == NULL) return; - debug("(%d) free(%d) = %04x\n", getpid(), (unsigned)(p[-1].ptr - p) << 1, p-1); + debug("(%d) free(%d) = %04x\n", getpid(), + (unsigned)(next(p-1) - p) * sizeof(union store), p-1); ASSERT(p>clearbusy(allocs[SIZE-1].ptr)&&p<=alloct); ASSERT(malloc_check_heap()); allocp = --p; @@ -256,10 +257,10 @@ __drealloc(void *ptr, size_t nbytes) return __dmalloc(nbytes); debug("(%d)realloc(%04x,%u) ", getpid(), (unsigned)(p-1), nbytes); - ASSERT(testbusy(p[-1].ptr)); - if(testbusy(p[-1].ptr)) - free(p); - onw = p[-1].ptr - p; + ASSERT(testbusy(next(p-1))); + if(testbusy(next(p-1))) + __dfree(p); + onw = next(p-1) - p; q = (NPTR)__dmalloc(nbytes); // FIXME and also use memcpy if(q==NULL || q==p) return((void *)q);