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[]) int main(int argc, char const *argv[])
{ {
sf_malloc(200);
sf_malloc(250);
sf_malloc(300);
sf_show_heap();
return EXIT_SUCCESS; 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 *header = &sf_free_list_heads[i];
sf_block *ptr = header->body.links.next; 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) remove_list(ptr);
{ sf_block *block = split_block(ptr, min_size);
set_entire_header(ptr, size, get_block_size(ptr->header), 1, get_prv_alloc(ptr->header), 0, 0); sf_size_t block_size = get_block_size(block->header);
total_block_size += get_block_size(ptr->header); set_entire_header(ptr, size, min_size, 1, get_prv_alloc(ptr->header), 0, 0);
add_payload(size); set_entire_header(block, 0, block_size, 0, get_prv_alloc(block->header), 0, 0);
return ptr->body.payload; put_block(block);
} total_block_size += get_block_size(ptr->header);
ptr = ptr->body.links.next; add_payload(size);
return ptr->body.payload;
} }
} else if (get_block_size(ptr->header) >= min_size)
else
{
while (ptr != header)
{ {
if (get_block_size(ptr->header) - min_size >= min) 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);
remove_list(ptr); add_payload(size);
sf_block *block = split_block(ptr, min_size); return ptr->body.payload;
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;
} }
ptr = ptr->body.links.next;
} }
} }