fix: update frame count when allocating some

This commit is contained in:
0x35c 2024-09-19 17:16:45 +02:00
parent f559e71433
commit 5ebf8ac6ab
2 changed files with 6 additions and 3 deletions

View File

@ -27,6 +27,6 @@ void kernel_main(void)
init_memory(); init_memory();
char *str = (char *)kalloc_frame(1); char *str = (char *)kalloc_frame(1);
memcpy(str, "Hello world!\n", 15); memcpy(str, "Hello world!\n", 15);
kprintf(KERN_INFO, "%s", str); kprintf(KERN_INFO, "%d: %s", str, str);
shell_init(); shell_init();
} }

View File

@ -22,12 +22,14 @@ static uint8_t frame_table[CEIL(MAX_FRAMES, 8)];
uintptr_t kalloc_frame(uint32_t nb_frames) 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); kprintf(KERN_CRIT, "Not enough frames (max: %d)\n", MAX_FRAMES);
size_t i = 0; size_t i = 0;
while (i < MAX_FRAMES) { while (i < MAX_FRAMES) {
size_t free_frames = 1; 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++; free_frames++;
if (free_frames == nb_frames) if (free_frames == nb_frames)
goto end; goto end;
@ -37,6 +39,7 @@ end:
if (i != MAX_FRAMES) { if (i != MAX_FRAMES) {
for (size_t j = 0; j < nb_frames; j++) for (size_t j = 0; j < nb_frames; j++)
SET_FRAME(j + i, 1); SET_FRAME(j + i, 1);
remaining_frames -= nb_frames;
return (uintptr_t)&end_kernel + i * PAGE_SIZE; return (uintptr_t)&end_kernel + i * PAGE_SIZE;
} }
kprintf(KERN_WARNING, "Not enough frames available\n", MAX_FRAMES); kprintf(KERN_WARNING, "Not enough frames available\n", MAX_FRAMES);