fix: somebugs in malloc

This commit is contained in:
Renge 2022-04-13 22:05:25 -04:00
parent 40f99b2b4b
commit ca01e05272
2 changed files with 21 additions and 27 deletions

View File

@ -3,5 +3,9 @@
int main(int argc, char const *argv[])
{
sf_malloc(200);
sf_malloc(250);
sf_malloc(300);
sf_show_heap();
return EXIT_SUCCESS;
}

View File

@ -75,22 +75,6 @@ void *sf_malloc(sf_size_t size)
{
sf_block *header = &sf_free_list_heads[i];
sf_block *ptr = header->body.links.next;
if (i == index)
{
while (ptr != header)
{
if (get_block_size(ptr->header) >= min_size)
{
set_entire_header(ptr, size, get_block_size(ptr->header), 1, get_prv_alloc(ptr->header), 0, 0);
total_block_size += get_block_size(ptr->header);
add_payload(size);
return ptr->body.payload;
}
ptr = ptr->body.links.next;
}
}
else
{
while (ptr != header)
{
if (get_block_size(ptr->header) - min_size >= min)
@ -105,8 +89,14 @@ void *sf_malloc(sf_size_t size)
add_payload(size);
return ptr->body.payload;
}
ptr = ptr->body.links.next;
else if (get_block_size(ptr->header) >= min_size)
{
set_entire_header(ptr, size, get_block_size(ptr->header), 1, get_prv_alloc(ptr->header), 0, 0);
total_block_size += get_block_size(ptr->header);
add_payload(size);
return ptr->body.payload;
}
ptr = ptr->body.links.next;
}
}