fix: free: use the new pt system

This commit is contained in:
2024-10-30 21:26:00 +01:00
parent ae3cfa7647
commit 6533334824
3 changed files with 30 additions and 22 deletions

View File

@ -9,15 +9,19 @@
#define MAX_FRAMES (HEAP_END / PAGE_SIZE)
enum {
FREE,
USED,
};
static uint8_t frame_table[MAX_FRAMES];
static uint32_t remaining_frames = MAX_FRAMES;
static int32_t find_next_block(size_t nb_frames)
{
for (uint32_t i = CEIL(HEAP_START, PAGE_SIZE);
i + nb_frames < MAX_FRAMES; i++) {
uint32_t j;
for (j = 0; frame_table[i + j] == 0 && j < nb_frames; j++)
for (j = 0; frame_table[i + j] == FREE && j < nb_frames; j++)
;
if (j == nb_frames)
return i;
@ -29,28 +33,22 @@ static int32_t find_next_block(size_t nb_frames)
void *alloc_frames(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;
}
int i = find_next_block(nb_frames);
const int i = find_next_block(nb_frames);
if (i < 0) {
kprintf(KERN_WARNING "Not enough frames available\n");
return NULL;
}
for (size_t j = 0; j < nb_frames; j++) {
assert(j + i < MAX_FRAMES);
frame_table[j + i] = 1;
frame_table[j + i] = USED;
}
assert(remaining_frames >= nb_frames);
remaining_frames -= nb_frames;
return (void *)(i * PAGE_SIZE);
}
int free_frames(void *frame_ptr, size_t size)
{
const uint32_t nb_frames = CEIL(size, PAGE_SIZE);
const uint32_t start = (uint32_t)(frame_ptr + HEAP_END) / PAGE_SIZE;
const uint32_t start = (uint32_t)frame_ptr / PAGE_SIZE;
if (start > MAX_FRAMES) {
kprintf(KERN_WARNING "Address out of range\n");
@ -63,7 +61,6 @@ int free_frames(void *frame_ptr, size_t size)
return -1;
}
for (size_t i = start; i < start + nb_frames; i++)
frame_table[i] = 0;
remaining_frames += nb_frames;
frame_table[i] = FREE;
return 0;
}