Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc] Further fixes to v7malloc #2132

Merged
merged 1 commit into from
Dec 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading