//! Object metadata belongs to one process arena. Allocation descriptors carry //! the arena owner and generation as well as the user virtual address. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Error { Invalid, NoMemory, Full, Foreign, BadPointer, Generation } impl Error { pub const fn errno(self)->u64 {match self { Self::Foreign|Self::BadPointer=>14, Self::NoMemory|Self::Full=>12, Self::Invalid=>22, Self::Generation=>75, }} } #[repr(C)] #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Allocation { pub owner:u64, pub generation:u64, pub address:u64, pub bytes:u64, pub slot:u64 } #[derive(Clone, Copy, Debug, PartialEq, Eq)] struct Slot { generation:u64, address:u64, bytes:u64, alignment:u64, live:bool } impl Slot { const EMPTY:Self=Self{generation:0,address:0,bytes:0,alignment:0,live:false}; } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub struct Snapshot { pub owner:u64, pub base:u64, pub end:u64, pub generations:[u64;N], pub entries:[Option<(u64,u64,u64)>;N], } #[derive(Debug, PartialEq, Eq)] pub enum AccessError { Invalid(Error), Backing(E) } pub struct Arena { owner:u64, base:u64, end:u64, slots:[Slot;N] } impl Arena { pub fn new(owner:u64,base:u64,bytes:u64)->Result { let end=base.checked_add(bytes).ok_or(Error::Invalid)?; if owner==0 || base==0 || bytes==0 || N==0 {return Err(Error::Invalid);} Ok(Self{owner,base,end,slots:[Slot::EMPTY;N]}) } pub fn snapshot(&self)->Snapshot { Snapshot{owner:self.owner,base:self.base,end:self.end, generations:core::array::from_fn(|i|self.slots[i].generation), entries:core::array::from_fn(|i|{let s=self.slots[i];s.live.then_some((s.address,s.bytes,s.alignment))})} } pub fn live(&self)->usize {self.slots.iter().filter(|s|s.live).count()} fn aligned(value:u64,alignment:u64)->Result { value.checked_add(alignment-1).map(|v|v&!(alignment-1)).ok_or(Error::NoMemory) } fn placement(&self,bytes:u64,alignment:u64)->Result<(usize,u64,u64),Error> { if bytes==0 || alignment==0 || !alignment.is_power_of_two() || alignment>4096 {return Err(Error::Invalid);} let slot=self.slots.iter().position(|s|!s.live&&s.generationself.end {return Err(Error::NoMemory);} let overlap=self.slots.iter().filter(|s|s.live&&addressaddress=Self::aligned(after,alignment)?,None=>return Ok((slot,generation,address))} } Err(Error::NoMemory) } /// The initialization callback receives only a fully checked range. Failed /// initialization leaves the metadata and generation unchanged. pub fn allocate_with(&mut self,bytes:u64,alignment:u64,initialize:impl FnOnce(u64,u64)->Result<(),E>) ->Result> { let (slot,generation,address)=self.placement(bytes,alignment).map_err(AccessError::Invalid)?; initialize(address,bytes).map_err(AccessError::Backing)?; self.slots[slot]=Slot{generation,address,bytes,alignment,live:true}; Ok(Allocation{owner:self.owner,generation,address,bytes,slot:slot as u64}) } fn resolve(&self,allocation:Allocation)->Result { if allocation.owner!=self.owner {return Err(Error::Foreign);} let index=usize::try_from(allocation.slot).map_err(|_|Error::BadPointer)?; let slot=self.slots.get(index).ok_or(Error::BadPointer)?; if !slot.live || slot.generation!=allocation.generation || slot.address!=allocation.address || slot.bytes!=allocation.bytes { return Err(Error::BadPointer); } Ok(index) } /// Owner/slot/generation/address/length are checked before clearing bytes. /// The backing operation must not change allocator metadata reentrantly. pub fn free_with(&mut self,allocation:Allocation,clear:impl FnOnce(u64,u64)->Result<(),E>) ->Result<(),AccessError> { let index=self.resolve(allocation).map_err(AccessError::Invalid)?; let slot=self.slots[index]; clear(slot.address,slot.bytes).map_err(AccessError::Backing)?; self.slots[index]=Slot{generation:slot.generation,..Slot::EMPTY}; Ok(()) } }