Skip to content

Commit

Permalink
[libc] Further fixes to v7malloc
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Dec 14, 2024
1 parent 49aba33 commit e50eb17
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions libc/malloc/v7malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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(q<alloct);
debug("(combine %u and %u) ",
(char *)p->ptr - (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;
}
Expand All @@ -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) {
Expand Down

0 comments on commit e50eb17

Please sign in to comment.