Skip to content

Commit

Permalink
Mem: Get the numbe of pages that are free
Browse files Browse the repository at this point in the history
  • Loading branch information
corigan01 committed Jan 19, 2025
1 parent 1436b6e commit 7916a7f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
4 changes: 4 additions & 0 deletions crates/mem/src/pmm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,8 @@ impl Pmm {
pub fn free_page(&mut self, page: PhysPage) -> Result<(), MemoryError> {
self.table.free_page(page)
}

pub fn pages_free(&self) -> Result<usize, MemoryError> {
self.table.pages_free()
}
}
27 changes: 27 additions & 0 deletions crates/mem/src/pmm/backing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ pub trait TableImpl: Sized {
page: PhysPage,
el_size: usize,
) -> Result<AllocationResult, MemoryError>;

fn pages_free(&self, el_size: usize) -> Result<usize, MemoryError>;
}

#[derive(Clone)]
Expand Down Expand Up @@ -157,6 +159,11 @@ impl<Table: TableImpl> MemoryTable<Table> {
pub fn free_page(&mut self, page: PhysPage) -> Result<(), MemoryError> {
self.table.free_page(page, self.element_size).map(|_| ())
}

#[inline]
pub fn pages_free(&self) -> Result<usize, MemoryError> {
self.table.pages_free(self.element_size)
}
}

impl TableImpl for TableFlat {
Expand Down Expand Up @@ -380,6 +387,22 @@ impl TableImpl for TableFlat {
new_size: self.healthy_tables.max(self.dirty_tables.min(1)),
})
}

fn pages_free(&self, el_size: usize) -> Result<usize, MemoryError> {
self.table.iter().try_fold(0, |acc, el| {
Ok(acc
+ match el {
TableElementKind::NotAllocated => 0,
TableElementKind::Present => el_size / PAGE_4K,
TableElementKind::TableFlat { ptr, .. } => {
unsafe { ptr.as_ref() }.pages_free()?
}
TableElementKind::TableBits { ptr, .. } => {
unsafe { ptr.as_ref() }.pages_free()?
}
})
})
}
}

impl TableImpl for TableBits {
Expand Down Expand Up @@ -457,6 +480,10 @@ impl TableImpl for TableBits {
new_size: self.atom_size,
})
}

fn pages_free(&self, el_size: usize) -> Result<usize, MemoryError> {
Ok(self.atom_size * (el_size / PAGE_4K))
}
}

#[cfg(test)]
Expand Down
9 changes: 8 additions & 1 deletion kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use mem::{
use scheduler::Scheduler;
use serial::{Serial, baud::SerialBaud};
use timer::kernel_ticks;
use util::bytes::HumanBytes;
use util::{bytes::HumanBytes, consts::PAGE_4K};

#[global_allocator]
static ALLOC: KernelAllocator = KernelAllocator::new();
Expand Down Expand Up @@ -90,6 +90,13 @@ fn main(kbh: &KernelBootHeader) {

logln!("Init PhysMemoryManager");
let pmm = Pmm::new(kbh.phys_mem_map).unwrap();
let free_pages = pmm.pages_free().unwrap();

logln!(
"Pages Free {} ({})",
free_pages,
HumanBytes::from(free_pages * PAGE_4K)
);
mem::pmm::set_physical_memory_manager(pmm);

logln!("Init VirtMemoryManager");
Expand Down

0 comments on commit 7916a7f

Please sign in to comment.