feature: kmalloc kfree and krealloc are good

This commit is contained in:
2024-09-21 12:17:27 +02:00
parent 0467c45bf0
commit 943f2beab9
15 changed files with 607 additions and 23 deletions

View File

@ -21,12 +21,14 @@ extern uint32_t end_kernel;
static uint8_t frame_table[CEIL(MAX_FRAMES, 8)];
static uint32_t remaining_frames = MAX_FRAMES;
void *kalloc_frame(uint32_t nb_frames)
void *kalloc_frame(size_t size)
{
const uint32_t nb_frames = CEIL(size, PAGE_SIZE);
if (nb_frames > remaining_frames) {
kprintf(KERN_CRIT "Not enough frames (max: %d)\n", MAX_FRAMES);
return NULL;
}
size_t i = 0;
while (i < MAX_FRAMES) {
size_t free_frames = 1;
@ -48,21 +50,23 @@ end:
return NULL;
}
void kfree_frame(void *frame, uint32_t nb_frames)
int kfree_frame(void *frame, size_t size)
{
const uint32_t nb_frames = CEIL(size, PAGE_SIZE);
const uint32_t start = (frame - (void *)&end_kernel) / PAGE_SIZE;
if (start > MAX_FRAMES || frame < (void *)&end_kernel) {
kprintf(KERN_WARNING "Address out of range\n");
return;
return -1;
} else if ((uint32_t)frame % PAGE_SIZE) {
kprintf(KERN_WARNING "Invalid address\n");
return;
return -1;
} else if (start + nb_frames > MAX_FRAMES) {
kprintf(KERN_WARNING "Invalid number of frames\n");
return;
return -1;
}
for (size_t i = start; i < start + nb_frames; i++)
SET_FRAME(i, 0);
remaining_frames += nb_frames;
return 0;
}