Skip to content

Commit e539af1

Browse files
committed
Use iterator to replace GuestMemory's redundant methods
Now we have iterator for the GuestMemory trait, so remove with_regions(), with_regions_mut() and map_and_fold(). Signed-off-by: Liu Jiang <[email protected]>
1 parent b7f0711 commit e539af1

File tree

2 files changed

+6
-98
lines changed

2 files changed

+6
-98
lines changed

src/guest_memory.rs

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -201,63 +201,8 @@ pub trait GuestMemory<'a> {
201201
/// Gets an iterator over the entries in the collection.
202202
fn iter(&'a self) -> Self::I;
203203

204-
/// Perform the specified action on each region.
205-
/// It only walks children of current region and do not step into sub regions.
206-
fn with_regions<F, E>(&self, cb: F) -> std::result::Result<(), E>
207-
where
208-
F: Fn(usize, &Self::R) -> std::result::Result<(), E>;
209-
210-
/// Perform the specified action on each region mutably.
211-
/// It only walks children of current region and do not step into sub regions.
212-
fn with_regions_mut<F, E>(&self, cb: F) -> std::result::Result<(), E>
213-
where
214-
F: FnMut(usize, &Self::R) -> std::result::Result<(), E>;
215-
216-
/// Applies two functions, specified as callbacks, on the inner memory regions.
217-
///
218-
/// # Arguments
219-
/// * `init` - Starting value of the accumulator for the `foldf` function.
220-
/// * `mapf` - "Map" function, applied to all the inner memory regions. It returns an array of
221-
/// the same size as the memory regions array, containing the function's results
222-
/// for each region.
223-
/// * `foldf` - "Fold" function, applied to the array returned by `mapf`. It acts as an
224-
/// operator, applying itself to the `init` value and to each subsequent elemnent
225-
/// in the array returned by `mapf`.
226-
///
227-
/// # Examples
228-
///
229-
/// * Compute the total size of all memory mappings in KB by iterating over the memory regions
230-
/// and dividing their sizes to 1024, then summing up the values in an accumulator.
231-
///
232-
/// ```
233-
/// # #[cfg(feature = "backend-mmap")]
234-
/// # fn test_map_fold() -> Result<(), ()> {
235-
/// # use vm_memory::{GuestAddress, GuestMemory, GuestMemoryRegion, mmap::GuestMemoryMmap};
236-
/// let start_addr1 = GuestAddress(0x0);
237-
/// let start_addr2 = GuestAddress(0x400);
238-
/// let mem = GuestMemoryMmap::new(&vec![(start_addr1, 1024), (start_addr2, 2048)]).unwrap();
239-
/// let total_size = mem.map_and_fold(
240-
/// 0,
241-
/// |(_, region)| region.len() / 1024,
242-
/// |acc, size| acc + size
243-
/// );
244-
/// println!("Total memory size = {} KB", total_size);
245-
/// Ok(())
246-
/// # }
247-
/// ```
248-
fn map_and_fold<F, G, T>(&self, init: T, mapf: F, foldf: G) -> T
249-
where
250-
F: Fn((usize, &Self::R)) -> T,
251-
G: Fn(T, T) -> T;
252-
253204
/// Get maximum (inclusive) address managed by the region.
254-
fn end_addr(&self) -> GuestAddress {
255-
self.map_and_fold(
256-
GuestAddress(0),
257-
|(_, region)| region.end_addr(),
258-
std::cmp::max,
259-
)
260-
}
205+
fn end_addr(&self) -> GuestAddress;
261206

262207
/// Convert an absolute address into an address space (GuestMemory)
263208
/// to a relative address within this region, or return None if

src/mmap.rs

Lines changed: 5 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -484,32 +484,11 @@ impl<'a> GuestMemory<'a> for GuestMemoryMmap {
484484
self.regions.iter()
485485
}
486486

487-
fn with_regions<F, E>(&self, cb: F) -> std::result::Result<(), E>
488-
where
489-
F: Fn(usize, &Self::R) -> std::result::Result<(), E>,
490-
{
491-
for (index, region) in self.regions.iter().enumerate() {
492-
cb(index, region)?;
493-
}
494-
Ok(())
495-
}
496-
497-
fn with_regions_mut<F, E>(&self, mut cb: F) -> std::result::Result<(), E>
498-
where
499-
F: FnMut(usize, &Self::R) -> std::result::Result<(), E>,
500-
{
501-
for (index, region) in self.regions.iter().enumerate() {
502-
cb(index, region)?;
503-
}
504-
Ok(())
505-
}
506-
507-
fn map_and_fold<F, G, T>(&self, init: T, mapf: F, foldf: G) -> T
508-
where
509-
F: Fn((usize, &Self::R)) -> T,
510-
G: Fn(T, T) -> T,
511-
{
512-
self.regions.iter().enumerate().map(mapf).fold(init, foldf)
487+
fn end_addr(&self) -> GuestAddress {
488+
self.regions
489+
.iter()
490+
.max_by_key(|x| x.start_addr())
491+
.map_or_else(|| GuestAddress(0), |x| x.end_addr())
513492
}
514493
}
515494

@@ -769,24 +748,8 @@ mod tests {
769748
(GuestAddress(0x0), region_size),
770749
(GuestAddress(0x1000), region_size),
771750
];
772-
let mut iterated_regions = Vec::new();
773751
let gm = GuestMemoryMmap::new(&regions).unwrap();
774752

775-
let res: Result<()> = gm.with_regions(|_, region| {
776-
assert_eq!(region.len(), region_size as GuestUsize);
777-
Ok(())
778-
});
779-
assert!(res.is_ok());
780-
781-
let res: Result<()> = gm.with_regions_mut(|_, region| {
782-
iterated_regions.push((region.start_addr(), region.len() as usize));
783-
Ok(())
784-
});
785-
assert!(res.is_ok());
786-
assert_eq!(regions, iterated_regions);
787-
assert_eq!(gm.clone().regions[0].guest_base, regions[0].0);
788-
assert_eq!(gm.clone().regions[1].guest_base, regions[1].0);
789-
790753
let mut size = 0;
791754
let mut count = 0;
792755
for region in gm.iter() {

0 commit comments

Comments
 (0)