SquidgeOS/src/kernel/memory.c

56 lines
1 KiB
C
Raw Normal View History

2026-02-08 23:51:37 +00:00
#include <stdint.h>
#include <stddef.h>
#include "drivers/uart.h"
#include "memory.h"
2026-02-09 00:13:00 +00:00
#include "panic.h"
2026-02-08 23:51:37 +00:00
#define PAGE_SIZE 4096
struct Page {
struct Page *next;
};
2026-02-09 00:13:00 +00:00
struct HeapHeader {
size_t size;
int is_free;
struct HeapHeader *next;
};
2026-02-08 23:51:37 +00:00
2026-02-09 00:13:00 +00:00
static struct Page *free_list = NULL;
extern uint8_t _heap_start[]; // named in the linker script
2026-02-08 23:51:37 +00:00
void page_init() {
kprint("Initialising page allocator.\n");
2026-02-09 00:13:00 +00:00
uintptr_t start = ((uintptr_t)_heap_start + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
2026-02-08 23:51:37 +00:00
uintptr_t end = 0x88000000; // Default QEMU RAM limit
for (uintptr_t addr = start; addr + PAGE_SIZE <= end; addr += PAGE_SIZE) {
page_free((void *)addr);
}
}
void page_free(void *addr) {
if (addr == NULL) return;
struct Page *p = (struct Page *)addr;
p->next = free_list;
free_list = p;
2026-02-09 00:13:00 +00:00
}
void *page_alloc() { {
if (free_list == NULL) {
kpanic("No free pages!");
}
struct Page *p = free_list;
free_list = free_list->next;
//zero out the page
for (int i = 0; i < (PAGE_SIZE/8); i++) {
((uint8_t *)p)[i] = 0;
}
return (void *)p;
}
2026-02-08 23:51:37 +00:00
}