kernel: mem: use iter instead of array access

This compresses the entirety of the memory range finder into a single
statement. As a result, duplicate code is removed and we can elide more
range checks.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2021-08-06 11:30:29 +08:00
parent 9dc4fcbf55
commit d27a2eb27e

View file

@ -249,27 +249,54 @@ impl MemoryManager {
#[cfg(baremetal)] #[cfg(baremetal)]
pub fn alloc_page(&mut self, pid: PID) -> Result<usize, xous_kernel::Error> { pub fn alloc_page(&mut self, pid: PID) -> Result<usize, xous_kernel::Error> {
// Go through all RAM pages looking for a free page. // Go through all RAM pages looking for a free page.
// Optimization: start from the previous address. println!("Allocating page for PID {}", pid);
// println!("Allocating page for PID {}", pid);
unsafe { unsafe {
for index in self.last_ram_page..((self.ram_size as usize) / PAGE_SIZE) { let end_point = self.ram_size / PAGE_SIZE;
// println!(" Checking {:08x}...", index * PAGE_SIZE + self.ram_start as usize); let starting_point = self.last_ram_page;
if MEMORY_ALLOCATIONS[index].is_none() { for (allocation, index) in MEMORY_ALLOCATIONS[starting_point..end_point]
MEMORY_ALLOCATIONS[index] = Some(pid); .iter_mut()
self.last_ram_page = index + 1; .zip(starting_point..)
let page = index * PAGE_SIZE + self.ram_start; .chain(MEMORY_ALLOCATIONS[..starting_point].iter_mut().zip(0..))
return Ok(page); {
} println!(
} " Checking {:08x}...",
for index in 0..self.last_ram_page { index * PAGE_SIZE + self.ram_start as usize
// println!(" Checking {:08x}...", index * PAGE_SIZE + self.ram_start as usize); );
if MEMORY_ALLOCATIONS[index].is_none() { if allocation.is_none() {
MEMORY_ALLOCATIONS[index] = Some(pid); *allocation = Some(pid);
self.last_ram_page = index + 1; self.last_ram_page = index + 1;
// if self.last_ram_page >= end_point {
// self.last_ram_page = 0;
// }
let page = index * PAGE_SIZE + self.ram_start; let page = index * PAGE_SIZE + self.ram_start;
return Ok(page); return Ok(page);
} }
} }
// for (index, allocation) in MEMORY_ALLOCATIONS
// [self.last_ram_page..((self.ram_size as usize) / PAGE_SIZE)]
// .iter_mut()
// .enumerate()
// {
// println!(" 1. Checking {:08x}...", (self.last_ram_page + index) * PAGE_SIZE + self.ram_start as usize);
// if allocation.is_none() {
// *allocation = Some(pid);
// self.last_ram_page = self.last_ram_page + index + 1;
// let page = (self.last_ram_page + index) * PAGE_SIZE + self.ram_start;
// return Ok(page);
// }
// }
// for (index, allocation) in MEMORY_ALLOCATIONS[..self.last_ram_page]
// .iter_mut()
// .enumerate()
// {
// println!(" 2. Checking {:08x}...", index * PAGE_SIZE + self.ram_start as usize);
// if allocation.is_none() {
// *allocation = Some(pid);
// self.last_ram_page = index + 1;
// let page = index * PAGE_SIZE + self.ram_start;
// return Ok(page);
// }
// }
} }
Err(xous_kernel::Error::OutOfMemory) Err(xous_kernel::Error::OutOfMemory)
} }
@ -386,7 +413,7 @@ impl MemoryManager {
// FIXME: Un-reserve addresses if we encounter an error here // FIXME: Un-reserve addresses if we encounter an error here
mm.reserve_address(self, virt, flags)?; mm.reserve_address(self, virt, flags)?;
} }
unsafe { xous_kernel::MemoryRange::new(virt_ptr as usize, size) } Ok(unsafe { xous_kernel::MemoryRange::new(virt_ptr as usize, size) }?)
} }
/// Attempt to allocate a single page from the default section. /// Attempt to allocate a single page from the default section.
@ -490,7 +517,7 @@ impl MemoryManager {
} }
} }
unsafe { MemoryRange::new(virt as usize, size) } Ok(unsafe { MemoryRange::new(virt as usize, size) }?)
} }
/// Attempt to map the given physical address into the virtual address space /// Attempt to map the given physical address into the virtual address space
@ -520,10 +547,10 @@ impl MemoryManager {
let phys_addr = crate::arch::mem::virt_to_phys(src_addr as usize)?; let phys_addr = crate::arch::mem::virt_to_phys(src_addr as usize)?;
crate::arch::mem::move_page_inner( crate::arch::mem::move_page_inner(
self, self,
src_mapping, &src_mapping,
src_addr, src_addr,
dest_pid, dest_pid,
dest_mapping, &dest_mapping,
dest_addr, dest_addr,
)?; )?;
self.claim_release_move( self.claim_release_move(
@ -552,10 +579,10 @@ impl MemoryManager {
// the page while it's borrowed. // the page while it's borrowed.
crate::arch::mem::lend_page_inner( crate::arch::mem::lend_page_inner(
self, self,
src_mapping, &src_mapping,
src_addr as _, src_addr as _,
dest_pid, dest_pid,
dest_mapping, &dest_mapping,
dest_addr as _, dest_addr as _,
mutable, mutable,
) )
@ -576,10 +603,10 @@ impl MemoryManager {
// the page while it's borrowed. // the page while it's borrowed.
crate::arch::mem::return_page_inner( crate::arch::mem::return_page_inner(
self, self,
src_mapping, &src_mapping,
src_addr, src_addr,
dest_pid, dest_pid,
dest_mapping, &dest_mapping,
dest_addr, dest_addr,
) )
} }
@ -647,8 +674,10 @@ impl MemoryManager {
let addr = addr as usize; let addr = addr as usize;
// Ensure the address lies on a page boundary // Ensure the address lies on a page boundary
if cfg!(baremetal) && addr & 0xfff != 0 { if cfg!(baremetal) {
return Err(xous_kernel::Error::BadAlignment); if addr & 0xfff != 0 {
return Err(xous_kernel::Error::BadAlignment);
}
} }
let mut offset = 0; let mut offset = 0;