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,38 +75,28 @@ 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)
{
while (ptr != header)
if (get_block_size(ptr->header) - min_size >= min)
{
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;
remove_list(ptr);
sf_block *block = split_block(ptr, min_size);
sf_size_t block_size = get_block_size(block->header);
set_entire_header(ptr, size, min_size, 1, get_prv_alloc(ptr->header), 0, 0);
set_entire_header(block, 0, block_size, 0, get_prv_alloc(block->header), 0, 0);
put_block(block);
total_block_size += get_block_size(ptr->header);
add_payload(size);
return ptr->body.payload;
}
}
else
{
while (ptr != header)
else if (get_block_size(ptr->header) >= min_size)
{
if (get_block_size(ptr->header) - min_size >= min)
{
remove_list(ptr);
sf_block *block = split_block(ptr, min_size);
sf_size_t block_size = get_block_size(block->header);
set_entire_header(ptr, size, min_size, 1, get_prv_alloc(ptr->header), 0, 0);
set_entire_header(block, 0, block_size, 0, get_prv_alloc(block->header), 0, 0);
put_block(block);
total_block_size += get_block_size(ptr->header);
add_payload(size);
return ptr->body.payload;
}
ptr = ptr->body.links.next;
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;
}
}