fixing issues from PR

This commit is contained in:
Gregory Kenneth Bowne 2024-02-14 16:21:16 -08:00
parent 19b2e31119
commit fe9d9e3836

99
main.c
View File

@ -68,7 +68,7 @@ struct block *find_first(size_t s)
struct block *fragment_block(struct block *in, size_t s) struct block *fragment_block(struct block *in, size_t s)
{ {
// Calculate the size of the new block, including the block header // 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 // Check if the current block can be split
if (in->size <= newBlockSize) if (in->size <= newBlockSize)
@ -107,8 +107,9 @@ struct block *find_best_fit(size_t s)
while (current) while (current)
{ {
if (current->free && current->size >= s) if (current->free && current->size >= s)
{
if (best_fit && current->size < best_fit->size) // Check for NULL before comparison
{ {
if (!best_fit || current->size < best_fit->size)
{ {
best_fit = current; best_fit = current;
} }
@ -116,6 +117,7 @@ struct block *find_best_fit(size_t s)
current = current->next; current = current->next;
} }
return best_fit; return best_fit;
}
} }
/// Will find or allocate a memory block. /// Will find or allocate a memory block.
@ -199,51 +201,62 @@ void *realloc(void *ptr, size_t new_size)
/// @note If all data after the provided memory is free, it will reduce the heap size. /// @note If all data after the provided memory is free, it will reduce the heap size.
void my_custom_free(void *ptr) void my_custom_free(void *ptr)
{ {
if (!ptr) if (!ptr)
{ {
return; return;
} }
struct block *b = (struct block *)((char *)ptr - BLOCK_SIZE); struct block *b = (struct block *)((char *)ptr - BLOCK_SIZE);
if (b->free) if (b->free)
{ {
fprintf(stderr, "Double free detected at block %p.\n", ptr); fprintf(stderr, "Double free detected at block %p.\n", ptr);
abort(); // Terminate the program immediately due to serious error abort(); // Terminate the program immediately due to serious error
} }
b->free = 1; b->free = 1;
// Coalesce free blocks // Coalesce free blocks
while (b->prev && b->prev->free) while (b->prev && b->prev->free)
{ {
// Merge with previous block // Merge with previous block
b->prev->size += BLOCK_SIZE + b->size; b->prev->size += BLOCK_SIZE + b->size;
b->prev->next = b->next; b->prev->next = b->next;
b = b->prev; b = b->prev;
} }
while (b->next && b->next->free)
{
// Merge with next block
b->size += BLOCK_SIZE + b->next->size;
b->next = b->next->next;
}
// Check if we can shrink the heap // If there is a next block and it's free, merge with it
if (b == last) if (b->next && b->next->free)
{ {
// Update 'last' to the previous block or NULL if there's no previous block b->size += BLOCK_SIZE + b->next->size;
last = b->prev; b->next = b->next->next;
if (last) if (b->next)
{ {
last->next = NULL; b->next->prev = b;
} }
else }
{
first = NULL; // After merging, update the 'last' pointer if necessary
} if (!b->next)
// Reduce the program break to release the memory {
sbrk(0 - (b->size + BLOCK_SIZE)); last = b;
} }
// Check if we can shrink the heap
if (b == last)
{
// Update 'last' to the previous block or NULL if there's no previous block
last = b->prev;
if (last)
{
last->next = NULL;
}
else
{
first = NULL;
}
// Reduce the program break to release the memory
sbrk(0 - (b->size + BLOCK_SIZE));
}
} }
int main() int main()