From e50eb17003bcced57d4ed139bba042694988350e Mon Sep 17 00:00:00 2001 From: Greg Haerr Date: Fri, 13 Dec 2024 20:06:47 -0700 Subject: [PATCH] [libc] Further fixes to v7malloc --- libc/malloc/v7malloc.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/libc/malloc/v7malloc.c b/libc/malloc/v7malloc.c index 1256a6840..4e13548c6 100644 --- a/libc/malloc/v7malloc.c +++ b/libc/malloc/v7malloc.c @@ -102,12 +102,13 @@ malloc(size_t nbytes) if (nbytes < MINALLOC) nbytes = MINALLOC; - /* check INT overflow beyond 32764 (nbytes/WORD+WORD+(WORD-1) > 0xFFFF/WORD/WORD)*/ - if (nbytes > ((unsigned)-1)/WORD-WORD-(WORD-1)) { + /* check INT overflow beyond 32762 (nbytes/WORD+WORD+WORD+(WORD-1) > 0xFFFF/WORD/WORD)*/ + if (nbytes > ((unsigned)-1)/WORD-WORD-WORD-(WORD-1)) { debug(" (req too big) = NULL\n"); errno = ENOMEM; return(NULL); } + nw = (nbytes+WORD+WORD-1)/WORD; /* extra word for link ptr/size*/ ASSERT(allocp>=allocs && allocp<=alloct); @@ -118,12 +119,15 @@ allocp = (union store __wcnear *)allocs; /* experimental */ for(temp=0; ; ) { if(!testbusy(p->ptr)) { while(!testbusy((q=p->ptr)->ptr)) { + if (debug_level > 2) malloc_show_heap(); ASSERT(q>p); ASSERT(qptr - (char *)p, (char *)q->ptr - (char *)q); p->ptr = q->ptr; } + debug2("q %04x p %04x nw %d p+nw %04x ", (unsigned)q, (unsigned)p, + nw, (unsigned)(p+nw)); if(q>=p+nw && p+nw>=p) goto found; } @@ -140,17 +144,19 @@ allocp = (union store __wcnear *)allocs; /* experimental */ break; } - /* extend break at least BLOCK bytes at a time*/ + /* extend break at least BLOCK bytes at a time, plus a word for top link */ if (nw < BLOCK/WORD) - temp = BLOCK/WORD; + temp = BLOCK/WORD + 1; else - temp = nw; + temp = nw + 1; /* NOTE always allocates full req w/o looking at free at top */ + if (debug_level > 2) malloc_show_heap(); debug("sbrk(%d) ", temp*WORD); - /* ensure next sbrk returns even address*/ +#if 0 /* not required and slow, initial break always even */ q = (union store __wcnear *)sbrk(0); if((INT)q & (sizeof(union store) - 1)) sbrk(4 - ((INT)q & (sizeof(union store) - 1))); +#endif /* check possible address wrap*/ if(q+temp+GRANULE < q) {