Compare commits

..

No commits in common. "5f075a90909bbc014e4d868d5c98df0849658fdb" and "3372e386f591a1035b6ae99a03aa3faaa8e28685" have entirely different histories.

2 changed files with 27 additions and 27 deletions

View File

@ -6,24 +6,13 @@
#include "memory.h" #include "memory.h"
#include "utils.h" #include "utils.h"
#define MAX_FRAMES (0xC0000000 / PAGE_SIZE) #define MAX_FRAMES 1048319
static uint32_t end_kernel; // WIP but it's useless now
static uint8_t frame_table[MAX_FRAMES]; static uint8_t frame_table[MAX_FRAMES];
static uint32_t remaining_frames = MAX_FRAMES; static uint32_t remaining_frames = MAX_FRAMES;
static int32_t find_next_block(size_t nb_frames)
{
for (uint32_t i = 1; i < MAX_FRAMES; i++) {
uint32_t j;
for (j = 0; frame_table[i + j] == 0 && j < nb_frames; j++)
;
if (j == nb_frames)
return i;
i += j;
}
return -1;
}
void *alloc_frames(size_t size) void *alloc_frames(size_t size)
{ {
const uint32_t nb_frames = CEIL(size, PAGE_SIZE); const uint32_t nb_frames = CEIL(size, PAGE_SIZE);
@ -31,24 +20,35 @@ void *alloc_frames(size_t size)
kprintf(KERN_CRIT "Not enough frames (max: %d)\n", MAX_FRAMES); kprintf(KERN_CRIT "Not enough frames (max: %d)\n", MAX_FRAMES);
return NULL; return NULL;
} }
size_t i = find_next_block(nb_frames);
if (i == -1) { size_t i = 0;
kprintf(KERN_WARNING "Not enough frames available\n", while (i < MAX_FRAMES) {
MAX_FRAMES); size_t free_frames = 1;
return NULL; while (!frame_table[i + free_frames] && free_frames < nb_frames)
free_frames++;
if (free_frames == nb_frames)
goto end;
if (!free_frames)
i++;
i += free_frames;
} }
for (size_t j = 0; j < nb_frames; j++) end:
frame_table[j + i] = 1; if (i != MAX_FRAMES) {
remaining_frames -= nb_frames; for (size_t j = 0; j < nb_frames; j++)
return (void*)(i * PAGE_SIZE); frame_table[j + i] = 1;
remaining_frames -= nb_frames;
return &end_kernel + i * PAGE_SIZE;
}
kprintf(KERN_WARNING "Not enough frames available\n", MAX_FRAMES);
return NULL;
} }
int free_frames(void *frame_ptr, size_t size) int free_frames(void *frame_ptr, size_t size)
{ {
const uint32_t nb_frames = CEIL(size, PAGE_SIZE); const uint32_t nb_frames = CEIL(size, PAGE_SIZE);
const uint32_t start = (uint32_t)frame_ptr / PAGE_SIZE; const uint32_t start = (frame_ptr - (void *)&end_kernel) / PAGE_SIZE;
if (start > MAX_FRAMES) { if (start > MAX_FRAMES || frame_ptr < (void *)&end_kernel) {
kprintf(KERN_WARNING "Address out of range\n"); kprintf(KERN_WARNING "Address out of range\n");
return -1; return -1;
} else if ((uint32_t)frame_ptr % PAGE_SIZE) { } else if ((uint32_t)frame_ptr % PAGE_SIZE) {

View File

@ -13,7 +13,7 @@ extern uint32_t page_table_entries[PT_SIZE] __attribute__((aligned(4096)));
static int16_t find_next_block(size_t nb_pages) static int16_t find_next_block(size_t nb_pages)
{ {
for (uint16_t i = 1; i < PT_SIZE; i++) { for (uint16_t i = 0; i < PT_SIZE; i++) {
uint16_t j; uint16_t j;
for (j = 0; for (j = 0;
page_table_entries[i + j] >> 12 == i + j && j < nb_pages; page_table_entries[i + j] >> 12 == i + j && j < nb_pages;
@ -46,7 +46,7 @@ void *alloc_pages(size_t size)
} }
page_table_entries[i] = (uint32_t)frame << 12 | INIT_FLAGS; page_table_entries[i] = (uint32_t)frame << 12 | INIT_FLAGS;
} }
return (void *)(index * PAGE_SIZE); return (void *)(page_table_entries[index] >> 12);
} }
int free_pages(void *page_ptr, size_t size) int free_pages(void *page_ptr, size_t size)