Re-make: kernel heap (malloc, free) via free list alloc + add UEFI emulation doc

This commit is contained in:
xamidev
2024-09-04 21:49:26 +02:00
parent 3b39a0a1f4
commit 4d05e0d620
4 changed files with 79 additions and 35 deletions

View File

@@ -5,42 +5,60 @@
#include "kheap.h"
#include <stdint.h>
#include "system.h"
extern uint32_t end;
uint32_t placement_address = (uint32_t)&end;
// Free list allocator
uint32_t kmalloc_int(uint32_t sz, int align, uint32_t *phys)
static uint8_t heap[HEAP_SIZE];
static block_t* free_list = NULL;
void init_alloc()
{
if (align == 1 && (placement_address & 0x00000FFF))
free_list = (block_t*)heap;
free_list->size = HEAP_SIZE-sizeof(block_t);
free_list->next = NULL;
}
void* malloc(size_t size)
{
block_t* prev = NULL;
block_t* curr = free_list;
while (curr != NULL)
{
placement_address &= 0xFFFFF000;
placement_address += 0x1000;
if (curr->size >= size)
{
if (curr->size > (size_t)(size + sizeof(block_t)))
{
block_t* new_block = (block_t*)((uint8_t*)curr + sizeof(block_t) + size);
new_block->size = curr->size - size - sizeof(block_t);
new_block->next = curr->next;
curr->size = size;
curr->next = new_block;
}
if (prev == NULL)
{
free_list = curr->next;
} else {
prev->next = curr->next;
}
return (void*)((uint8_t*)curr + sizeof(block_t));
}
prev = curr;
curr = curr->next;
}
if (phys)
{
*phys = placement_address;
}
uint32_t tmp = placement_address;
placement_address += sz;
return tmp;
return NULL;
}
uint32_t kmalloc_a(uint32_t sz)
void free(void* ptr)
{
return kmalloc_int(sz, 1, 0);
}
if (ptr == NULL) return;
uint32_t kmalloc_p(uint32_t sz, uint32_t *phys)
{
return kmalloc_int(sz, 0, phys);
}
uint32_t kmalloc_ap(uint32_t sz, uint32_t *phys)
{
return kmalloc_int(sz, 1, phys);
}
uint32_t kmalloc(uint32_t sz)
{
return kmalloc_int(sz, 0, 0);
block_t* block_to_free = (block_t*)((uint8_t*)ptr - sizeof(block_t));
block_to_free->next = free_list;
free_list = block_to_free;
}

View File

@@ -7,10 +7,19 @@
#define KHEAP_H
#include <stdint.h>
#include "system.h"
uint32_t kmalloc_a(uint32_t sz);
uint32_t kmalloc_p(uint32_t sz, uint32_t *phys);
uint32_t kmalloc_ap(uint32_t sz, uint32_t *phys);
uint32_t kmalloc(uint32_t sz);
typedef struct block
{
size_t size;
struct block* next;
} block_t;
#define HEAP_SIZE 1024*1024 // 1MB malloc-able
void init_alloc();
void* malloc(size_t size);
void free(void* ptr);
#endif

View File

@@ -13,6 +13,7 @@
#include "../drivers/framebuffer.h"
#include "kmain.h"
#include "multiboot2.h"
#include "kheap.h"
void kmain(multiboot2_info *mb_info)
{
@@ -66,14 +67,17 @@ void kmain(multiboot2_info *mb_info)
while ((uint8_t*) mmap < tags + mmap_tag->size)
{
if (mmap->addr != 0)
{
/*
printf("[debug] base addr=0x%x%x, length=0x%x%x, type=%u\n",
(uint32_t) (mmap->addr >> 32),
(uint32_t) (mmap->addr & 0xFFFFFFFF),
(uint32_t) (mmap->len >> 32),
(uint32_t) (mmap->len & 0xFFFFFFFF),
mmap->type);
*/
}
mmap = (struct multiboot_mmap_entry*) ((uint8_t*)mmap + mmap_tag->entry_size);
@@ -86,8 +90,11 @@ void kmain(multiboot2_info *mb_info)
isr_install();
irq_install();
__asm__ __volatile__("sti");
//test_read_sector();
init_alloc();
void* ptr1 = malloc(256);
void* ptr2 = malloc(512);
free(ptr2);
timer_install();
keyboard_install();