2024-02-07 23:15:44 -08:00
|
|
|
#include <stddef.h>
|
2024-02-08 01:44:09 -08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2024-02-14 00:21:54 -08:00
|
|
|
#include <stdlib.h>
|
2024-02-07 23:15:44 -08:00
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
#define ALIGN16(s) (((s) + 15) & ~0x0F)
|
2024-02-08 01:44:09 -08:00
|
|
|
#define BLOCK_SIZE sizeof(struct block)
|
2024-02-14 00:21:54 -08:00
|
|
|
#define MINIMUM_BLOCK_SIZE (sizeof(struct block) + 16)
|
2024-02-08 01:44:09 -08:00
|
|
|
|
2024-02-08 02:05:06 -08:00
|
|
|
/// The memory block's header.
|
2024-02-08 01:44:09 -08:00
|
|
|
struct block
|
|
|
|
{
|
|
|
|
size_t size;
|
2024-02-14 00:21:54 -08:00
|
|
|
struct block *prev;
|
|
|
|
struct block *next;
|
2024-02-08 01:44:09 -08:00
|
|
|
int free;
|
|
|
|
};
|
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
struct block *first = NULL;
|
|
|
|
struct block *last = NULL;
|
2024-02-08 01:44:09 -08:00
|
|
|
|
2024-02-08 02:00:57 -08:00
|
|
|
/// Extends heap memory upwards, towards zero.
|
|
|
|
/// @param [in] s The size of the memory needed aligned by 4 bytes.
|
|
|
|
/// @returns The new memory block.
|
2024-02-14 00:21:54 -08:00
|
|
|
struct block *extend_heap(size_t s)
|
2024-02-08 01:44:09 -08:00
|
|
|
{
|
2024-02-14 00:21:54 -08:00
|
|
|
// Ensure the allocated size is at least the minimum block size
|
|
|
|
if (s < MINIMUM_BLOCK_SIZE)
|
|
|
|
s = MINIMUM_BLOCK_SIZE;
|
2024-02-08 01:44:09 -08:00
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
struct block *b = (struct block *)sbrk(0); // Get the current break
|
|
|
|
|
|
|
|
// 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
|
2024-02-08 01:44:09 -08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
b->size = s;
|
2024-02-08 17:04:41 -08:00
|
|
|
b->prev = last;
|
2024-02-08 01:44:09 -08:00
|
|
|
b->next = NULL;
|
|
|
|
b->free = 0;
|
|
|
|
|
|
|
|
if (last)
|
|
|
|
last->next = b;
|
|
|
|
|
2024-02-08 16:44:26 -08:00
|
|
|
last = b;
|
|
|
|
|
2024-02-08 01:44:09 -08:00
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2024-02-08 02:00:57 -08:00
|
|
|
/// Finds the first block that will fit the given size.
|
|
|
|
/// @param [in] s The 4 byte aligned size to look for.
|
|
|
|
/// @returns The matching available memory block.
|
2024-02-14 00:21:54 -08:00
|
|
|
struct block *find_first(size_t s)
|
2024-02-08 01:44:09 -08:00
|
|
|
{
|
2024-02-14 00:21:54 -08:00
|
|
|
struct block *current = first;
|
2024-02-08 01:44:09 -08:00
|
|
|
while (current && (!current->free || current->size < s))
|
|
|
|
current = current->next;
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2024-02-08 17:42:03 -08:00
|
|
|
/// Fragments an existing free memory block into the given size.
|
|
|
|
/// @param [in] in The memory block to fragment.
|
|
|
|
/// @param [in] s The size of the new memory block.
|
|
|
|
/// @returns The new memory block.
|
2024-02-14 00:21:54 -08:00
|
|
|
struct block *fragment_block(struct block *in, size_t s)
|
2024-02-08 17:24:49 -08:00
|
|
|
{
|
2024-02-14 00:21:54 -08:00
|
|
|
// Calculate the size of the new block, including the block header
|
|
|
|
size_t newBlockSize = ALIGN16(s) + BLOCK_SIZE;
|
2024-02-08 17:24:49 -08:00
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
// Check if the current block can be split
|
|
|
|
if (in->size <= newBlockSize)
|
|
|
|
{
|
|
|
|
// Cannot split, return the original block
|
|
|
|
return in;
|
|
|
|
}
|
2024-02-08 17:24:49 -08:00
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
// 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)
|
|
|
|
{
|
|
|
|
best_fit = current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
return best_fit;
|
2024-02-08 17:24:49 -08:00
|
|
|
}
|
|
|
|
|
2024-02-08 02:00:57 -08:00
|
|
|
/// Will find or allocate a memory block.
|
|
|
|
/// @param [in] size The size of the memory block to request.
|
|
|
|
/// @returns The requested memory on the heap.
|
|
|
|
/// @todo Fragmenting functionality.
|
2024-02-14 00:21:54 -08:00
|
|
|
void *malloc(size_t size)
|
2024-02-08 01:44:09 -08:00
|
|
|
{
|
2024-02-14 00:21:54 -08:00
|
|
|
if (size == 0)
|
|
|
|
{
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-02-08 01:44:09 -08:00
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
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;
|
2024-02-08 01:44:09 -08:00
|
|
|
|
2024-02-08 16:44:26 -08:00
|
|
|
if (first)
|
2024-02-08 01:44:09 -08:00
|
|
|
{
|
2024-02-14 00:21:54 -08:00
|
|
|
b = find_best_fit(size);
|
2024-02-08 01:44:09 -08:00
|
|
|
if (!b)
|
2024-02-14 00:21:54 -08:00
|
|
|
{
|
|
|
|
b = extend_heap(total_size);
|
|
|
|
if (!b)
|
|
|
|
{
|
|
|
|
return NULL; // Check if heap extension failed
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (b->size > total_size + MINIMUM_BLOCK_SIZE)
|
|
|
|
{
|
2024-02-08 17:42:03 -08:00
|
|
|
b = fragment_block(b, size);
|
2024-02-14 00:21:54 -08:00
|
|
|
}
|
2024-02-08 01:44:09 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-14 00:21:54 -08:00
|
|
|
b = extend_heap(total_size);
|
2024-02-08 01:44:09 -08:00
|
|
|
if (!b)
|
2024-02-14 00:21:54 -08:00
|
|
|
{
|
2024-02-08 01:44:09 -08:00
|
|
|
return NULL;
|
2024-02-14 00:21:54 -08:00
|
|
|
}
|
2024-02-08 16:44:26 -08:00
|
|
|
first = b;
|
2024-02-08 01:44:09 -08:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
b->free = 0; // Mark the block as used
|
|
|
|
return (char *)b + BLOCK_SIZE; // Return a pointer to the usable memory
|
2024-02-07 23:15:44 -08:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
void *realloc(void *ptr, size_t new_size)
|
2024-02-08 17:42:03 -08:00
|
|
|
{
|
|
|
|
if (!ptr)
|
2024-02-14 00:21:54 -08:00
|
|
|
{
|
2024-02-08 17:42:03 -08:00
|
|
|
return malloc(new_size);
|
2024-02-14 00:21:54 -08:00
|
|
|
}
|
2024-02-08 17:42:03 -08:00
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
if (new_size == 0)
|
|
|
|
{
|
|
|
|
free(ptr);
|
2024-02-08 17:42:03 -08:00
|
|
|
return NULL;
|
2024-02-14 00:21:54 -08:00
|
|
|
}
|
2024-02-08 17:42:03 -08:00
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
struct block *b = (struct block *)((char *)ptr - BLOCK_SIZE);
|
|
|
|
if (b->size >= new_size + BLOCK_SIZE)
|
|
|
|
{
|
|
|
|
return ptr; // The block is already big enough
|
|
|
|
}
|
2024-02-08 17:42:03 -08:00
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
void *new_ptr = malloc(new_size);
|
|
|
|
if (!new_ptr)
|
|
|
|
{
|
|
|
|
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
|
2024-02-08 17:42:03 -08:00
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
return new_ptr;
|
2024-02-08 17:42:03 -08:00
|
|
|
}
|
|
|
|
|
2024-02-08 02:00:57 -08:00
|
|
|
/// Will flag the provided memory as free and will defragment other blocks adjacent to it.
|
|
|
|
/// @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.
|
2024-02-14 00:21:54 -08:00
|
|
|
void my_custom_free(void *ptr)
|
2024-02-07 23:15:44 -08:00
|
|
|
{
|
2024-02-08 01:44:09 -08:00
|
|
|
if (!ptr)
|
2024-02-14 00:21:54 -08:00
|
|
|
{
|
2024-02-08 01:44:09 -08:00
|
|
|
return;
|
2024-02-14 00:21:54 -08:00
|
|
|
}
|
2024-02-08 01:44:09 -08:00
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
struct block *b = (struct block *)((char *)ptr - BLOCK_SIZE);
|
2024-02-08 17:24:49 -08:00
|
|
|
if (b->free)
|
2024-02-14 00:21:54 -08:00
|
|
|
{
|
|
|
|
fprintf(stderr, "Double free detected at block %p.\n", ptr);
|
|
|
|
abort(); // Terminate the program immediately due to serious error
|
|
|
|
}
|
2024-02-08 17:24:49 -08:00
|
|
|
|
2024-02-08 01:44:09 -08:00
|
|
|
b->free = 1;
|
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
// Coalesce free blocks
|
2024-02-08 17:04:41 -08:00
|
|
|
while (b->prev && b->prev->free)
|
2024-02-14 00:21:54 -08:00
|
|
|
{
|
|
|
|
// Merge with previous block
|
|
|
|
b->prev->size += BLOCK_SIZE + b->size;
|
|
|
|
b->prev->next = b->next;
|
2024-02-08 17:04:41 -08:00
|
|
|
b = b->prev;
|
2024-02-14 00:21:54 -08:00
|
|
|
}
|
2024-02-08 01:44:09 -08:00
|
|
|
while (b->next && b->next->free)
|
|
|
|
{
|
2024-02-14 00:21:54 -08:00
|
|
|
// Merge with next block
|
2024-02-08 01:44:09 -08:00
|
|
|
b->size += BLOCK_SIZE + b->next->size;
|
2024-02-08 17:04:41 -08:00
|
|
|
b->next = b->next->next;
|
2024-02-08 01:44:09 -08:00
|
|
|
}
|
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
// 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));
|
|
|
|
}
|
2024-02-07 23:15:44 -08:00
|
|
|
}
|
|
|
|
|
2024-02-08 01:44:09 -08:00
|
|
|
int main()
|
2024-02-07 23:15:44 -08:00
|
|
|
{
|
2024-02-14 00:21:54 -08:00
|
|
|
int *a = (int *)malloc(sizeof(int));
|
|
|
|
int *b = (int *)malloc(sizeof(int));
|
2024-02-08 01:44:09 -08:00
|
|
|
|
|
|
|
*a = 5;
|
|
|
|
*b = 12;
|
|
|
|
|
|
|
|
printf("Test 1: %i\n", *a);
|
2024-02-08 17:04:41 -08:00
|
|
|
printf("Test 2: %i\n", *b);
|
2024-02-08 01:44:09 -08:00
|
|
|
|
|
|
|
free(a);
|
2024-02-08 17:04:41 -08:00
|
|
|
free(b);
|
2024-02-08 01:44:09 -08:00
|
|
|
|
2024-02-14 00:21:54 -08:00
|
|
|
int *c = (int *)malloc(sizeof(int));
|
2024-02-08 17:24:49 -08:00
|
|
|
|
2024-02-07 23:15:44 -08:00
|
|
|
return 0;
|
|
|
|
}
|