forked from rust-vmm/vm-memory
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
`AtomicBitmapArc` wraps an `AtomicBitmap` instance to provide a `Bitmap` implementation that uses slices which are based on `Arc` instead of references with their associated lifetimes. Signed-off-by: Alexandru Agache <aagch@amazon.com>
- Loading branch information
1 parent
1d15af0
commit 183cdbc
Showing
6 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"coverage_score": 88.5, | ||
"coverage_score": 87.6, | ||
"exclude_path": "mmap_windows.rs", | ||
"crate_features": "backend-mmap,backend-atomic,backend-bitmap" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 OR BSD-3-Clause | ||
|
||
use std::ops::Deref; | ||
use std::sync::Arc; | ||
|
||
use crate::bitmap::{ArcSlice, AtomicBitmap, Bitmap, WithBitmapSlice}; | ||
|
||
#[cfg(feature = "backend-mmap")] | ||
use crate::mmap::NewBitmap; | ||
|
||
/// A `Bitmap` implementation that's based on an atomically reference counted handle to an | ||
/// `AtomicBitmap` object. | ||
pub struct AtomicBitmapArc { | ||
inner: Arc<AtomicBitmap>, | ||
} | ||
|
||
impl AtomicBitmapArc { | ||
pub fn new(inner: AtomicBitmap) -> Self { | ||
AtomicBitmapArc { | ||
inner: Arc::new(inner), | ||
} | ||
} | ||
} | ||
|
||
// The current clone implementation creates a deep clone of the inner bitmap, as opposed to | ||
// simply cloning the `Arc`. | ||
impl Clone for AtomicBitmapArc { | ||
fn clone(&self) -> Self { | ||
Self::new(self.inner.deref().clone()) | ||
} | ||
} | ||
|
||
// Providing a `Deref` to `AtomicBitmap` implementation, so the methods of the inner object | ||
// can be called in a transparent manner. | ||
impl Deref for AtomicBitmapArc { | ||
type Target = AtomicBitmap; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.inner.deref() | ||
} | ||
} | ||
|
||
impl WithBitmapSlice<'_> for AtomicBitmapArc { | ||
type S = ArcSlice<AtomicBitmap>; | ||
} | ||
|
||
impl Bitmap for AtomicBitmapArc { | ||
fn mark_dirty(&self, offset: usize, len: usize) { | ||
self.inner.set_addr_range(offset, len) | ||
} | ||
|
||
fn dirty_at(&self, offset: usize) -> bool { | ||
self.inner.is_addr_set(offset) | ||
} | ||
|
||
fn slice_at(&self, offset: usize) -> <Self as WithBitmapSlice>::S { | ||
ArcSlice::new(self.inner.clone(), offset) | ||
} | ||
} | ||
|
||
impl Default for AtomicBitmapArc { | ||
fn default() -> Self { | ||
Self::new(AtomicBitmap::default()) | ||
} | ||
} | ||
|
||
#[cfg(feature = "backend-mmap")] | ||
impl NewBitmap for AtomicBitmapArc { | ||
fn with_len(len: usize) -> Self { | ||
Self::new(AtomicBitmap::with_len(len)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
use crate::bitmap::tests::test_bitmap; | ||
|
||
#[test] | ||
fn test_bitmap_impl() { | ||
let b = AtomicBitmapArc::new(AtomicBitmap::new(0x2000, 128)); | ||
test_bitmap(&b); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters