gbowne1 version of implementation with more implementations done #4
99
main.c
99
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)
|
||||
@ -107,8 +107,9 @@ struct block *find_best_fit(size_t s)
|
||||
while (current)
|
||||
{
|
||||
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;
|
||||
}
|
||||
@ -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.
|
||||
@ -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.
|
||||
void my_custom_free(void *ptr)
|
||||
{
|
||||
if (!ptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (!ptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
struct block *b = (struct block *)((char *)ptr - BLOCK_SIZE);
|
||||
if (b->free)
|
||||
{
|
||||
fprintf(stderr, "Double free detected at block %p.\n", ptr);
|
||||
abort(); // Terminate the program immediately due to serious error
|
||||
}
|
||||
struct block *b = (struct block *)((char *)ptr - BLOCK_SIZE);
|
||||
if (b->free)
|
||||
{
|
||||
fprintf(stderr, "Double free detected at block %p.\n", ptr);
|
||||
abort(); // Terminate the program immediately due to serious error
|
||||
}
|
||||
|
||||
b->free = 1;
|
||||
b->free = 1;
|
||||
|
||||
// Coalesce free blocks
|
||||
while (b->prev && b->prev->free)
|
||||
{
|
||||
// Merge with previous block
|
||||
b->prev->size += BLOCK_SIZE + b->size;
|
||||
b->prev->next = b->next;
|
||||
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;
|
||||
}
|
||||
// Coalesce free blocks
|
||||
while (b->prev && b->prev->free)
|
||||
{
|
||||
// Merge with previous block
|
||||
b->prev->size += BLOCK_SIZE + b->size;
|
||||
b->prev->next = b->next;
|
||||
b = b->prev;
|
||||
}
|
||||
|
||||
// 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));
|
||||
}
|
||||
// If there is a next block and it's free, merge with it
|
||||
if (b->next && b->next->free)
|
||||
{
|
||||
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
|
||||
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()
|
||||
|
Loading…
Reference in New Issue
Block a user