gbowne1 version of implementation with more implementations done #4
233
main.c
233
main.c
@ -1,36 +1,39 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/syscall.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define ALIGN4(s) (((((s)-1)>>2)<<2)+4)
|
#define ALIGN16(s) (((s) + 15) & ~0x0F)
|
||||||
#define BLOCK_SIZE sizeof(struct block)
|
#define BLOCK_SIZE sizeof(struct block)
|
||||||
#define MINIMUM_BLOCK_SIZE 4
|
#define MINIMUM_BLOCK_SIZE (sizeof(struct block) + 16)
|
||||||
|
|
||||||
/// The memory block's header.
|
/// The memory block's header.
|
||||||
struct block
|
struct block
|
||||||
{
|
{
|
||||||
size_t size;
|
size_t size;
|
||||||
struct block* prev;
|
struct block *prev;
|
||||||
struct block* next;
|
struct block *next;
|
||||||
int free;
|
int free;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct block* first = NULL;
|
struct block *first = NULL;
|
||||||
struct block* last = NULL;
|
struct block *last = NULL;
|
||||||
|
|
||||||
/// Extends heap memory upwards, towards zero.
|
/// Extends heap memory upwards, towards zero.
|
||||||
/// @param [in] s The size of the memory needed aligned by 4 bytes.
|
/// @param [in] s The size of the memory needed aligned by 4 bytes.
|
||||||
/// @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*)syscall(SYS_brk, NULL);
|
struct block *b = (struct block *)sbrk(0); // Get the current break
|
||||||
|
|
||||||
if ((void*)syscall(SYS_brk, (char*)(b + 1) + s) == (void*)-1)
|
// Add the size of the block header to the requested size
|
||||||
|
s += BLOCK_SIZE;
|
||||||
|
|
||||||
|
if (sbrk(s) == (void *)-1) // Extend the break by s bytes
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
b->size = s;
|
b->size = s;
|
||||||
@ -49,9 +52,9 @@ struct block* extend_heap(size_t s)
|
|||||||
/// Finds the first block that will fit the given size.
|
/// Finds the first block that will fit the given size.
|
||||||
/// @param [in] s The 4 byte aligned size to look for.
|
/// @param [in] s The 4 byte aligned size to look for.
|
||||||
/// @returns The matching available memory block.
|
/// @returns The matching available memory block.
|
||||||
struct block* find_first(size_t s)
|
struct block *find_first(size_t s)
|
||||||
{
|
{
|
||||||
struct block* current = first;
|
struct block *current = first;
|
||||||
while (current && (!current->free || current->size < s))
|
while (current && (!current->free || current->size < s))
|
||||||
current = current->next;
|
current = current->next;
|
||||||
|
|
||||||
@ -62,112 +65,204 @@ struct block* find_first(size_t s)
|
|||||||
/// @param [in] in The memory block to fragment.
|
/// @param [in] in The memory block to fragment.
|
||||||
/// @param [in] s The size of the new memory block.
|
/// @param [in] s The size of the new memory block.
|
||||||
/// @returns The new memory block.
|
/// @returns The new memory block.
|
||||||
struct block* fragment_block(struct block* in, size_t s)
|
struct block *fragment_block(struct block *in, size_t s)
|
||||||
{
|
{
|
||||||
size_t totalSize = BLOCK_SIZE + s;
|
// Calculate the size of the new block, including the block header
|
||||||
struct block* b = (struct block*)((char*)(in + 1) + (in->size - totalSize));
|
size_t newBlockSize = s + BLOCK_SIZE;
|
||||||
b->size = s;
|
|
||||||
b->prev = in;
|
|
||||||
b->next = in->next;
|
|
||||||
b->free = 0;
|
|
||||||
|
|
||||||
in->size -= totalSize;
|
// Check if the current block can be split
|
||||||
in->next = b;
|
if (in->size <= newBlockSize)
|
||||||
|
{
|
||||||
|
// Cannot split, return the original block
|
||||||
|
return in;
|
||||||
|
}
|
||||||
|
|
||||||
return b;
|
// Calculate the size of the remainder block
|
||||||
|
size_t remainderSize = in->size - newBlockSize;
|
||||||
|
|
||||||
|
// Create the new block in the remainder space
|
||||||
|
struct block *newBlock = (struct block *)((char *)(in + 1) + s);
|
||||||
|
newBlock->size = remainderSize;
|
||||||
|
newBlock->prev = in;
|
||||||
|
newBlock->next = in->next;
|
||||||
|
newBlock->free = 1; // Set the new block as free
|
||||||
|
|
||||||
|
// Update the current block to reflect the reduced size
|
||||||
|
in->size = newBlockSize;
|
||||||
|
in->next = newBlock;
|
||||||
|
|
||||||
|
// Insert the new block into the linked list of blocks
|
||||||
|
if (newBlock->next)
|
||||||
|
{
|
||||||
|
newBlock->next->prev = newBlock;
|
||||||
|
}
|
||||||
|
|
||||||
|
return newBlock;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct block *find_best_fit(size_t s)
|
||||||
|
{
|
||||||
|
struct block *current = first;
|
||||||
|
struct block *best_fit = NULL;
|
||||||
|
while (current)
|
||||||
|
{
|
||||||
|
if (current->free && current->size >= s)
|
||||||
|
{
|
||||||
|
if (best_fit && current->size < best_fit->size) // Check for NULL before comparison
|
||||||
|
{
|
||||||
|
{
|
||||||
|
best_fit = current;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
current = current->next;
|
||||||
|
}
|
||||||
|
return best_fit;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Will find or allocate a memory block.
|
/// Will find or allocate a memory block.
|
||||||
/// @param [in] size The size of the memory block to request.
|
/// @param [in] size The size of the memory block to request.
|
||||||
/// @returns The requested memory on the heap.
|
/// @returns The requested memory on the heap.
|
||||||
/// @todo Fragmenting functionality.
|
/// @todo Fragmenting functionality.
|
||||||
void* malloc(size_t size)
|
void *malloc(size_t size)
|
||||||
{
|
{
|
||||||
struct block* b;
|
if (size == 0)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
size = ALIGN4(size);
|
size = ALIGN16(size); // First align the requested size
|
||||||
|
size_t total_size = size + BLOCK_SIZE; // Then add the size of the block header
|
||||||
|
|
||||||
|
struct block *b;
|
||||||
|
|
||||||
if (first)
|
if (first)
|
||||||
{
|
{
|
||||||
b = find_first(size);
|
b = find_best_fit(size);
|
||||||
if (!b)
|
if (!b)
|
||||||
b = extend_heap(size);
|
{
|
||||||
else if (b->size > BLOCK_SIZE + size)
|
b = extend_heap(total_size);
|
||||||
|
if (!b)
|
||||||
|
{
|
||||||
|
return NULL; // Check if heap extension failed
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (b->size > total_size + MINIMUM_BLOCK_SIZE)
|
||||||
|
{
|
||||||
b = fragment_block(b, size);
|
b = fragment_block(b, size);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
b = extend_heap(size);
|
b = extend_heap(total_size);
|
||||||
if (!b)
|
if (!b)
|
||||||
|
{
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
first = b;
|
first = b;
|
||||||
}
|
}
|
||||||
|
|
||||||
return b + 1;
|
b->free = 0; // Mark the block as used
|
||||||
|
return (char *)b + BLOCK_SIZE; // Return a pointer to the usable memory
|
||||||
}
|
}
|
||||||
|
|
||||||
void* realloc(void* ptr, size_t new_size)
|
void *realloc(void *ptr, size_t new_size)
|
||||||
{
|
{
|
||||||
if (!ptr)
|
if (!ptr)
|
||||||
|
{
|
||||||
return malloc(new_size);
|
return malloc(new_size);
|
||||||
|
}
|
||||||
|
|
||||||
if (!new_size)
|
if (new_size == 0)
|
||||||
|
{
|
||||||
|
free(ptr);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
struct block* b = (struct block*)ptr - 1;
|
struct block *b = (struct block *)((char *)ptr - BLOCK_SIZE);
|
||||||
if (b->free)
|
if (b->size >= new_size + BLOCK_SIZE)
|
||||||
return NULL;
|
{
|
||||||
|
return ptr; // The block is already big enough
|
||||||
|
}
|
||||||
|
|
||||||
if (b->size == new_size)
|
void *new_ptr = malloc(new_size);
|
||||||
return ptr;
|
if (!new_ptr)
|
||||||
else if (b->size > BLOCK_SIZE + new_size)
|
{
|
||||||
return fragment_block(b, new_size) + 1;
|
return NULL; // Allocation failed
|
||||||
|
}
|
||||||
|
memcpy(new_ptr, ptr, b->size - BLOCK_SIZE); // Copy old data to new block, excluding the header size
|
||||||
|
free(ptr); // Free the old block
|
||||||
|
|
||||||
return NULL;
|
return new_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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 free(void* ptr)
|
void my_custom_free(void *ptr)
|
||||||
{
|
{
|
||||||
if (!ptr)
|
if (!ptr)
|
||||||
return;
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
struct block* b = (struct block*)ptr - 1;
|
struct block *b = (struct block *)((char *)ptr - BLOCK_SIZE);
|
||||||
if (b->free)
|
if (b->free)
|
||||||
return;
|
{
|
||||||
|
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;
|
||||||
|
|
||||||
while (b->prev && b->prev->free)
|
// Coalesce free blocks
|
||||||
b = b->prev;
|
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)
|
// 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;
|
b->size += BLOCK_SIZE + b->next->size;
|
||||||
}
|
b->next = b->next->next;
|
||||||
|
if (b->next)
|
||||||
|
{
|
||||||
|
b->next->prev = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!b->next)
|
// After merging, update the 'last' pointer if necessary
|
||||||
syscall(SYS_brk, b);
|
if (!b->next)
|
||||||
}
|
{
|
||||||
|
last = b;
|
||||||
|
}
|
||||||
|
|
||||||
/// Retrieves the total heap usage in bytes.
|
// Check if we can shrink the heap
|
||||||
/// @returns Usage in bytes.
|
if (b == last)
|
||||||
size_t get_heap_size()
|
{
|
||||||
{
|
// Update 'last' to the previous block or NULL if there's no previous block
|
||||||
if (!first || !last)
|
last = b->prev;
|
||||||
return 0;
|
if (last)
|
||||||
|
{
|
||||||
return ((char*)last + last->size) - (char*)first;
|
last->next = NULL;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
first = NULL;
|
||||||
|
}
|
||||||
|
// Reduce the program break to release the memory
|
||||||
|
sbrk(0 - (b->size + BLOCK_SIZE));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int* a = (int*)malloc(1);
|
int *a = (int *)malloc(sizeof(int));
|
||||||
int* b = (int*)malloc(1);
|
int *b = (int *)malloc(sizeof(int));
|
||||||
|
|
||||||
*a = 5;
|
*a = 5;
|
||||||
*b = 12;
|
*b = 12;
|
||||||
@ -179,7 +274,7 @@ int main()
|
|||||||
free(a);
|
free(a);
|
||||||
free(b);
|
free(b);
|
||||||
|
|
||||||
int* c = (int*)malloc(sizeof(int));
|
int *c = (int *)malloc(sizeof(int));
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user