Skip to content

Commit 25e5174

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 585bee7 commit 25e5174

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
@@ -330,7 +330,7 @@ pub trait Bytes<A> {
330330
/// * Read bytes from /dev/urandom (uses the `backend-mmap` feature)
331331
///
332332
/// ```
333-
/// # #[cfg(all(feature = "backend-mmap", feature = "rawfd"))]
333+
/// # #[cfg(all(feature = "backend-mmap", feature = "rawfd", target_family = "unix", not(feature = "xen")))]
334334
/// # {
335335
/// # use vm_memory::{Address, GuestMemory, Bytes, GuestAddress, GuestMemoryMmap};
336336
/// # use std::fs::File;

src/guest_memory.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl FileOffset {
174174
/// # Examples (uses the `backend-mmap` and `backend-atomic` features)
175175
///
176176
/// ```
177-
/// # #[cfg(feature = "backend-mmap")]
177+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
178178
/// # {
179179
/// # use std::sync::Arc;
180180
/// # use vm_memory::{GuestAddress, GuestAddressSpace, GuestMemory, GuestMemoryMmap};
@@ -292,7 +292,7 @@ pub trait GuestMemory {
292292
/// `backend-mmap` feature)
293293
///
294294
/// ```
295-
/// # #[cfg(feature = "backend-mmap")]
295+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
296296
/// # {
297297
/// # use vm_memory::{GuestAddress, GuestMemory, GuestMemoryRegion, GuestMemoryMmap};
298298
/// #
@@ -316,7 +316,7 @@ pub trait GuestMemory {
316316
/// # Examples (uses the `backend-mmap` feature)
317317
///
318318
/// ```
319-
/// # #[cfg(feature = "backend-mmap")]
319+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
320320
/// # {
321321
/// # use vm_memory::{Address, GuestAddress, GuestMemory, GuestMemoryMmap};
322322
/// #
@@ -428,7 +428,7 @@ pub trait GuestMemory {
428428
/// # Examples (uses the `backend-mmap` feature)
429429
///
430430
/// ```
431-
/// # #[cfg(feature = "backend-mmap")]
431+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
432432
/// # {
433433
/// # use vm_memory::{GuestAddress, GuestMemory, GuestMemoryMmap};
434434
/// #
@@ -485,7 +485,7 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
485485
/// * Write a slice at guestaddress 0x1000. (uses the `backend-mmap` feature)
486486
///
487487
/// ```
488-
/// # #[cfg(feature = "backend-mmap")]
488+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
489489
/// # {
490490
/// # use vm_memory::{Bytes, GuestAddress, mmap::GuestMemoryMmap};
491491
/// #
@@ -513,7 +513,7 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
513513
/// * Read a slice of length 16 at guestaddress 0x1000. (uses the `backend-mmap` feature)
514514
///
515515
/// ```
516-
/// # #[cfg(feature = "backend-mmap")]
516+
/// # #[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
517517
/// # {
518518
/// # use vm_memory::{Bytes, GuestAddress, mmap::GuestMemoryMmap};
519519
/// #
@@ -609,13 +609,15 @@ impl<T: GuestMemory + ?Sized> Bytes<GuestAddress> for T {
609609
mod tests {
610610
#![allow(clippy::undocumented_unsafe_blocks)]
611611

612-
use super::*;
612+
#[cfg(feature = "backend-mmap")]
613613
use std::time::{Duration, Instant};
614+
use super::*;
614615

615616
#[cfg(feature = "backend-mmap")]
616617
use crate::mmap::tests::AnyBackend;
617-
use crate::ByteValued;
618618
use vmm_sys_util::tempfile::TempFile;
619+
#[cfg(feature = "backend-mmap")]
620+
use crate::ByteValued;
619621

620622
#[test]
621623
fn test_file_offset() {

src/lib.rs

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

64-
#[cfg(feature = "backend-mmap")]
65-
pub use mmap::{GuestMemoryMmap, GuestRegionMmap, MmapRegion};
6664
#[cfg(all(feature = "backend-mmap", feature = "xen", target_family = "unix"))]
6765
pub use mmap::{MmapRange, MmapXenFlags};
6866

69-
#[cfg(all(feature = "xen", unix))]
67+
#[cfg(all(feature = "xen", target_family = "unix"))]
7068
pub use mmap::xen::{GuestMemoryXen, MmapRegion as MmapRegionXen};
7169

70+
#[cfg(all(feature = "backend-mmap", target_family = "unix", not(feature = "xen")))]
71+
pub use mmap::unix::{Error as MmapRegionError, GuestMemoryMmap, GuestRegionMmap, MmapRegion};
72+
73+
#[cfg(windows)]
74+
pub use crate::mmap::windows::{
75+
GuestMemoryWindows as GuestMemoryMmap, GuestRegionWindows as GuestRegionMmap, MmapRegion,
76+
}; // rename for backwards compat
77+
#[cfg(windows)]
78+
pub use std::io::Error as MmapRegionError;
79+
7280
pub mod volatile_memory;
7381
pub use volatile_memory::{
7482
Error as VolatileMemoryError, Result as VolatileMemoryResult, VolatileArrayRef, VolatileMemory,

0 commit comments

Comments
 (0)