fixing issues from PR
This commit is contained in:
parent
19b2e31119
commit
fe9d9e3836
21
main.c
21
main.c
@ -68,7 +68,7 @@ struct block *find_first(size_t s)
|
||||
struct block *fragment_block(struct block *in, size_t s)
|
||||
{
|
||||
// Calculate the size of the new block, including the block header
|
||||
size_t newBlockSize = ALIGN16(s) + BLOCK_SIZE;
|
||||
size_t newBlockSize = s + BLOCK_SIZE;
|
||||
|
||||
// Check if the current block can be split
|
||||
if (in->size <= newBlockSize)
|
||||
@ -108,7 +108,8 @@ struct block *find_best_fit(size_t s)
|
||||
{
|
||||
if (current->free && current->size >= s)
|
||||
{
|
||||
if (!best_fit || current->size < best_fit->size)
|
||||
if (best_fit && current->size < best_fit->size) // Check for NULL before comparison
|
||||
{
|
||||
{
|
||||
best_fit = current;
|
||||
}
|
||||
@ -116,6 +117,7 @@ struct block *find_best_fit(size_t s)
|
||||
current = current->next;
|
||||
}
|
||||
return best_fit;
|
||||
}
|
||||
}
|
||||
|
||||
/// Will find or allocate a memory block.
|
||||
@ -221,11 +223,22 @@ void my_custom_free(void *ptr)
|
||||
b->prev->next = b->next;
|
||||
b = b->prev;
|
||||
}
|
||||
while (b->next && b->next->free)
|
||||
|
||||
// If there is a next block and it's free, merge with it
|
||||
if (b->next && b->next->free)
|
||||
{
|
||||
// Merge with next block
|
||||
b->size += BLOCK_SIZE + b->next->size;
|
||||
b->next = b->next->next;
|
||||
if (b->next)
|
||||
{
|
||||
b->next->prev = b;
|
||||
}
|
||||
}
|
||||
|
||||
// After merging, update the 'last' pointer if necessary
|
||||
if (!b->next)
|
||||
{
|
||||
last = b;
|
||||
}
|
||||
|
||||
// Check if we can shrink the heap
|
||||
|
Loading…
Reference in New Issue
Block a user