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-11 00:25:42 -08:00
|
|
|
#include <sys/syscall.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-11 00:25:42 -08:00
|
|
|
#define MINIMUM_BLOCK_SIZE 4
|
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-08 17:04:41 -08:00
|
|
|
struct block* prev;
|
2024-02-08 01:44:09 -08:00
|
|
|
struct block* next;
|
|
|
|
int free;
|
|
|
|
};
|
|
|
|
|
2024-02-08 16:44:26 -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-08 16:44:26 -08:00
|
|
|
struct block* extend_heap(size_t s)
|
2024-02-08 01:44:09 -08:00
|
|
|
{
|
2024-02-08 17:47:44 -08:00
|
|
|
// Ensure the allocated size is at least the minimum block size
|
|
|
|
if (s < MINIMUM_BLOCK_SIZE)
|
|
|
|
s = MINIMUM_BLOCK_SIZE;
|
|
|
|
|
2024-02-11 00:25:42 -08:00
|
|
|
struct block* b = (struct block*)syscall(SYS_brk, NULL);
|
2024-02-08 01:44:09 -08:00
|
|
|
|
2024-02-11 00:25:42 -08:00
|
|
|
if ((void*)syscall(SYS_brk, (char*)(b + 1) + s) == (void*)-1)
|
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-08 01:44:09 -08:00
|
|
|
struct block* find_first(size_t s)
|
|
|
|
{
|
2024-02-08 16:44:26 -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-08 17:24:49 -08:00
|
|
|
struct block* fragment_block(struct block* in, size_t s)
|
|
|
|
{
|
|
|
|
size_t totalSize = BLOCK_SIZE + s;
|
|
|
|
struct block* b = (struct block*)((char*)(in + 1) + (in->size - totalSize));
|
|
|
|
b->size = s;
|
|
|
|
b->prev = in;
|
|
|
|
b->next = in->next;
|
|
|
|
b->free = 0;
|
|
|
|
|
|
|
|
in->size -= totalSize;
|
|
|
|
in->next = b;
|
|
|
|
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
2024-02-08 17:42:03 -08:00
|
|
|
size = ALIGN4(size);
|
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-08 17:42:03 -08:00
|
|
|
b = find_first(size);
|
2024-02-08 01:44:09 -08:00
|
|
|
if (!b)
|
2024-02-08 17:42:03 -08:00
|
|
|
b = extend_heap(size);
|
|
|
|
else if (b->size > BLOCK_SIZE + size)
|
|
|
|
b = fragment_block(b, size);
|
2024-02-08 01:44:09 -08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-08 17:42:03 -08:00
|
|
|
b = extend_heap(size);
|
2024-02-08 01:44:09 -08:00
|
|
|
if (!b)
|
|
|
|
return NULL;
|
|
|
|
|
2024-02-08 16:44:26 -08:00
|
|
|
first = b;
|
2024-02-08 01:44:09 -08:00
|
|
|
}
|
|
|
|
|
2024-02-08 02:08:38 -08:00
|
|
|
return b + 1;
|
2024-02-07 23:15:44 -08:00
|
|
|
}
|
|
|
|
|
2024-02-08 17:42:03 -08:00
|
|
|
void* realloc(void* ptr, size_t new_size)
|
|
|
|
{
|
|
|
|
if (!ptr)
|
|
|
|
return malloc(new_size);
|
|
|
|
|
|
|
|
if (!new_size)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
struct block* b = (struct block*)ptr - 1;
|
|
|
|
if (b->free)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (b->size == new_size)
|
|
|
|
return ptr;
|
|
|
|
else if (b->size > BLOCK_SIZE + new_size)
|
|
|
|
return fragment_block(b, new_size) + 1;
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
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-07 23:15:44 -08:00
|
|
|
void free(void* ptr)
|
|
|
|
{
|
2024-02-08 01:44:09 -08:00
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
|
2024-02-08 02:09:58 -08:00
|
|
|
struct block* b = (struct block*)ptr - 1;
|
2024-02-08 17:24:49 -08:00
|
|
|
if (b->free)
|
|
|
|
return;
|
|
|
|
|
2024-02-08 01:44:09 -08:00
|
|
|
b->free = 1;
|
|
|
|
|
2024-02-08 17:04:41 -08:00
|
|
|
while (b->prev && b->prev->free)
|
|
|
|
b = b->prev;
|
|
|
|
|
2024-02-08 01:44:09 -08:00
|
|
|
while (b->next && b->next->free)
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (!b->next)
|
2024-02-11 00:25:42 -08:00
|
|
|
syscall(SYS_brk, b);
|
2024-02-07 23:15:44 -08:00
|
|
|
}
|
|
|
|
|
2024-02-08 18:50:58 -08:00
|
|
|
/// Retrieves the total heap usage in bytes.
|
|
|
|
/// @returns Usage in bytes.
|
|
|
|
size_t get_heap_size()
|
|
|
|
{
|
2024-02-08 18:51:44 -08:00
|
|
|
if (!first || !last)
|
|
|
|
return 0;
|
|
|
|
|
2024-02-08 18:50:58 -08:00
|
|
|
return ((char*)last + last->size) - (char*)first;
|
|
|
|
}
|
|
|
|
|
2024-02-08 01:44:09 -08:00
|
|
|
int main()
|
2024-02-07 23:15:44 -08:00
|
|
|
{
|
2024-02-11 00:25:42 -08:00
|
|
|
int* a = (int*)malloc(1);
|
|
|
|
int* b = (int*)malloc(1);
|
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 18:50:58 -08:00
|
|
|
printf("Heap Size: %zu Bytes\n", get_heap_size());
|
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-08 17:24:49 -08:00
|
|
|
int* c = (int*)malloc(sizeof(int));
|
|
|
|
|
2024-02-07 23:15:44 -08:00
|
|
|
return 0;
|
|
|
|
}
|