48 lines
998 B
Rust
48 lines
998 B
Rust
use types::id::ProcessId;
|
|
|
|
use crate::error::KernelError;
|
|
|
|
pub struct ProcessMemory {
|
|
process: ProcessId,
|
|
page_count: usize,
|
|
limit: usize,
|
|
}
|
|
|
|
impl ProcessMemory {
|
|
pub fn new(process: ProcessId, limit: usize) -> Self {
|
|
Self {
|
|
process,
|
|
page_count: 0,
|
|
limit,
|
|
}
|
|
}
|
|
|
|
pub fn process(&self) -> ProcessId {
|
|
self.process
|
|
}
|
|
|
|
pub fn page_count(&self) -> usize {
|
|
self.page_count
|
|
}
|
|
|
|
pub fn limit(&self) -> usize {
|
|
self.limit
|
|
}
|
|
|
|
pub fn grant(&mut self, count: usize) -> Result<(), KernelError> {
|
|
if self.page_count + count > self.limit {
|
|
return Err(KernelError::OutOfMemory);
|
|
}
|
|
self.page_count += count;
|
|
Ok(())
|
|
}
|
|
|
|
pub fn release(&mut self, count: usize) -> Result<(), KernelError> {
|
|
if count > self.page_count {
|
|
return Err(KernelError::OutOfMemory);
|
|
}
|
|
self.page_count -= count;
|
|
Ok(())
|
|
}
|
|
}
|