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-07 23:15:44 -08:00
|
|
|
|
2024-02-08 01:44:09 -08:00
|
|
|
#define ALIGN4(s) (((((s)-1)>>2)<<2)+4)
|
|
|
|
#define BLOCK_SIZE sizeof(struct block)
|
|
|
|
|
2024-02-08 02:00:57 -08:00
|
|
|
/// The memory's block header.
|
2024-02-08 01:44:09 -08:00
|
|
|
struct block
|
|
|
|
{
|
|
|
|
size_t size;
|
|
|
|
struct block* next;
|
|
|
|
int free;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct block* freeList = NULL;
|
|
|
|
|
2024-02-08 02:00:57 -08:00
|
|
|
/// Extends heap memory upwards, towards zero.
|
|
|
|
/// @param [in] last The last block that the newly created block will link to.
|
|
|
|
/// @param [in] s The size of the memory needed aligned by 4 bytes.
|
|
|
|
/// @returns The new memory block.
|
2024-02-08 01:44:09 -08:00
|
|
|
struct block* extend_heap(struct block* last, size_t s)
|
|
|
|
{
|
|
|
|
struct block* b = sbrk(0);
|
|
|
|
|
|
|
|
if (sbrk(BLOCK_SIZE + s) == (void*)-1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
b->size = s;
|
|
|
|
b->next = NULL;
|
|
|
|
b->free = 0;
|
|
|
|
|
|
|
|
if (last)
|
|
|
|
last->next = b;
|
|
|
|
|
|
|
|
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-08 01:44:09 -08:00
|
|
|
struct block* find_first(size_t s)
|
|
|
|
{
|
|
|
|
struct block* current = freeList;
|
|
|
|
while (current && (!current->free || current->size < s))
|
|
|
|
current = current->next;
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
2024-02-08 02:00:57 -08:00
|
|
|
/// Finds the last memory block in the linked list to attach a new block to.
|
|
|
|
/// @returns The last memory block.
|
2024-02-08 01:44:09 -08:00
|
|
|
struct block* find_last()
|
2024-02-07 23:15:44 -08:00
|
|
|
{
|
2024-02-08 01:44:09 -08:00
|
|
|
struct block* current = freeList;
|
|
|
|
while (current && current->next)
|
|
|
|
current = freeList->next;
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
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-08 01:44:09 -08:00
|
|
|
void* malloc(size_t size)
|
|
|
|
{
|
|
|
|
struct block* b;
|
|
|
|
|
|
|
|
size_t alignedSize = ALIGN4(size);
|
|
|
|
|
|
|
|
if (freeList)
|
|
|
|
{
|
|
|
|
b = find_first(alignedSize);
|
|
|
|
if (!b)
|
|
|
|
b = extend_heap(find_last(), alignedSize);
|
2024-02-08 02:00:57 -08:00
|
|
|
/*
|
|
|
|
else
|
|
|
|
Fragment here if possible.
|
|
|
|
*/
|
2024-02-08 01:44:09 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
b = extend_heap(NULL, alignedSize);
|
|
|
|
if (!b)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
freeList = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (char*)b + BLOCK_SIZE;
|
2024-02-07 23:15:44 -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.
|
|
|
|
/// @todo Reverse defragmenting.
|
2024-02-07 23:15:44 -08:00
|
|
|
void free(void* ptr)
|
|
|
|
{
|
2024-02-08 01:44:09 -08:00
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct block* b = (struct block*)((char*)ptr - BLOCK_SIZE);
|
|
|
|
b->free = 1;
|
|
|
|
|
|
|
|
struct block* link = b->next;
|
|
|
|
while (b->next && b->next->free)
|
|
|
|
{
|
|
|
|
b->size += BLOCK_SIZE + b->next->size;
|
|
|
|
b->next = link->next;
|
|
|
|
}
|
|
|
|
|
2024-02-08 02:00:57 -08:00
|
|
|
// Reverse defragment here.
|
|
|
|
|
2024-02-08 01:44:09 -08:00
|
|
|
if (!b->next)
|
|
|
|
brk(b);
|
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-08 01:44:09 -08:00
|
|
|
int* a = (int*)malloc(sizeof(int));
|
|
|
|
int* b = (int*)malloc(sizeof(int));
|
|
|
|
|
|
|
|
*a = 5;
|
|
|
|
*b = 12;
|
|
|
|
|
|
|
|
printf("Test 1: %i\n", *a);
|
|
|
|
printf("Test 2: %i", *b);
|
|
|
|
|
|
|
|
free(b);
|
|
|
|
free(a);
|
|
|
|
|
2024-02-07 23:15:44 -08:00
|
|
|
return 0;
|
|
|
|
}
|