Skip to content

Commit

Permalink
rename MemoryAddr::sub_addr to MemoryAddr::offset_from
Browse files Browse the repository at this point in the history
  • Loading branch information
aarkegz committed Aug 21, 2024
1 parent 77972d9 commit fe4511e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
11 changes: 8 additions & 3 deletions memory_addr/src/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,21 @@ pub trait MemoryAddr:

// About address modification:

/// Adds the given signed offset to the address.
/// Adds a given offset to the address to get a new address.
///
/// Unlike `<*const T>::offset`, this method always wraps around on overflow,
/// as in `<*const T>::wrapping_offset`.
#[inline]
fn offset(self, offset: isize) -> Self {
Self::from(usize::wrapping_add_signed(self.into(), offset))
}

/// Gets the distance between two addresses.
///
/// Unlike `<*const T>::offset_from`, this method always wraps around on overflow.
#[inline]
fn sub_addr(self, base: Self) -> usize {
usize::wrapping_sub(self.into(), base.into())
fn offset_from(self, base: Self) -> isize {
usize::wrapping_sub(self.into(), base.into()) as isize
}
}

Expand Down
6 changes: 5 additions & 1 deletion memory_addr/src/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,11 @@ where
/// ```
#[inline]
pub fn size(self) -> usize {
self.end.sub_addr(self.start)
if self.is_empty() {
0
} else {
self.end.offset_from(self.start) as usize
}
}

/// Checks if the range contains the given address.
Expand Down
2 changes: 1 addition & 1 deletion memory_set/src/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl<B: MappingBackend> MemoryArea<B> {
if self.start() < pos && pos < self.end() {
let new_area = Self::new(
pos,
self.end().sub_addr(pos),
self.end().offset_from(pos) as usize,
self.flags,
self.backend.clone(),
);
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 @@ -150,11 +150,11 @@ impl<B: MappingBackend> MemorySet<B> {
if before_end > start {
if before_end <= end {
// the unmapped area is at the end of `before`.
before.shrink_right(start.sub_addr(before_start), page_table)?;
before.shrink_right(start.offset_from(before_start) as usize, page_table)?;
} else {
// the unmapped area is in the middle `before`, need to split.
let right_part = before.split(end).unwrap();
before.shrink_right(start.sub_addr(before_start), page_table)?;
before.shrink_right(start.offset_from(before_start) as usize, page_table)?;
assert_eq!(right_part.start().into(), Into::<usize>::into(end));
self.areas.insert(end, right_part);
}
Expand All @@ -167,7 +167,7 @@ impl<B: MappingBackend> MemorySet<B> {
if after_start < end {
// the unmapped area is at the start of `after`.
let mut new_area = self.areas.remove(&after_start).unwrap();
new_area.shrink_left(after_end.sub_addr(end), page_table)?;
new_area.shrink_left(after_end.offset_from(end) as usize, page_table)?;
assert_eq!(new_area.start().into(), Into::<usize>::into(end));
self.areas.insert(end, new_area);
}
Expand Down

0 comments on commit fe4511e

Please sign in to comment.