From 5ebf8ac6ab99892a63385f11306bf18c68ea6d98 Mon Sep 17 00:00:00 2001 From: 0x35c Date: Thu, 19 Sep 2024 17:16:45 +0200 Subject: [PATCH] fix: update frame count when allocating some --- src/kernel.c | 2 +- src/memory/frame.c | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/kernel.c b/src/kernel.c index a6854dd..2e7f49b 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -27,6 +27,6 @@ void kernel_main(void) init_memory(); char *str = (char *)kalloc_frame(1); memcpy(str, "Hello world!\n", 15); - kprintf(KERN_INFO, "%s", str); + kprintf(KERN_INFO, "%d: %s", str, str); shell_init(); } diff --git a/src/memory/frame.c b/src/memory/frame.c index d2fade4..b9da6d1 100644 --- a/src/memory/frame.c +++ b/src/memory/frame.c @@ -22,12 +22,14 @@ static uint8_t frame_table[CEIL(MAX_FRAMES, 8)]; uintptr_t kalloc_frame(uint32_t nb_frames) { - if (nb_frames > MAX_FRAMES) + static uint32_t remaining_frames = MAX_FRAMES; + if (nb_frames > remaining_frames) kprintf(KERN_CRIT, "Not enough frames (max: %d)\n", MAX_FRAMES); size_t i = 0; while (i < MAX_FRAMES) { size_t free_frames = 1; - while (!GET_FRAME(i + free_frames) && free_frames < nb_frames) + while (!GET_FRAME(i + free_frames) && free_frames < nb_frames && + i + free_frames < remaining_frames) free_frames++; if (free_frames == nb_frames) goto end; @@ -37,6 +39,7 @@ end: if (i != MAX_FRAMES) { for (size_t j = 0; j < nb_frames; j++) SET_FRAME(j + i, 1); + remaining_frames -= nb_frames; return (uintptr_t)&end_kernel + i * PAGE_SIZE; } kprintf(KERN_WARNING, "Not enough frames available\n", MAX_FRAMES);