Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 36 additions & 9 deletions src/address_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ impl AddressSpace {
let mut addr_iter = PAGE_SIZE; // let's not map page 0
let mut gap;
for mapping in &self.mappings {
gap = mapping.addr - addr_iter;
if gap > span + 2 * PAGE_SIZE {
gap = mapping.addr - addr_iter; // space between
if gap > span + 2 * PAGE_SIZE { // this means there is space
break;
}
addr_iter = mapping.addr + mapping.span;
Expand All @@ -91,6 +91,7 @@ impl AddressSpace {
///
/// # Errors
/// If there is insufficient room subsequent to `start`.
///
pub fn add_mapping_at<D: DataSource + 'static>(
&mut self,
source: Arc<D>,
Expand All @@ -99,19 +100,33 @@ impl AddressSpace {
start: VirtualAddress,
flags: FlagBuilder
) -> Result<(), &str> {
todo!()
// do not need go through the work of finding available space
// just test whether start address given is valid for datasource to be mapped to
if start + span + 2 * PAGE_SIZE < VADDR_MAX {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the requested mapping overlaps with an existing mapping?

let mapping_addr = start + PAGE_SIZE;
let new_mapping = MapEntry::new(source, offset, span, mapping_addr, flags);
self.mappings.push(new_mapping);
self.mappings.sort_by(|a, b| a.addr.cmp(&b.addr));
return Ok(());
}
Err("out of address space!")
}

/// Remove the mapping to `DataSource` that starts at the given address.
///
/// # Errors
/// If the mapping could not be removed.
pub fn remove_mapping<D: DataSource>(
&self,
&mut self,
source: Arc<D>,
start: VirtualAddress,
) -> Result<(), &str> {
todo!()
if start < VADDR_MAX {
let index = self.mappings.iter().position(|r| r.addr == start).unwrap();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should you unwrap here? If there is no such mapping, should you panic, or return an error?

A more idiomatic approach is .position( ... ).map_err(|_| "invalid mapping")?. The map_err says "if this is an Err, replace it with this other error, and the ? says "if this is an Err, return it immediately.

self.mappings.remove(index);
return Ok(());
}
Err("out of address space!")
}

/// Look up the DataSource and offset within that DataSource for a
Expand All @@ -124,13 +139,25 @@ impl AddressSpace {
&self,
addr: VirtualAddress,
access_type: FlagBuilder,
) -> Result<(Arc<D>, usize), &str> {
todo!();
) -> Result<(Arc<dyn DataSource>, usize), &str> {
if addr > VADDR_MAX {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the address is greater than the max address, how would you ever have a mapping there?

if let Ok(mapping) = self.get_mapping_for_addr(addr) {
return Ok((mapping.source.clone(), mapping.offset));
}
}
Err("out of address space!")
}

/// Helper function for looking up mappings
fn get_mapping_for_addr(&self, addr: VirtualAddress) -> Result<MapEntry, &str> {
todo!();
fn get_mapping_for_addr(&self, addr: VirtualAddress) -> Result<&MapEntry, &str> {
if addr > VADDR_MAX {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the address is greater than the max address, how would you ever have a mapping there?

for mapping in &self.mappings {
if mapping.addr == addr {
return Ok(mapping);
}
}
}
Err("out of address space!")
}
}

Expand Down