fix: add to free list while increasing size

This commit is contained in:
Renge 2022-03-25 23:16:50 -04:00
parent cc31d8604b
commit 908663e721

View File

@ -105,25 +105,24 @@ void *sf_malloc(sf_size_t size)
sf_block *last = get_prev_block(epi); sf_block *last = get_prev_block(epi);
if (get_alloc(last->header) == 1) if (get_alloc(last->header) == 1)
last = epi; last = epi;
if (last->body.links.next != NULL) while (get_block_size(last->header) < min_size)
{
remove_list(last);
}
sf_size_t require_size = min_size - get_block_size(last->header);
size_t i = 0;
for (; i < require_size; i += 1024)
{ {
if (sf_mem_grow() == NULL) if (sf_mem_grow() == NULL)
{ {
sf_errno = ENOMEM; sf_errno = ENOMEM;
return NULL; return NULL;
} }
if (last->body.links.next != NULL)
remove_list(last);
set_entire_header(last, 0, (get_block_size(last->header) + PAGE_SZ), 0, get_prv_alloc(last->header), 0);
put_block(last);
} }
set_entire_header(last, 0, (get_block_size(last->header) + i), 0, get_prv_alloc(last->header), 0);
sf_block *ptr = last; sf_block *ptr = last;
if (get_block_size(last->header) - min_size >= 32) remove_list(last);
if (get_block_size(last->header) - min_size >= min)
{ {
ptr = split_block(last, min_size); ptr = split_block(last, min_size);
set_entire_header(ptr, 0, (get_block_size(ptr->header)), 0, get_prv_alloc(ptr->header), 0);
put_block(ptr); put_block(ptr);
} }
set_entire_header(last, size, get_block_size(last->header), 1, get_prv_alloc(last->header), 0); set_entire_header(last, size, get_block_size(last->header), 1, get_prv_alloc(last->header), 0);
@ -386,7 +385,7 @@ sf_size_t get_index(sf_size_t size)
{ {
size_t index = 0; size_t index = 0;
for (; index < (NUM_FREE_LISTS - 1); index++) for (; index < (NUM_FREE_LISTS - 1); index++)
if (size <= get_pow(index)*32) if (size <= get_pow(index) * 32)
break; break;
return index; return index;
} }