Skip to content

Commit e22be31

Browse files
bonziniandreeaflorescu
authored andcommitted
GuestAddressSpace: mark the GuestMemory smart pointer as cloneable
This makes it possible to upgrade the short-lived pointer returned by memory() to an owned pointer that can be stored. The bound is already provided by the trivial GuestAddressSpaces (references and Rc/Arc) and also by ArcSwap via vorner/arc-swap#26. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 43f7b64 commit e22be31

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ backend-atomic = ["arc-swap"]
1717

1818
[dependencies]
1919
libc = ">=0.2.39"
20-
arc-swap = { version = ">=0.4.4", optional = true }
20+
arc-swap = { version = ">=0.4.5", optional = true }
2121

2222
[target.'cfg(windows)'.dependencies.winapi]
2323
version = ">=0.3"

coverage_config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"coverage_score": 85.6,
2+
"coverage_score": 85.7,
33
"exclude_path": "mmap_windows.rs",
44
"crate_features": "backend-mmap,backend-atomic"
55
}

src/atomic.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,14 @@ impl<M: GuestMemory> GuestMemoryLoadGuard<M> {
104104
}
105105
}
106106

107+
impl<M: GuestMemory> Clone for GuestMemoryLoadGuard<M> {
108+
fn clone(&self) -> Self {
109+
GuestMemoryLoadGuard {
110+
guard: Guard::from_inner(Arc::clone(&*self.guard)),
111+
}
112+
}
113+
}
114+
107115
impl<M: GuestMemory> Deref for GuestMemoryLoadGuard<M> {
108116
type Target = M;
109117

@@ -203,6 +211,22 @@ mod tests {
203211
assert!(mem3.find_region(GuestAddress(0x10000)).is_none());
204212
}
205213

214+
#[test]
215+
fn test_clone_guard() {
216+
let region_size = 0x400;
217+
let regions = vec![
218+
(GuestAddress(0x0), region_size),
219+
(GuestAddress(0x1000), region_size),
220+
];
221+
let gmm = GuestMemoryMmap::from_ranges(&regions).unwrap();
222+
let gm = GuestMemoryMmapAtomic::new(gmm);
223+
let mem = {
224+
let guard1 = gm.memory();
225+
Clone::clone(&guard1)
226+
};
227+
assert_eq!(mem.num_regions(), 2);
228+
}
229+
206230
#[test]
207231
fn test_atomic_hotplug() {
208232
let region_size = 0x1000;

src/guest_memory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ pub trait GuestAddressSpace {
356356
type M: GuestMemory;
357357

358358
/// A type that provides access to the memory.
359-
type T: Deref<Target = Self::M>;
359+
type T: Clone + Deref<Target = Self::M>;
360360

361361
/// Return an object (e.g. a reference or guard) that can be used
362362
/// to access memory through this address space. The object provides

0 commit comments

Comments
 (0)