Espresso 0.0.2a

This commit is contained in:
2025-10-20 21:57:30 -05:00
parent 102d517097
commit ff6cba1164
59 changed files with 29272 additions and 773 deletions

View File

@ -1,76 +1,138 @@
#include <stdio.h>
#include <string.h>
#include <mm/heap.h>
#include <mm/pmm.h>
#include <mm/paging.h>
#include <string.h>
#include <stdio.h>
#include <mm_macros.h>
#include <mm/heap.h>
#define ALIGNMENT 8
#define ALIGN(size) (((size) + (ALIGNMENT - 1)) & ~(ALIGNMENT - 1))
#define ALIGN4(x) (((x) + 3) & ~3)
#define MIN_BLOCK_SIZE 16
typedef struct block_header {
typedef struct block {
size_t size;
struct block_header* next;
struct block* next;
int free;
} block_header_t;
} block_t;
#define BLOCK_SIZE sizeof(block_header_t)
#define BLOCK_SIZE sizeof(block_t)
static uint8_t* heap_base = (uint8_t*)HEAP_START;
static uint8_t* heap_end = (uint8_t*)(HEAP_START + HEAP_SIZE);
static uint8_t* heap_base;
static uint8_t* heap_end;
static size_t heap_size;
static block_t* free_list;
static block_header_t* free_list = NULL;
void heap_init(void)
void heap_init(uint32_t start, uint32_t size)
{
free_list = (block_header_t*)heap_base;
free_list->size = HEAP_SIZE - BLOCK_SIZE;
free_list->next = NULL;
#ifdef _DEBUG
printf("[ HEAP ] Initializing heap allocator...\n");
#endif
heap_base = (uint8_t*) ALIGN4((uintptr_t) start);
heap_end = heap_base;
heap_size = size;
free_list = NULL;
for (uint32_t i = 0; i < size; i += 0x1000)
{
map_page(pmm_alloc_page(), (void*)(start + i));
}
/* Set up initial free block */
free_list = (block_t*)heap_base;
free_list->size = heap_size - BLOCK_SIZE;
free_list->free = 1;
free_list->next = NULL;
heap_end = heap_base + size;
#ifdef _DEBUG
printf("[ HEAP ] Heap allocator initialized\n");
#endif
}
void* malloc(size_t size)
static block_t* find_free_block(size_t size)
{
size = ALIGN(size);
block_header_t* curr = free_list;
block_t* curr = free_list;
while (curr)
{
if (curr->free && curr->size >= size)
{
/* Split if there's space for another block */
if (curr->size >= size + BLOCK_SIZE + ALIGNMENT)
{
block_header_t* new_block = (block_header_t*)((uint8_t*)curr + BLOCK_SIZE + size);
new_block->size = curr->size - size - BLOCK_SIZE;
new_block->next = curr->next;
new_block->free = 1;
curr->next = new_block;
curr->size = size;
}
curr->free = 0;
return (void*)((uint8_t*)curr + BLOCK_SIZE);
return curr;
}
curr = curr->next;
}
printd("Malloc failed due to lack of free memory\n");
printf("find_free_block(): No free block found!\n");
return NULL;
}
static void split_block(block_t* blk, size_t size)
{
if (blk->size >= size + BLOCK_SIZE + MIN_BLOCK_SIZE)
{
block_t* new_blk = (block_t*)((uint8_t*)blk + BLOCK_SIZE + size);
new_blk->size = blk->size - size - BLOCK_SIZE;
new_blk->free = 1;
new_blk->next = blk->next;
blk->next = new_blk;
blk->size = size;
}
}
void* malloc(size_t size)
{
size = ALIGN4(size);
block_t* blk = find_free_block(size);
if (!blk)
{
printf("malloc(): No free block found!\n");
return NULL;
}
split_block(blk, size);
blk->free = 0;
return (void*)((uint8_t*)blk + BLOCK_SIZE);
}
void free(void* ptr)
{
if (!ptr)
{
return;
}
block_t* blk = (block_t*)((uint8_t*)ptr - BLOCK_SIZE);
blk->free = 1;
/* coalesce */
block_t* curr = free_list;
while (curr && curr->next)
{
if (curr->free && curr->next->free)
{
curr->size += BLOCK_SIZE + curr->next->size;
curr->next = curr->next->next;
}
else
{
curr = curr->next;
}
}
}
void* calloc(size_t nmemb, size_t size)
{
size_t total = nmemb * size;
void* ptr = malloc(total);
if (ptr)
{
memset(ptr, 0, total);
MEMSET(ptr, 0, total);
}
return ptr;
}
@ -81,57 +143,25 @@ void* realloc(void* ptr, size_t size)
return malloc(size);
}
if (size == 0)
if (!size)
{
free(ptr);
return NULL;
}
block_header_t* block = (block_header_t*)((uint8_t*)ptr - BLOCK_SIZE);
if (block->size >= size)
block_t* blk = (block_t*)((uint8_t*)ptr - BLOCK_SIZE);
if (blk->size >= size)
{
return ptr;
}
void* new_ptr = malloc(size);
if (new_ptr) {
memcpy(new_ptr, ptr, block->size);
if (new_ptr)
{
memcpy(new_ptr, ptr, blk->size);
free(ptr);
}
return new_ptr;
}
void free(void* ptr)
{
if (!ptr)
{
return;
}
block_header_t* block = (block_header_t*)((uint8_t*)ptr - BLOCK_SIZE);
block->free = 1;
/* Forward coalescing */
if (block->next && block->next->free)
{
block->size += BLOCK_SIZE + block->next->size;
block->next = block->next->next;
}
/* Backward coalescing */
block_header_t* prev = NULL;
block_header_t* curr = free_list;
while (curr && curr != block)
{
prev = curr;
curr = curr->next;
}
if (prev && prev->free)
{
prev->size += BLOCK_SIZE + block->size;
prev->next = block->next;
}
}