Skip to content

Commit

Permalink
add add and sub to MemoryAddr
Browse files Browse the repository at this point in the history
  • Loading branch information
aarkegz committed Aug 23, 2024
1 parent 1ce711c commit 85477b1
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
26 changes: 26 additions & 0 deletions memory_addr/src/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ pub trait MemoryAddr:
/// Unlike `<*const T>::offset`, this method always wraps around on overflow,
/// as in `<*const T>::wrapping_offset`.
#[inline]
#[must_use = "this returns a new address, without modifying the original"]
fn offset(self, offset: isize) -> Self {
Self::from(usize::wrapping_add_signed(self.into(), offset))
}
Expand All @@ -92,9 +93,34 @@ pub trait MemoryAddr:
///
/// Unlike `<*const T>::offset_from`, this method always wraps around on overflow.
#[inline]
#[must_use = "this function has no side effects, so it can be removed if the return value is unused"]
fn offset_from(self, base: Self) -> isize {
usize::wrapping_sub(self.into(), base.into()) as isize
}

/// Adds a given **unsigned** offset to the address to get a new address.
///
/// This method is similar to `offset`, but it takes an unsigned offset. It's a
/// convenience of `offset(offset as isize)`.
///
/// Unlike `<*const T>::add`, this method always wraps around on overflow,
/// as in `<*const T>::wrapping_add`.
#[inline]
fn add(self, rhs: usize) -> Self {
Self::from(usize::wrapping_add(self.into(), rhs))
}

/// Subtracts a given **unsigned** offset from the address to get a new address.
///
/// This method is similar to `add`, but it subtracts the offset. It's a convenience
/// of `offset(-(offset as isize))`.
///
/// Unlike `<*const T>::sub`, this method always wraps around on overflow,
/// as in `<*const T>::wrapping_sub`.
#[inline]
fn sub(self, rhs: usize) -> Self {
Self::from(usize::wrapping_sub(self.into(), rhs))
}
}

// Implement the `MemoryAddr` trait for any type that is `Copy`, `From<usize>`, `Into<usize>`, and `Ord`.
Expand Down
2 changes: 1 addition & 1 deletion memory_addr/src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ where
pub fn from_start_size(start: A, size: usize) -> Self {
Self {
start,
end: start.offset(size as isize),
end: start.add(size),
}
}

Expand Down
13 changes: 6 additions & 7 deletions memory_set/src/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl<B: MappingBackend> MemoryArea<B> {
if !self.backend.unmap(self.start(), unmap_size, page_table) {
return Err(MappingError::BadState);
}
self.va_range.start = self.va_range.start.offset(unmap_size as isize);
self.va_range.start = self.va_range.start.add(unmap_size);
Ok(())
}

Expand All @@ -121,14 +121,13 @@ impl<B: MappingBackend> MemoryArea<B> {
page_table: &mut B::PageTable,
) -> MappingResult {
let unmap_size = self.size() - new_size;
if !self.backend.unmap(
self.start().offset(new_size as isize),
unmap_size,
page_table,
) {
if !self
.backend
.unmap(self.start().add(new_size), unmap_size, page_table)
{
return Err(MappingError::BadState);
}
self.va_range.end = self.va_range.end.offset(-(unmap_size as isize));
self.va_range.end = self.va_range.end.sub(unmap_size);
Ok(())
}

Expand Down
6 changes: 3 additions & 3 deletions memory_set/src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ impl<B: MappingBackend> MemorySet<B> {
// brute force: try each area's end address as the start.
let mut last_end = hint.max(limit.start);
for (&addr, area) in self.areas.iter() {
if last_end.offset(size as isize) <= addr {
if last_end.add(size) <= addr {
return Some(last_end);
}
last_end = area.end();
}
if last_end.offset(size as isize) <= limit.end {
if last_end.add(size) <= limit.end {
Some(last_end)
} else {
None
Expand Down Expand Up @@ -201,7 +201,7 @@ impl<B: MappingBackend> MemorySet<B> {
update_flags: impl Fn(B::Flags) -> Option<B::Flags>,
page_table: &mut B::PageTable,
) -> MappingResult {
let end = start.offset(size as isize);
let end = start.add(size);
let mut to_insert = Vec::new();
for (area_start, area) in self.areas.iter_mut() {
let area_start = *area_start;
Expand Down

0 comments on commit 85477b1

Please sign in to comment.