diff --git a/kernel/src/mem.rs b/kernel/src/mem.rs index ca05c0a57..cdf39b5b7 100644 --- a/kernel/src/mem.rs +++ b/kernel/src/mem.rs @@ -114,6 +114,18 @@ impl MemoryManager { MEMORY_MANAGER.with(|ss| f(&mut ss.borrow_mut())) } + pub fn with(f: F) -> R + where + F: FnOnce(&MemoryManager) -> R, + { + #[cfg(baremetal)] + unsafe { + f(&MEMORY_MANAGER) + } + + #[cfg(not(baremetal))] + MEMORY_MANAGER.with(|ss| f(&ss.borrow_mut())) + } #[cfg(baremetal)] pub fn init_from_memory( &mut self, @@ -168,6 +180,20 @@ impl MemoryManager { Ok(()) } + /// Print the number of RAM bytes used by the specified process. + /// This does not include memory such as peripherals and CSRs. + pub fn ram_used_by(&self, pid: PID) -> usize { + let mut owned_bytes = 0; + unsafe { + for owner in &MEMORY_ALLOCATIONS[0..self.ram_size / PAGE_SIZE] { + if owner == &Some(pid) { + owned_bytes += PAGE_SIZE; + } + } + } + owned_bytes + } + #[cfg(all(baremetal, feature = "print-debug"))] pub fn print_ownership(&self) { println!("Ownership ({} bytes in all):", unsafe {