feature: kfree_alloc is now working (uncomment a free in the main to prove)
This commit is contained in:
parent
5ebf8ac6ab
commit
4cc1dba5f9
@ -10,4 +10,5 @@
|
||||
#define PAGE_SIZE 4096
|
||||
|
||||
void init_memory(void);
|
||||
uintptr_t kalloc_frame(uint32_t nb_frames);
|
||||
void *kalloc_frame(uint32_t nb_frames);
|
||||
void kfree_frame(void *frame, uint32_t nb_frames);
|
||||
|
19
src/kernel.c
19
src/kernel.c
@ -25,7 +25,24 @@ void kernel_main(void)
|
||||
terminal_initialize();
|
||||
init_gdt();
|
||||
init_memory();
|
||||
char *str = (char *)kalloc_frame(1);
|
||||
char *str = kalloc_frame(104831);
|
||||
str = kalloc_frame(104831);
|
||||
str = kalloc_frame(104831);
|
||||
str = kalloc_frame(104831);
|
||||
str = kalloc_frame(104831);
|
||||
str = kalloc_frame(104831);
|
||||
str = kalloc_frame(104831);
|
||||
str = kalloc_frame(104831);
|
||||
str = kalloc_frame(104831);
|
||||
str = kalloc_frame(104831);
|
||||
kfree_frame(str, 104831);
|
||||
str = kalloc_frame(104831);
|
||||
kfree_frame(str, 104831);
|
||||
str = kalloc_frame(104831);
|
||||
kfree_frame(str, 104831);
|
||||
str = kalloc_frame(104831);
|
||||
kfree_frame(str, 104831);
|
||||
str = kalloc_frame(104831);
|
||||
memcpy(str, "Hello world!\n", 15);
|
||||
kprintf(KERN_INFO, "%d: %s", str, str);
|
||||
shell_init();
|
||||
|
@ -19,12 +19,14 @@
|
||||
extern uint32_t end_kernel;
|
||||
|
||||
static uint8_t frame_table[CEIL(MAX_FRAMES, 8)];
|
||||
|
||||
uintptr_t kalloc_frame(uint32_t nb_frames)
|
||||
{
|
||||
static uint32_t remaining_frames = MAX_FRAMES;
|
||||
if (nb_frames > remaining_frames)
|
||||
|
||||
void *kalloc_frame(uint32_t nb_frames)
|
||||
{
|
||||
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;
|
||||
@ -40,8 +42,27 @@ end:
|
||||
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;
|
||||
return &end_kernel + i * PAGE_SIZE;
|
||||
}
|
||||
kprintf(KERN_WARNING, "Not enough frames available\n", MAX_FRAMES);
|
||||
return 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void kfree_frame(void *frame, uint32_t nb_frames)
|
||||
{
|
||||
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;
|
||||
} else if ((uint32_t)frame % PAGE_SIZE) {
|
||||
kprintf(KERN_WARNING, "Invalid address\n");
|
||||
return;
|
||||
} else if (start + nb_frames > MAX_FRAMES) {
|
||||
kprintf(KERN_WARNING, "Invalid number of frames\n");
|
||||
return;
|
||||
}
|
||||
for (size_t i = start; i < start + nb_frames; i++)
|
||||
SET_FRAME(i, 0);
|
||||
remaining_frames += nb_frames;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user