Skip to content

Commit 3f2fd80

Browse files
committed
refactor: Move NewBitmap trait to bitmap module
There is really no reason (at least that I can see) for this to be defined in `mmap.rs`, and for it to be gated behind the backend-mmap feature. While we're at it, add a comment explaining why this cannot be part of the normal `Bitmap` trait. Add a public reexport from mmap.rs anyway for backwards compat reasons. No functional change. Signed-off-by: Patrick Roy <roypat@amazon.co.uk>
1 parent d631e1b commit 3f2fd80

File tree

7 files changed

+21
-26
lines changed

7 files changed

+21
-26
lines changed

src/bitmap/backend/atomic_bitmap.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
use std::num::NonZeroUsize;
77
use std::sync::atomic::{AtomicU64, Ordering};
88

9-
use crate::bitmap::{Bitmap, RefSlice, WithBitmapSlice};
10-
11-
#[cfg(feature = "backend-mmap")]
12-
use crate::mmap::NewBitmap;
9+
use crate::bitmap::{Bitmap, NewBitmap, RefSlice, WithBitmapSlice};
1310

1411
/// `AtomicBitmap` implements a simple bit map on the page level with test and set operations.
1512
/// It is page-size aware, so it converts addresses to page numbers before setting or clearing
@@ -191,7 +188,6 @@ impl Default for AtomicBitmap {
191188
}
192189
}
193190

194-
#[cfg(feature = "backend-mmap")]
195191
impl NewBitmap for AtomicBitmap {
196192
fn with_len(len: usize) -> Self {
197193
#[cfg(target_family = "unix")]

src/bitmap/backend/atomic_bitmap_arc.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44
use std::ops::Deref;
55
use std::sync::Arc;
66

7-
use crate::bitmap::{ArcSlice, AtomicBitmap, Bitmap, WithBitmapSlice};
8-
9-
#[cfg(feature = "backend-mmap")]
10-
use crate::mmap::NewBitmap;
7+
use crate::bitmap::{ArcSlice, AtomicBitmap, Bitmap, NewBitmap, WithBitmapSlice};
118

129
/// A `Bitmap` implementation that's based on an atomically reference counted handle to an
1310
/// `AtomicBitmap` object.
@@ -65,7 +62,6 @@ impl Default for AtomicBitmapArc {
6562
}
6663
}
6764

68-
#[cfg(feature = "backend-mmap")]
6965
impl NewBitmap for AtomicBitmapArc {
7066
fn with_len(len: usize) -> Self {
7167
Self::new(AtomicBitmap::with_len(len))

src/bitmap/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ pub trait Bitmap: for<'a> WithBitmapSlice<'a> {
4747
fn slice_at(&self, offset: usize) -> <Self as WithBitmapSlice>::S;
4848
}
4949

50+
/// A `Bitmap` that can be created starting from an initial size.
51+
// Cannot be a part of the Bitmap trait itself because it cannot be implemented for BaseSlice
52+
pub trait NewBitmap: Bitmap + Default {
53+
/// Create a new object based on the specified length in bytes.
54+
fn with_len(len: usize) -> Self;
55+
}
56+
5057
/// A no-op `Bitmap` implementation that can be provided for backends that do not actually
5158
/// require the tracking functionality.
5259
impl WithBitmapSlice<'_> for () {
@@ -65,6 +72,10 @@ impl Bitmap for () {
6572
fn slice_at(&self, _offset: usize) -> Self {}
6673
}
6774

75+
impl NewBitmap for () {
76+
fn with_len(_len: usize) -> Self {}
77+
}
78+
6879
/// A `Bitmap` and `BitmapSlice` implementation for `Option<B>`.
6980
impl<'a, B> WithBitmapSlice<'a> for Option<B>
7081
where

src/mmap/mod.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ use crate::guest_memory::{
2828
use crate::volatile_memory::{VolatileMemory, VolatileSlice};
2929
use crate::{AtomicAccess, Bytes, ReadVolatile, WriteVolatile};
3030

31+
// re-export for backward compat, as the trait used to be defined in mmap.rs
32+
pub use crate::bitmap::NewBitmap;
33+
3134
#[cfg(all(not(feature = "xen"), target_family = "unix"))]
3235
mod unix;
3336

@@ -48,16 +51,6 @@ pub use std::io::Error as MmapRegionError;
4851
#[cfg(target_family = "windows")]
4952
pub use windows::MmapRegion;
5053

51-
/// A `Bitmap` that can be created starting from an initial size.
52-
pub trait NewBitmap: Bitmap + Default {
53-
/// Create a new object based on the specified length in bytes.
54-
fn with_len(len: usize) -> Self;
55-
}
56-
57-
impl NewBitmap for () {
58-
fn with_len(_len: usize) -> Self {}
59-
}
60-
6154
/// Errors that can occur when creating a memory map.
6255
#[derive(Debug, thiserror::Error)]
6356
pub enum Error {

src/mmap/unix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use std::os::unix::io::AsRawFd;
1515
use std::ptr::null_mut;
1616
use std::result;
1717

18-
use crate::bitmap::{Bitmap, BS};
18+
use crate::bitmap::{Bitmap, NewBitmap, BS};
1919
use crate::guest_memory::FileOffset;
20-
use crate::mmap::{check_file_offset, NewBitmap};
20+
use crate::mmap::check_file_offset;
2121
use crate::volatile_memory::{self, VolatileMemory, VolatileSlice};
2222

2323
/// Error conditions that may arise when creating a new `MmapRegion` object.

src/mmap/windows.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,8 @@ use libc::{c_void, size_t};
1212

1313
use winapi::um::errhandlingapi::GetLastError;
1414

15-
use crate::bitmap::{Bitmap, BS};
15+
use crate::bitmap::{Bitmap, NewBitmap, BS};
1616
use crate::guest_memory::FileOffset;
17-
use crate::mmap::NewBitmap;
1817
use crate::volatile_memory::{self, compute_offset, VolatileMemory, VolatileSlice};
1918

2019
#[allow(non_snake_case)]

src/mmap/xen.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ use vmm_sys_util::ioctl::ioctl_with_ref;
2424
#[cfg(test)]
2525
use tests::ioctl_with_ref;
2626

27-
use crate::bitmap::{Bitmap, BS};
27+
use crate::bitmap::{Bitmap, NewBitmap, BS};
2828
use crate::guest_memory::{FileOffset, GuestAddress};
29-
use crate::mmap::{check_file_offset, NewBitmap};
29+
use crate::mmap::check_file_offset;
3030
use crate::volatile_memory::{self, VolatileMemory, VolatileSlice};
3131

3232
/// Error conditions that may arise when creating a new `MmapRegion` object.

0 commit comments

Comments
 (0)