44 lines
988 B
C
44 lines
988 B
C
#include <stdbool.h>
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
#include "debug.h"
|
|
#include "kprintf.h"
|
|
#include "memory.h"
|
|
#include "string.h"
|
|
#include "utils.h"
|
|
|
|
void *alloc_frame(void)
|
|
{
|
|
struct frame_zone *it = head;
|
|
while (it && !it->remaining_frames)
|
|
it = it->next;
|
|
if (!it || it->remaining_frames == 0) {
|
|
kprintf(KERN_CRIT "No memory zone available (ratio)\n");
|
|
return NULL;
|
|
}
|
|
|
|
size_t i = it->first_free_frame;
|
|
for (; GET_FRAME(it->frame_table, i); i++)
|
|
;
|
|
it->first_free_frame++;
|
|
it->remaining_frames--;
|
|
SET_FRAME(it->frame_table, i, 1);
|
|
return it->addr + i * PAGE_SIZE;
|
|
}
|
|
|
|
int free_frame(void *frame_ptr)
|
|
{
|
|
struct frame_zone *it = head;
|
|
while (it && (frame_ptr < it->addr ||
|
|
frame_ptr >= it->addr + it->total_frames * PAGE_SIZE))
|
|
it = it->next;
|
|
|
|
uint32_t index = ((frame_ptr - it->addr) / PAGE_SIZE);
|
|
SET_FRAME(it->frame_table, index, 0);
|
|
if (it->first_free_frame > index)
|
|
it->first_free_frame = index;
|
|
it->remaining_frames++;
|
|
return 0;
|
|
}
|