//! Tail-page reclamation admission. Mapping's existing copy pins remain the //! authority for unmapping; this layer never decrements or bypasses them. use super::user_objects::Snapshot; const PAGE:u64=4096; #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub enum Error{Invalid,Owner,LiveObjects,Stale,Generation} #[derive(Debug,PartialEq,Eq)] pub enum CommitError{Invalid(Error),Mapping(E)} /// An inert proposal, not a frame lease. Replaying one after success is stale. #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct Proposal{owner:u64,base:u64,old_pages:usize,new_pages:usize,revision:u64} impl Proposal{ pub fn range(self)->(u64,usize){(self.base+self.new_pages as u64*PAGE,self.old_pages-self.new_pages)} } #[derive(Clone,Copy,Debug,PartialEq,Eq)] pub struct State{pub owner:u64,pub base:u64,pub pages:usize,pub revision:u64} pub struct Heap{state:State} impl Heap{ pub fn new(owner:u64,base:u64,pages:usize)->Result{ if owner==0||base==0||base%PAGE!=0||pages==0||pages>8||base.checked_add(pages as u64*PAGE).is_none(){return Err(Error::Invalid);} Ok(Self{state:State{owner,base,pages,revision:0}}) } pub fn state(&self)->State{self.state} /// Allocation callbacks must apply this committed bound before touching /// backing or publishing a user allocator slot after shrink. pub fn contains(&self,address:u64,bytes:u64)->bool{ address>=self.state.base&&address.checked_add(bytes).is_some_and(|end|end<=self.state.base+self.state.pages as u64*PAGE) } pub fn prepare(&self,pages:usize,objects:&Snapshot)->Result{ let s=self.state; if pages==0||pages>=s.pages{return Err(Error::Invalid);} if objects.owner!=s.owner{return Err(Error::Owner);} let old_end=s.base+s.pages as u64*PAGE;let new_end=s.base+pages as u64*PAGE; if objects.base!=s.base||objects.endobjects.end{return Err(Error::Invalid);} if addressnew_end{return Err(Error::LiveObjects);} } s.revision.checked_add(1).ok_or(Error::Generation)?; Ok(Proposal{owner:s.owner,base:s.base,old_pages:s.pages,new_pages:pages,revision:s.revision}) } /// `unmap` must be the retained Mapping operation (or PinnedRange's /// attempt_unmap), including real pin checks, BBM/TLBI and frame custody. /// On error it must preserve the complete old mapping and ownership. pub fn commit(&mut self,proposal:Proposal,unmap:impl FnOnce(u64,usize)->Result<(),E>)->Result>{ let s=self.state; if (proposal.owner,proposal.base,proposal.old_pages,proposal.revision)!=(s.owner,s.base,s.pages,s.revision){return Err(CommitError::Invalid(Error::Stale));} let next=s.revision.checked_add(1).ok_or(CommitError::Invalid(Error::Generation))?; let (start,pages)=proposal.range();unmap(start,pages).map_err(CommitError::Mapping)?; self.state.pages=proposal.new_pages;self.state.revision=next;Ok(pages) } }