A poorly written OS for the x86 arch. (WIP)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
eOS/kernel/memory.c

41 lines
1015 B

3 years ago
#include "memory.h"
3 years ago
#include "../drivers/vga.h"
3 years ago
// https://wiki.osdev.org/Page_Frame_Allocation
// page = block
// MAX_BLOCK_SIZE_IN_BITS/8 bytes
// i.e. : bit i of byte n define status of block 8n+i
// block = 8n+i
3 years ago
// in 32-bit mode, we have access to 2^32 blocks
// which is (2^32)*BLOCK_SIZE blocks
// and with a blocksize of 1024, we git around 4.3 trillion blocks
// which is more than enough
3 years ago
int bitmap = 0;
3 years ago
3 years ago
#define CHECK_BITMAP(map, idx) ((map) & (1<<(idx)))
void mod_bitmap(uint bit, uint bflag) {
3 years ago
// create a bitmask that will be applied to the bitmap
int bitmask = 1 << bit;
// apply the bitmask, resulting in the bit:n bit will bet
// set to bflag
bitmap = (((bitmap & ~bitmask)) | (bflag << bit));
3 years ago
}
3 years ago
int block_alloc(uint blockidx) {
if( CHECK_BITMAP(bitmap, blockidx) == 1 ) { // check if block is free
println("Alloc!", DEFAULT_COLOR);
mod_bitmap(blockidx, 0);
3 years ago
return 0; // placeholder
3 years ago
} else {
println("ERROR! Attemped to allocate non-free block.", 0x0c);
return -1;
}
}