diff --git a/elkscmd/ash/Makefile b/elkscmd/ash/Makefile index 47102757c..29276d26b 100644 --- a/elkscmd/ash/Makefile +++ b/elkscmd/ash/Makefile @@ -26,7 +26,7 @@ OBJS= builtins.o cd.o dirent.o error.o eval.o exec.o expand.o input.o \ linenoise_elks.o autocomplete.o # heap debugging using v7 debug malloc -#OBJS += v7stub.o +#OBJS += dmalloc.o # builtins must be manually added OBJS += bltin/echo.o diff --git a/elkscmd/ash/v7stub.c b/elkscmd/ash/dmalloc.c similarity index 68% rename from elkscmd/ash/v7stub.c rename to elkscmd/ash/dmalloc.c index ef580f950..5e6af1c6b 100644 --- a/elkscmd/ash/v7stub.c +++ b/elkscmd/ash/dmalloc.c @@ -3,15 +3,15 @@ void *malloc(size_t size) { - return __dmalloc(size); + return _dmalloc(size); } void free(void *ptr) { - __dfree(ptr); + _dfree(ptr); } void *realloc(void *ptr, size_t size) { - return __drealloc(ptr, size); + return _drealloc(ptr, size); } diff --git a/elkscmd/tui/Makefile b/elkscmd/tui/Makefile index de0cf5c85..3a225d7a7 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=v7stub.o +MALLOC=dmalloc.o all: $(PRGS) $(PRGS_HOST) diff --git a/elkscmd/tui/v7stub.c b/elkscmd/tui/dmalloc.c similarity index 68% rename from elkscmd/tui/v7stub.c rename to elkscmd/tui/dmalloc.c index ef580f950..5e6af1c6b 100644 --- a/elkscmd/tui/v7stub.c +++ b/elkscmd/tui/dmalloc.c @@ -3,15 +3,15 @@ void *malloc(size_t size) { - return __dmalloc(size); + return _dmalloc(size); } void free(void *ptr) { - __dfree(ptr); + _dfree(ptr); } void *realloc(void *ptr, size_t size) { - return __drealloc(ptr, size); + return _drealloc(ptr, size); } diff --git a/libc/include/malloc.h b/libc/include/malloc.h index 1c3a0f755..82d6704bc 100644 --- a/libc/include/malloc.h +++ b/libc/include/malloc.h @@ -3,26 +3,26 @@ #include -/* default malloc (dev86) */ +/* default malloc (dev86 near heap) */ 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 *); - -/* 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); /* not implemented */ -void __afree(void *); -size_t __amalloc_usable_size(void *); -extern unsigned int malloc_arena_size; -extern unsigned int malloc_arena_thresh; +/* debug malloc (v7 malloc near heap) */ +void *_dmalloc(size_t); +void *_drealloc(void *, size_t); +void _dfree(void *); +size_t _dmalloc_usable_size(void *); + +/* _fmalloc (single arena 64k far heap) */ +void __far *_fmalloc(size_t); +int _fmalloc_add_heap(char __far *start, size_t size); +void __far *_frealloc(void *, size_t); /* not implemented */ +void _ffree(void __far *); +size_t _fmalloc_usable_size(void __far *); +extern unsigned int malloc_arena_size; /* mem.c wrapper function */ +extern unsigned int malloc_arena_thresh; /* mem.c wrapper function */ /* usable with all mallocs */ void *calloc(size_t elm, size_t sz); diff --git a/libc/malloc/Makefile b/libc/malloc/Makefile index 5cb926e1d..2edb04a98 100644 --- a/libc/malloc/Makefile +++ b/libc/malloc/Makefile @@ -26,10 +26,10 @@ DEFAULT_MALLOC_OBJS = \ alloca.o \ # debug malloc (v7) -DEBUG_MALLOC_OBJS = v7malloc.o +DEBUG_MALLOC_OBJS = dmalloc.o -# arena malloc -ARENA_MALLOC_OBJS = amalloc.o +# far malloc (single arena) +FAR_MALLOC_OBJS = fmalloc.o # these objects work with any malloc OBJS = \ @@ -42,9 +42,9 @@ OBJS = \ # default and debug mallocs for all compilers OBJS += $(DEFAULT_MALLOC_OBJS) $(DEBUG_MALLOC_OBJS) -# arena malloc works with OWC only for now +# far malloc works with OWC only for now ifeq "$(COMPILER)" "watcom" -OBJS += $(ARENA_MALLOC_OBJS) +OBJS += $(FAR_MALLOC_OBJS) endif IA16OBJS = \ diff --git a/libc/malloc/v7malloc.c b/libc/malloc/dmalloc.c similarity index 94% rename from libc/malloc/v7malloc.c rename to libc/malloc/dmalloc.c index aafc0641c..54734f8a9 100644 --- a/libc/malloc/v7malloc.c +++ b/libc/malloc/dmalloc.c @@ -1,5 +1,5 @@ /* - * Small malloc/realloc/free with heap checking + * _dmalloc - Small malloc/realloc/free with heap checking * Ported to ELKS from V7 malloc by Greg Haerr 20 Apr 2020 * * Enhancements: @@ -79,7 +79,7 @@ static int malloc_check_heap(void); #endif void * -__dmalloc(size_t nbytes) +_dmalloc(size_t nbytes) { NPTR p, q; unsigned int nw, temp; @@ -89,10 +89,6 @@ __dmalloc(size_t nbytes) #endif if (allocs[0].ptr == 0) { /*first time*/ allocs[0].ptr = setbusy(&allocs[1]); -#if SIZE > 2 - allocs[1].ptr = (NPTR)&allocs[SIZE-2]; - allocs[SIZE-2].ptr = setbusy(&allocs[SIZE-1]); -#endif allocs[SIZE-1].ptr = setbusy(&allocs[0]); alloct = (NPTR)&allocs[SIZE-1]; allocp = (NPTR)&allocs[0]; @@ -149,11 +145,6 @@ __dmalloc(size_t nbytes) break; } -#if SIZE > 2 - debug("Out of fixed heap\n"); - return NULL; -#else - /* extend break at least BLOCK bytes at a time, plus a word for top link */ if (nw < BLOCK/WORD) temp = BLOCK/WORD + 1; @@ -191,7 +182,6 @@ __dmalloc(size_t nbytes) sizeof(union store) + (clearbusy(alloct) - clearbusy(allocs[SIZE-1].ptr)) * sizeof(union store)); next(alloct) = setbusy(allocs); -#endif } found: allocp = p + nw; @@ -209,11 +199,11 @@ __dmalloc(size_t nbytes) /* freeing strategy tuned for LIFO allocation */ void -__dfree(void *ptr) +_dfree(void *ptr) { NPTR p = (NPTR)ptr; - if (p == NULL) + if (ptr == NULL) return; debug("(%d) free(%5u) ", getpid(), (unsigned)(next(p-1) - p) * sizeof(union store)); debug2("= %04x\n", p-1); @@ -226,7 +216,7 @@ __dfree(void *ptr) malloc_show_heap(); } -size_t __dmalloc_usable_size(void *ptr) +size_t _dmalloc_usable_size(void *ptr) { NPTR p = (NPTR)ptr; @@ -244,7 +234,7 @@ size_t __dmalloc_usable_size(void *ptr) * returns new location, or 0 on failure */ void * -__drealloc(void *ptr, size_t nbytes) +_drealloc(void *ptr, size_t nbytes) { NPTR p = (NPTR)ptr; NPTR q; @@ -252,14 +242,14 @@ __drealloc(void *ptr, size_t nbytes) unsigned int nw, onw; if (p == 0) - return __dmalloc(nbytes); + return _dmalloc(nbytes); debug("(%d)realloc(%04x,%u) ", getpid(), (unsigned)(p-1), nbytes); ASSERT(testbusy(next(p-1))); if(testbusy(next(p-1))) - __dfree(p); + _dfree(p); onw = next(p-1) - p; - q = (NPTR)__dmalloc(nbytes); // FIXME and also use memcpy + q = (NPTR)_dmalloc(nbytes); // FIXME and also use memcpy if(q==NULL || q==p) return((void *)q); @@ -312,7 +302,7 @@ malloc_show_heap(void) unsigned int size, alloc = 0, free = 0; static unsigned int maxalloc; - if (!debug_level) return; + if (debug_level < 2) return; debug2("--- heap size ---\n"); malloc_check_heap(); for(p = (NPTR)&allocs[0]; clearbusy(next(p)) > p; p=clearbusy(next(p))) { @@ -322,11 +312,9 @@ malloc_show_heap(void) debug2(" (free)"); free += size; } else { -#if SIZE == 2 if (n < 3) /* don't count ptr to first sbrk()*/ debug2(" (skipped)"); else -#endif alloc += size; } n++; diff --git a/libc/malloc/amalloc.c b/libc/malloc/fmalloc.c similarity index 83% rename from libc/malloc/amalloc.c rename to libc/malloc/fmalloc.c index 029534ef5..3e9b125ea 100644 --- a/libc/malloc/amalloc.c +++ b/libc/malloc/fmalloc.c @@ -1,6 +1,6 @@ /* - * __amalloc - Arena-based heap allocator - provides up to 64k local (far) heap - * Based on __dmalloc (v7 debug malloc). + * _fmalloc - Arena-based far heap allocator - provides up to 64k far heap + * Based on _dmalloc (v7 debug malloc). * 16 Dec 2024 Greg Haerr * * Small malloc/realloc/free with heap checking @@ -86,7 +86,7 @@ static int malloc_check_heap(void); #endif /* add size bytes to arena malloc heap, must be done before first malloc */ -int __amalloc_add_heap(char __far *start, size_t size) +int _fmalloc_add_heap(char __far *start, size_t size) { ASSERT(start != NULL && size >= 16); allocs = (FPTR)start; @@ -103,8 +103,8 @@ int __amalloc_add_heap(char __far *start, size_t size) return 1; } -void * -__amalloc(size_t nbytes) +void __far * +_fmalloc(size_t nbytes) { NPTR p, q; unsigned int nw, temp; @@ -169,50 +169,8 @@ __amalloc(size_t nbytes) } else if(++temp>1) break; } - -#if 1 // SIZE > 2 debug("Out of fixed heap\n"); return NULL; -#else - - /* extend break at least BLOCK bytes at a time, plus a word for top link */ - if (nw < BLOCK/WORD) - temp = BLOCK/WORD + 1; - else - temp = nw + 1; /* NOTE always allocates full req w/o looking at free at top */ - - if (debug_level > 2) malloc_show_heap(); - debug2("sbrk(%d) ", temp*WORD); -#if 0 /* not required and slow, initial break always even */ - q = (NPTR)sbrk(0); - if((INT)q & (sizeof(union store) - 1)) - sbrk(4 - ((INT)q & (sizeof(union store) - 1))); - - /* check possible address wrap - performed in kernel */ - if(q+temp+GRANULE < q) { - debug(" (no more address space) = NULL\n"); - errno = ENOMEM; - return(NULL); - } -#endif - q = (NPTR)sbrk(temp*WORD); - if((INT)q == -1) { - debug(" (no more mem) = NULL\n"); - malloc_show_heap(); - errno = ENOMEM; - return(NULL); - } - ASSERT(!((INT)q & 1)); - ASSERT(q>alloct); - next(alloct) = q; - if(q!=alloct+1) /* mark any gap as permanently allocated*/ - next(alloct) = setbusy(next(alloct)); - alloct = next(q) = q+temp-1; - debug2("(TOTAL %u) ", - sizeof(union store) + - (clearbusy(alloct) - clearbusy(allocs[allocsize-1].ptr)) * sizeof(union store)); - next(alloct) = setbusy((NPTR)allocs); -#endif } found: //__dprintf("n %d, nb %d, f %d\n", n, nb, f); @@ -231,7 +189,7 @@ __amalloc(size_t nbytes) /* freeing strategy tuned for LIFO allocation */ void -__afree(void *ptr) +_ffree(void __far *ptr) { NPTR p = (NPTR)ptr; @@ -249,7 +207,7 @@ __afree(void *ptr) malloc_show_heap(); } -size_t __amalloc_usable_size(void *ptr) +size_t _fmalloc_usable_size(void __far *ptr) { NPTR p = (NPTR)ptr; @@ -269,7 +227,7 @@ size_t __amalloc_usable_size(void *ptr) * returns new location, or 0 on failure */ void * -__arealloc(void *ptr, size_t nbytes) +_frealloc(void __far *ptr, size_t nbytes) { NPTR p = (NPTR)ptr; NPTR q; @@ -277,14 +235,14 @@ __arealloc(void *ptr, size_t nbytes) unsigned int nw, onw; if (p == 0) - return __amalloc(nbytes); + return _fmalloc(nbytes); debug("(%d)realloc(%04x,%u) ", getpid(), (unsigned)(p-1), nbytes); ASSERT(testbusy(next(p-1))); if(testbusy(next(p-1))) - __afree(p); + _ffree(p); onw = next(p-1) - p; - q = (NPTR)__amalloc(nbytes); // FIXME and also use memcpy + q = (NPTR)_fmalloc(nbytes); // FIXME and also use memcpy if(q==NULL || q==p) return((void *)q); @@ -310,7 +268,7 @@ __arealloc(void *ptr, size_t nbytes) #if DEBUG static void malloc_assert_fail(char *s, int line) { - __dprintf("amalloc assert fail: %s (line %d)\n", s, line); + __dprintf("fmalloc assert fail: %s (line %d)\n", s, line); abort(); }