gbowne1-mathfix #6
178
main.c
178
main.c
@ -2,6 +2,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#define ALIGN16(s) (((s) + 15) & ~0x0F)
|
#define ALIGN16(s) (((s) + 15) & ~0x0F)
|
||||||
#define BLOCK_SIZE sizeof(struct block)
|
#define BLOCK_SIZE sizeof(struct block)
|
||||||
@ -24,29 +25,32 @@ struct block *last = NULL;
|
|||||||
/// @returns The new memory block.
|
/// @returns The new memory block.
|
||||||
struct block *extend_heap(size_t s)
|
struct block *extend_heap(size_t s)
|
||||||
{
|
{
|
||||||
// Ensure the allocated size is at least the minimum block size
|
// Ensure the allocated size is at least the minimum block size
|
||||||
if (s < MINIMUM_BLOCK_SIZE)
|
if (s < MINIMUM_BLOCK_SIZE)
|
||||||
s = MINIMUM_BLOCK_SIZE;
|
s = MINIMUM_BLOCK_SIZE;
|
||||||
|
|
||||||
struct block *b = (struct block *)sbrk(0); // Get the current break
|
struct block *b = (struct block *)sbrk(0); // Get the current break
|
||||||
|
|
||||||
// Add the size of the block header to the requested size
|
// Attempt to extend the break by s bytes
|
||||||
s += BLOCK_SIZE;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
if (sbrk(s) == (void *)-1) // Extend the break by s bytes
|
b->size = s;
|
||||||
return NULL;
|
b->prev = last;
|
||||||
|
b->next = NULL;
|
||||||
|
b->free = 0;
|
||||||
|
|
||||||
b->size = s;
|
if (last)
|
||||||
b->prev = last;
|
last->next = b;
|
||||||
b->next = NULL;
|
|
||||||
b->free = 0;
|
|
||||||
|
|
||||||
if (last)
|
last = b;
|
||||||
last->next = b;
|
|
||||||
|
|
||||||
last = b;
|
return b;
|
||||||
|
|
||||||
return b;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Finds the first block that will fit the given size.
|
/// Finds the first block that will fit the given size.
|
||||||
@ -82,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
|
||||||
@ -107,17 +111,15 @@ 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 == 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.
|
||||||
@ -199,82 +201,96 @@ 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)
|
||||||
{
|
{
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there is a next block and it's free, merge with it
|
// If there is a next block and it's free, merge with it
|
||||||
if (b->next && b->next->free)
|
if (b->next && b->next->free)
|
||||||
{
|
{
|
||||||
b->size += BLOCK_SIZE + b->next->size;
|
b->size += BLOCK_SIZE + b->next->size;
|
||||||
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// After merging, update the 'last' pointer if necessary
|
// After merging, update the 'last' pointer if necessary
|
||||||
if (!b->next)
|
if (!b->next)
|
||||||
{
|
{
|
||||||
last = b;
|
last = b;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if we can shrink the heap
|
// Check if we can shrink the heap
|
||||||
if (b == last)
|
if (b == last)
|
||||||
{
|
{
|
||||||
// Update 'last' to the previous block or NULL if there's no previous block
|
// Update 'last' to the previous block or NULL if there's no previous block
|
||||||
last = b->prev;
|
last = b->prev;
|
||||||
if (last)
|
if (last)
|
||||||
{
|
{
|
||||||
last->next = NULL;
|
last->next = NULL;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
first = NULL;
|
first = NULL;
|
||||||
}
|
}
|
||||||
// Reduce the program break to release the memory
|
// Reduce the program break to release the memory
|
||||||
sbrk(0 - (b->size + BLOCK_SIZE));
|
sbrk(0 - (b->size + BLOCK_SIZE));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t get_heap_size()
|
||||||
|
{
|
||||||
|
size_t total_size = 0;
|
||||||
|
struct block *current = first;
|
||||||
|
|
||||||
|
while (current != NULL)
|
||||||
|
{
|
||||||
|
total_size += current->size;
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
|
||||||
|
return total_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int *a = (int *)malloc(sizeof(int));
|
int *a = (int *)malloc(sizeof(int));
|
||||||
int *b = (int *)malloc(sizeof(int));
|
int *b = (int *)malloc(sizeof(int));
|
||||||
|
int *c = (int *)malloc(sizeof(int)); // Allocate memory for c
|
||||||
|
|
||||||
*a = 5;
|
*a = 5;
|
||||||
*b = 12;
|
*b = 12;
|
||||||
|
|
||||||
printf("Test 1: %i\n", *a);
|
printf("Test 1: %i\n", *a);
|
||||||
printf("Test 2: %i\n", *b);
|
printf("Test 2: %i\n", *b);
|
||||||
printf("Heap Size: %zu Bytes\n", get_heap_size());
|
printf("Heap Size: %zu Bytes\n", get_heap_size()); // Assuming get_heap_size is implemented
|
||||||
|
|
||||||
free(a);
|
free(a); // Free memory for a
|
||||||
free(b);
|
free(b); // Free memory for b
|
||||||
|
free(c); // Free memory for c
|
||||||
int *c = (int *)malloc(sizeof(int));
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user