implements first free mem block

This commit is contained in:
2025-06-11 17:41:35 -04:00
parent f216c32f22
commit 98f8a0dc88
3 changed files with 13 additions and 27 deletions

View File

@ -1,3 +1,6 @@
#include <stdint.h>
#include <kernel/_kernel.h>
#include <kernel/pmm.h>
/**
@ -37,12 +40,20 @@ void __pmm_unset(int bit, uint32_t* bitmap)
bitmap[bit / 32] &= ~(1 << (bit % 32));
}
void __pmm_first_free(struct pmm_mem_info mem_block)
int __pmm_first_free(struct pmm_mem_info mem_block)
{
for (uint32_t i = 0; i < PMM_GET_MEM_BLOCKS(mem_block) / 32; i++) {
if (mem_block.bitmap[i] == 0xFFFFFFFF) // this segment is full
continue;
for (int j = 0; j < 32; j++) {
if (mem_block.bitmap[i] & (1 << j))
continue; // this page is used
return (i * 32) + j; // i * 32 is the chunk of 32, plus j to get to the page in the chunk
}
}
kwarn("OUT OF MEMORY");
return -1;
}
void pmm_set(int bit)
{

View File

@ -1,20 +1,4 @@
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#ifndef ARCH_PMM_H
#define ARCH_PMM_H
#define PMM_BLOCKS_PER_BYTE 8 // This is for the Bitmap
#define PMM_BLOCK_SIZE 4096 // 4KiB
#define PMM_BLOCK_ALIGN (PMM_BLOCK_SIZE) // must be aligned at 4KiB
void set_grub_mem_map(uint32_t addr, uint32_t len);
uint32_t pmm_get_block_count(void);
int pmm_first_free(void);
int pmm_first_free_s(size_t pages);
void pmm_init(size_t mem_size, uint32_t* bitmap);
#endif

View File

@ -38,24 +38,15 @@ void verify_memmap(multiboot_info_t* mbd, uint32_t magic)
// mmmt-> len is in bytes (according to multiboot specification0
// mmmt->len / 1024 == kib // block size == blocks
if (mmmt->type == MULTIBOOT_MEMORY_AVAILABLE && (mmmt->addr == 0x100000)) {
// This is the main mem address we want
// TODO: this is probably flaky, so i want to find a better and more reliable way to do this
set_grub_mem_map(mmmt->addr, mmmt->len);
}
}
printf("%2 %2\n", mbd->mem_lower, mbd->mem_upper);
}
void _main(multiboot_info_t* mbd, uint32_t magic)
{
#ifdef __TESTING__ // important components should be declared first, but if we're testing we want to log all of that
terminal_initialize();
serial_initialize();
#endif
printf("%2, %2\n", mbd->mem_lower, mbd->mem_upper);
verify_memmap(mbd, magic);
gdt_init();