feature: memory limit with multiboot (wip, still crashing)

This commit is contained in:
2024-11-07 13:45:29 +01:00
parent a72c3ca195
commit 5fccbf3708
7 changed files with 304 additions and 28 deletions

View File

@ -8,6 +8,7 @@
#include "utils.h"
#define MAX_FRAMES (HEAP_END / PAGE_SIZE)
#define NB_FRAMES (mem_size / PAGE_SIZE)
enum {
FREE,
@ -19,7 +20,7 @@ static uint8_t frame_table[MAX_FRAMES];
static int32_t find_next_block(size_t nb_frames)
{
for (uint32_t i = CEIL(HEAP_START, PAGE_SIZE);
i + nb_frames < MAX_FRAMES; i++) {
i + nb_frames < NB_FRAMES; i++) {
uint32_t j;
for (j = 0; frame_table[i + j] == FREE && j < nb_frames; j++)
;
@ -39,7 +40,7 @@ void *alloc_frames(size_t size)
return NULL;
}
for (size_t j = 0; j < nb_frames; j++) {
assert(j + i < MAX_FRAMES);
assert(j + i < NB_FRAMES);
frame_table[j + i] = USED;
}
return (void *)(i * PAGE_SIZE);
@ -50,13 +51,13 @@ int free_frames(void *frame_ptr, size_t size)
const uint32_t nb_frames = CEIL(size, PAGE_SIZE);
const uint32_t start = (uint32_t)frame_ptr / PAGE_SIZE;
if (start > MAX_FRAMES) {
if (start > NB_FRAMES) {
kprintf(KERN_WARNING "Address out of range\n");
return -1;
} else if ((uint32_t)frame_ptr % PAGE_SIZE) {
kprintf(KERN_WARNING "Invalid address\n");
return -1;
} else if (start + nb_frames > MAX_FRAMES) {
} else if (start + nb_frames > NB_FRAMES) {
kprintf(KERN_WARNING "Invalid number of frames\n");
return -1;
}

View File

@ -6,6 +6,7 @@
uint32_t *page_directory = &boot_page_directory;
uint32_t page_table_default[1024] __attribute__((aligned(4096)));
uint32_t mem_size;
void init_memory(void)
{