Skip to content

Commit 1f59518

Browse files
committed
Add naive default impl for GuestMemory::find_region()
This function can be default-implemented in terms of `GuestMemory::iter()`. Downstream impls can overwrite this more specialized and efficient versions of course (such as GuestMemoryMmap using a binary search). Signed-off-by: Patrick Roy <[email protected]>
1 parent 3f2fd80 commit 1f59518

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
- \[[#307](https://github.com/rust-vmm/vm-memory/pull/304)\] Move `read_volatile_from`, `read_exact_volatile_from`,
1212
`write_volatile_to` and `write_all_volatile_to` functions from the `GuestMemory` trait to the `Bytes` trait.
13+
- \[[#312](https://github.com/rust-vmm/vm-memory/pull/312)\]: Give `GuestMemory::find_region` a default implementation,
14+
based on linear search.
1315

1416
### Removed
1517

src/guest_memory.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,9 @@ pub trait GuestMemory {
408408
fn num_regions(&self) -> usize;
409409

410410
/// Returns the region containing the specified address or `None`.
411-
fn find_region(&self, addr: GuestAddress) -> Option<&Self::R>;
411+
fn find_region(&self, addr: GuestAddress) -> Option<&Self::R> {
412+
self.iter().find(|region| addr >= region.start_addr() && addr <= region.last_addr())
413+
}
412414

413415
/// Gets an iterator over the entries in the collection.
414416
///

0 commit comments

Comments
 (0)