Skip to content

Commit 31b28b0

Browse files
committed
refactor: scatter mmap.rs contents to unix/windows modules
This is some duplication, but it once and for all eliminates all cfg fuckery and code that expects precisely one module to be defined. Admittedly, for windows/unix we could have stuck with the cfg stuff, but I wanted a clean slate for now. The windows parts are compile-tested only, as I do not have a windows system available. The handling of various from_range functions is based on the assumption that these were only used in test code and nowhere else (hence all the preceding refactors of test code). In unit tests, their use has been eliminated, and doc tests now got a cfg(target_family="unix", not(feature="xen")) so that they only run when mmap_unix is available. This is fine, because they are examples of how to use specific functions, and not any serious tests (e.g. dont need to run for all supported backends). Signed-off-by: Patrick Roy <[email protected]>
1 parent 0f2e831 commit 31b28b0

File tree

8 files changed

+305
-225
lines changed

8 files changed

+305
-225
lines changed

src/bitmap/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ pub(crate) mod tests {
219219
// them to test the mmap-based backend implementations for now. Going forward, the generic
220220
// test functions defined here can be placed in a separate module (i.e. `test_utilities`)
221221
// which is gated by a feature and can be used for testing purposes by other crates as well.
222-
#[cfg(feature = "backend-mmap")]
222+
#[cfg(all(feature = "backend-mmap", target_family = "unix"))]
223223
fn test_guest_memory_region<R: GuestMemoryRegion>(region: &R) {
224224
let dirty_addr = MemoryRegionAddress(0x0);
225225
let val = 123u64;
@@ -253,7 +253,7 @@ pub(crate) mod tests {
253253
);
254254
}
255255

256-
#[cfg(feature = "backend-mmap")]
256+
#[cfg(all(feature = "backend-mmap", target_family = "unix"))]
257257
// Assumptions about M generated by f ...
258258
pub fn test_guest_memory_and_region<M, F>(f: F)
259259
where

src/bytes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ pub trait Bytes<A> {
331331
/// * Read bytes from /dev/urandom (uses the `backend-mmap` feature)
332332
///
333333
/// ```
334-
/// # #[cfg(all(feature = "backend-mmap", feature = "rawfd"))]
334+
/// # #[cfg(all(feature = "backend-mmap", feature = "rawfd", target_family = "unix", not(feature = "xen")))]
335335
/// # {
336336
/// # use vm_memory::{Address, GuestMemory, Bytes, GuestAddress, GuestMemoryMmap};
337337
/// # use std::fs::File;

src/guest_memory.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl FileOffset {
176176
/// # Examples (uses the `backend-mmap` and `backend-atomic` features)
177177
///
178178
/// ```
179-
/// # #[cfg(feature = "backend-mmap")]
179+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
180180
/// # {
181181
/// # use std::sync::Arc;
182182
/// # use vm_memory::{GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryMmap};
@@ -294,7 +294,7 @@ pub trait GuestMemory {
294294
/// `backend-mmap` feature)
295295
///
296296
/// ```
297-
/// # #[cfg(feature = "backend-mmap")]
297+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
298298
/// # {
299299
/// # use vm_memory::{GuestAddress, GuestMemory, GuestMemoryRegion, GuestMemoryMmap};
300300
/// #
@@ -318,7 +318,7 @@ pub trait GuestMemory {
318318
/// # Examples (uses the `backend-mmap` feature)
319319
///
320320
/// ```
321-
/// # #[cfg(feature = "backend-mmap")]
321+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
322322
/// # {
323323
/// # use vm_memory::{Address, GuestAddress, GuestMemory, GuestMemoryMmap};
324324
/// #
@@ -433,7 +433,7 @@ pub trait GuestMemory {
433433
/// # Examples (uses the `backend-mmap` feature)
434434
///
435435
/// ```
436-
/// # #[cfg(feature = "backend-mmap")]
436+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
437437
/// # {
438438
/// # use vm_memory::{GuestAddress, GuestMemory, GuestMemoryMmap};
439439
/// #
@@ -592,7 +592,7 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
592592
/// * Write a slice at guestaddress 0x1000. (uses the `backend-mmap` feature)
593593
///
594594
/// ```
595-
/// # #[cfg(feature = "backend-mmap")]
595+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
596596
/// # {
597597
/// # use vm_memory::{Bytes, GuestAddress, mmap::GuestMemoryMmap};
598598
/// #
@@ -620,7 +620,7 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
620620
/// * Read a slice of length 16 at guestaddress 0x1000. (uses the `backend-mmap` feature)
621621
///
622622
/// ```
623-
/// # #[cfg(feature = "backend-mmap")]
623+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
624624
/// # {
625625
/// # use vm_memory::{Bytes, GuestAddress, mmap::GuestMemoryMmap};
626626
/// #
@@ -727,13 +727,15 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
727727
mod tests {
728728
#![allow(clippy::undocumented_unsafe_blocks)]
729729

730-
use super::*;
730+
#[cfg(feature = "backend-mmap")]
731731
use std::time::{Duration, Instant};
732+
use super::*;
732733

733734
#[cfg(feature = "backend-mmap")]
734735
use crate::mmap::tests::AnyBackend;
735-
use crate::ByteValued;
736736
use vmm_sys_util::tempfile::TempFile;
737+
#[cfg(feature = "backend-mmap")]
738+
use crate::ByteValued;
737739

738740
#[test]
739741
fn test_file_offset() {

src/lib.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,22 @@ pub use io::{ReadVolatile, WriteVolatile};
6767
#[cfg(feature = "backend-mmap")]
6868
pub mod mmap;
6969

70-
#[cfg(feature = "backend-mmap")]
71-
pub use mmap::{GuestMemoryMmap, GuestRegionMmap, MmapRegion};
7270
#[cfg(all(feature = "backend-mmap", feature = "xen", target_family = "unix"))]
7371
pub use mmap::{MmapRange, MmapXenFlags};
7472

75-
#[cfg(all(feature = "xen", unix))]
73+
#[cfg(all(feature = "xen", target_family = "unix"))]
7674
pub use mmap::xen::{GuestMemoryXen, MmapRegion as MmapRegionXen};
7775

76+
#[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
77+
pub use mmap::unix::{Error as MmapRegionError, GuestMemoryMmap, GuestRegionMmap, MmapRegion};
78+
79+
#[cfg(windows)]
80+
pub use crate::mmap::windows::{
81+
GuestMemoryWindows as GuestMemoryMmap, GuestRegionWindows as GuestRegionMmap, MmapRegion,
82+
}; // rename for backwards compat
83+
#[cfg(windows)]
84+
pub use std::io::Error as MmapRegionError;
85+
7886
pub mod volatile_memory;
7987
pub use volatile_memory::{
8088
Error as VolatileMemoryError, Result as VolatileMemoryResult, VolatileArrayRef, VolatileMemory,

0 commit comments

Comments
 (0)