gbowne1-mathfix #6
24
main.c
24
main.c
@ -31,10 +31,16 @@ struct block *extend_heap(size_t s)
|
|||||||
|
|
||||||
struct block *b = (struct block *)sbrk(0); // Get the current break
|
struct block *b = (struct block *)sbrk(0); // Get the current break
|
||||||
|
|
||||||
if (sbrk(BLOCK_SIZE + s) == (void *)-1) // Extend the break by s bytes
|
// Attempt to extend the break by s bytes
|
||||||
|
void *new_break = sbrk(BLOCK_SIZE + s);
|
||||||
|
if (new_break == (void *)-1)
|
||||||
|
{
|
||||||
|
// Handle the error, e.g., by setting an error code or printing an error message
|
||||||
|
fprintf(stderr, "Failed to extend heap by %zu bytes.\n", s);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
b->size = s; // Corrected from b->size to b->size
|
b->size = s;
|
||||||
b->prev = last;
|
b->prev = last;
|
||||||
b->next = NULL;
|
b->next = NULL;
|
||||||
b->free = 0;
|
b->free = 0;
|
||||||
@ -80,13 +86,13 @@ struct block *fragment_block(struct block *in, size_t s)
|
|||||||
|
|
||||||
// Create the new block in the remainder space
|
// Create the new block in the remainder space
|
||||||
struct block *newBlock = (struct block *)((char *)(in + 1) + s);
|
struct block *newBlock = (struct block *)((char *)(in + 1) + s);
|
||||||
newBlock->size = remainderSize;
|
newBlock->size = remainderSize; // Subtract the size of the block header
|
||||||
newBlock->prev = in;
|
newBlock->prev = in;
|
||||||
newBlock->next = in->next;
|
newBlock->next = in->next;
|
||||||
newBlock->free = 1; // Set the new block as free
|
newBlock->free = 1; // Set the new block as free
|
||||||
|
|
||||||
// Update the current block to reflect the reduced size
|
// Update the current block to reflect the reduced size
|
||||||
in->size = newBlockSize;
|
in->size = newBlockSize; // Subtract the size of the block header
|
||||||
in->next = newBlock;
|
in->next = newBlock;
|
||||||
|
|
||||||
// Insert the new block into the linked list of blocks
|
// Insert the new block into the linked list of blocks
|
||||||
@ -106,16 +112,14 @@ struct block *find_best_fit(size_t s)
|
|||||||
{
|
{
|
||||||
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 == NULL || current->size < best_fit->size)
|
||||||
{
|
|
||||||
{
|
{
|
||||||
best_fit = current;
|
best_fit = current;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
}
|
||||||
return best_fit;
|
return best_fit; // This return statement is now outside the while loop
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Will find or allocate a memory block.
|
/// Will find or allocate a memory block.
|
||||||
@ -197,7 +201,7 @@ void *realloc(void *ptr, size_t new_size)
|
|||||||
/// Will flag the provided memory as free and will defragment other blocks adjacent to it.
|
/// Will flag the provided memory as free and will defragment other blocks adjacent to it.
|
||||||
/// @param [in] ptr The memory to flag as free.
|
/// @param [in] ptr The memory to flag as free.
|
||||||
/// @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 free(void *ptr)
|
||||||
{
|
{
|
||||||
if (!ptr)
|
if (!ptr)
|
||||||
{
|
{
|
||||||
@ -229,7 +233,7 @@ void my_custom_free(void *ptr)
|
|||||||
b->next = b->next->next;
|
b->next = b->next->next;
|
||||||
if (b->next)
|
if (b->next)
|
||||||
{
|
{
|
||||||
b->next->prev = b;
|
b->next->prev = b; // Update the prev pointer of the next block
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user