22// Copyright (C) 2020 Red Hat, Inc. All rights reserved.
33// SPDX-License-Identifier: Apache-2.0
44
5- //! A wrapper over an `ArcSwap<IoMemory >` struct to support RCU-style mutability.
5+ //! A wrapper over an `ArcSwap<GuestMemory >` 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};
1515use std:: ops:: Deref ;
1616use std:: sync:: { Arc , LockResult , Mutex , MutexGuard , PoisonError } ;
1717
18- use crate :: { GuestAddressSpace , IoMemory } ;
18+ use crate :: { GuestAddressSpace , GuestMemory } ;
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 `IoMemory ` object, and
23+ /// every update of the memory map creates a completely new `GuestMemory ` 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 : IoMemory > {
28+ pub struct GuestMemoryAtomic < M : GuestMemory > {
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: IoMemory> {
3434 inner : Arc < ( ArcSwap < M > , Mutex < ( ) > ) > ,
3535}
3636
37- impl < M : IoMemory > From < Arc < M > > for GuestMemoryAtomic < M > {
37+ impl < M : GuestMemory > From < Arc < M > > for GuestMemoryAtomic < M > {
3838 /// create a new `GuestMemoryAtomic` object whose initial contents come from
39- /// the `map` reference counted `IoMemory `.
39+ /// the `map` reference counted `GuestMemory `.
4040 fn from ( map : Arc < M > ) -> Self {
4141 let inner = ( ArcSwap :: new ( map) , Mutex :: new ( ( ) ) ) ;
4242 GuestMemoryAtomic {
@@ -45,9 +45,9 @@ impl<M: IoMemory> From<Arc<M>> for GuestMemoryAtomic<M> {
4545 }
4646}
4747
48- impl < M : IoMemory > GuestMemoryAtomic < M > {
48+ impl < M : GuestMemory > GuestMemoryAtomic < M > {
4949 /// create a new `GuestMemoryAtomic` object whose initial contents come from
50- /// the `map` `IoMemory `.
50+ /// the `map` `GuestMemory `.
5151 pub fn new ( map : M ) -> Self {
5252 Arc :: new ( map) . into ( )
5353 }
@@ -75,15 +75,15 @@ impl<M: IoMemory> GuestMemoryAtomic<M> {
7575 }
7676}
7777
78- impl < M : IoMemory > Clone for GuestMemoryAtomic < M > {
78+ impl < M : GuestMemory > Clone for GuestMemoryAtomic < M > {
7979 fn clone ( & self ) -> Self {
8080 Self {
8181 inner : self . inner . clone ( ) ,
8282 }
8383 }
8484}
8585
86- impl < M : IoMemory > GuestAddressSpace for GuestMemoryAtomic < M > {
86+ impl < M : GuestMemory > GuestAddressSpace for GuestMemoryAtomic < M > {
8787 type T = GuestMemoryLoadGuard < M > ;
8888 type M = M ;
8989
@@ -94,14 +94,14 @@ impl<M: IoMemory> 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 `IoMemory `, so it can be used transparently to
97+ /// a snapshot of the `GuestMemory `, so it can be used transparently to
9898/// access memory.
9999#[ derive( Debug ) ]
100- pub struct GuestMemoryLoadGuard < M : IoMemory > {
100+ pub struct GuestMemoryLoadGuard < M : GuestMemory > {
101101 guard : Guard < Arc < M > > ,
102102}
103103
104- impl < M : IoMemory > GuestMemoryLoadGuard < M > {
104+ impl < M : GuestMemory > 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: IoMemory> GuestMemoryLoadGuard<M> {
112112 }
113113}
114114
115- impl < M : IoMemory > Clone for GuestMemoryLoadGuard < M > {
115+ impl < M : GuestMemory > 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 : IoMemory > Deref for GuestMemoryLoadGuard < M > {
123+ impl < M : GuestMemory > Deref for GuestMemoryLoadGuard < M > {
124124 type Target = M ;
125125
126126 fn deref ( & self ) -> & Self :: Target {
@@ -133,12 +133,12 @@ impl<M: IoMemory> 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 : IoMemory > {
136+ pub struct GuestMemoryExclusiveGuard < ' a , M : GuestMemory > {
137137 parent : & ' a GuestMemoryAtomic < M > ,
138138 _guard : MutexGuard < ' a , ( ) > ,
139139}
140140
141- impl < M : IoMemory > GuestMemoryExclusiveGuard < ' _ , M > {
141+ impl < M : GuestMemory > 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.
@@ -151,7 +151,7 @@ impl<M: IoMemory> GuestMemoryExclusiveGuard<'_, M> {
151151mod tests {
152152 use super :: * ;
153153 use crate :: region:: tests:: { new_guest_memory_collection_from_regions, Collection , MockRegion } ;
154- use crate :: { GuestAddress , GuestMemory , GuestMemoryRegion , GuestUsize , IoMemory } ;
154+ use crate :: { GuestAddress , GuestMemoryBackend , GuestMemoryRegion , GuestUsize , GuestMemory } ;
155155
156156 type GuestMemoryMmapAtomic = GuestMemoryAtomic < Collection > ;
157157
0 commit comments