Skip to content

Commit 62d2c37

Browse files
committed
Allow any IoMemory for GuestMemoryAtomic
This simply makes `GuestMemoryAtomic` more general. (However, this change requires the preceding commit that relaxed the `GuestAddressSpace::M` requirement from `GuestMemory` to `IoMemory`.) Signed-off-by: Hanna Czenczek <[email protected]>
1 parent 442297b commit 62d2c37

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

src/atomic.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Copyright (C) 2020 Red Hat, Inc. All rights reserved.
33
// SPDX-License-Identifier: Apache-2.0
44

5-
//! A wrapper over an `ArcSwap<GuestMemory>` struct to support RCU-style mutability.
5+
//! A wrapper over an `ArcSwap<IoMemory>` struct to support RCU-style mutability.
66
//!
77
//! With the `backend-atomic` feature enabled, simply replacing `GuestMemoryMmap`
88
//! with `GuestMemoryAtomic<GuestMemoryMmap>` will enable support for mutable memory maps.
@@ -15,17 +15,17 @@ use arc_swap::{ArcSwap, Guard};
1515
use std::ops::Deref;
1616
use std::sync::{Arc, LockResult, Mutex, MutexGuard, PoisonError};
1717

18-
use crate::{GuestAddressSpace, GuestMemory};
18+
use crate::{GuestAddressSpace, IoMemory};
1919

2020
/// A fast implementation of a mutable collection of memory regions.
2121
///
2222
/// This implementation uses `ArcSwap` to provide RCU-like snapshotting of the memory map:
23-
/// every update of the memory map creates a completely new `GuestMemory` object, and
23+
/// every update of the memory map creates a completely new `IoMemory` object, and
2424
/// readers will not be blocked because the copies they retrieved will be collected once
2525
/// no one can access them anymore. Under the assumption that updates to the memory map
2626
/// are rare, this allows a very efficient implementation of the `memory()` method.
2727
#[derive(Debug)]
28-
pub struct GuestMemoryAtomic<M: GuestMemory> {
28+
pub struct GuestMemoryAtomic<M: IoMemory> {
2929
// GuestAddressSpace<M>, which we want to implement, is basically a drop-in
3030
// replacement for &M. Therefore, we need to pass to devices the `GuestMemoryAtomic`
3131
// rather than a reference to it. To obtain this effect we wrap the actual fields
@@ -34,9 +34,9 @@ pub struct GuestMemoryAtomic<M: GuestMemory> {
3434
inner: Arc<(ArcSwap<M>, Mutex<()>)>,
3535
}
3636

37-
impl<M: GuestMemory> From<Arc<M>> for GuestMemoryAtomic<M> {
37+
impl<M: IoMemory> From<Arc<M>> for GuestMemoryAtomic<M> {
3838
/// create a new `GuestMemoryAtomic` object whose initial contents come from
39-
/// the `map` reference counted `GuestMemory`.
39+
/// the `map` reference counted `IoMemory`.
4040
fn from(map: Arc<M>) -> Self {
4141
let inner = (ArcSwap::new(map), Mutex::new(()));
4242
GuestMemoryAtomic {
@@ -45,9 +45,9 @@ impl<M: GuestMemory> From<Arc<M>> for GuestMemoryAtomic<M> {
4545
}
4646
}
4747

48-
impl<M: GuestMemory> GuestMemoryAtomic<M> {
48+
impl<M: IoMemory> GuestMemoryAtomic<M> {
4949
/// create a new `GuestMemoryAtomic` object whose initial contents come from
50-
/// the `map` `GuestMemory`.
50+
/// the `map` `IoMemory`.
5151
pub fn new(map: M) -> Self {
5252
Arc::new(map).into()
5353
}
@@ -75,15 +75,15 @@ impl<M: GuestMemory> GuestMemoryAtomic<M> {
7575
}
7676
}
7777

78-
impl<M: GuestMemory> Clone for GuestMemoryAtomic<M> {
78+
impl<M: IoMemory> Clone for GuestMemoryAtomic<M> {
7979
fn clone(&self) -> Self {
8080
Self {
8181
inner: self.inner.clone(),
8282
}
8383
}
8484
}
8585

86-
impl<M: GuestMemory> GuestAddressSpace for GuestMemoryAtomic<M> {
86+
impl<M: IoMemory> GuestAddressSpace for GuestMemoryAtomic<M> {
8787
type T = GuestMemoryLoadGuard<M>;
8888
type M = M;
8989

@@ -94,14 +94,14 @@ impl<M: GuestMemory> GuestAddressSpace for GuestMemoryAtomic<M> {
9494

9595
/// A guard that provides temporary access to a `GuestMemoryAtomic`. This
9696
/// object is returned from the `memory()` method. It dereference to
97-
/// a snapshot of the `GuestMemory`, so it can be used transparently to
97+
/// a snapshot of the `IoMemory`, so it can be used transparently to
9898
/// access memory.
9999
#[derive(Debug)]
100-
pub struct GuestMemoryLoadGuard<M: GuestMemory> {
100+
pub struct GuestMemoryLoadGuard<M: IoMemory> {
101101
guard: Guard<Arc<M>>,
102102
}
103103

104-
impl<M: GuestMemory> GuestMemoryLoadGuard<M> {
104+
impl<M: IoMemory> GuestMemoryLoadGuard<M> {
105105
/// Make a clone of the held pointer and returns it. This is more
106106
/// expensive than just using the snapshot, but it allows to hold on
107107
/// to the snapshot outside the scope of the guard. It also allows
@@ -112,15 +112,15 @@ impl<M: GuestMemory> GuestMemoryLoadGuard<M> {
112112
}
113113
}
114114

115-
impl<M: GuestMemory> Clone for GuestMemoryLoadGuard<M> {
115+
impl<M: IoMemory> Clone for GuestMemoryLoadGuard<M> {
116116
fn clone(&self) -> Self {
117117
GuestMemoryLoadGuard {
118118
guard: Guard::from_inner(Arc::clone(&*self.guard)),
119119
}
120120
}
121121
}
122122

123-
impl<M: GuestMemory> Deref for GuestMemoryLoadGuard<M> {
123+
impl<M: IoMemory> Deref for GuestMemoryLoadGuard<M> {
124124
type Target = M;
125125

126126
fn deref(&self) -> &Self::Target {
@@ -133,12 +133,12 @@ impl<M: GuestMemory> Deref for GuestMemoryLoadGuard<M> {
133133
/// possibly after updating the memory map represented by the
134134
/// `GuestMemoryAtomic` that created the guard.
135135
#[derive(Debug)]
136-
pub struct GuestMemoryExclusiveGuard<'a, M: GuestMemory> {
136+
pub struct GuestMemoryExclusiveGuard<'a, M: IoMemory> {
137137
parent: &'a GuestMemoryAtomic<M>,
138138
_guard: MutexGuard<'a, ()>,
139139
}
140140

141-
impl<M: GuestMemory> GuestMemoryExclusiveGuard<'_, M> {
141+
impl<M: IoMemory> GuestMemoryExclusiveGuard<'_, M> {
142142
/// Replace the memory map in the `GuestMemoryAtomic` that created the guard
143143
/// with the new memory map, `map`. The lock is then dropped since this
144144
/// method consumes the guard.

0 commit comments

Comments
 (0)