-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70a0ce2
commit 9c55d90
Showing
19 changed files
with
1,939 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
//! BlockRangeIter is an iterator for blocks within a specific range. | ||
//! BlockRange describes sub-range information about each block. | ||
use crate::prelude::*; | ||
|
||
/// Given a range and iterate sub-range for each block. | ||
#[derive(Debug)] | ||
pub struct BlockRangeIter { | ||
pub begin: usize, | ||
pub end: usize, | ||
pub block_size: usize, | ||
} | ||
|
||
/// Describe the range for one block. | ||
#[derive(Debug, Eq, PartialEq)] | ||
pub struct BlockRange { | ||
pub block_id: BlockId, | ||
pub begin: usize, | ||
pub end: usize, | ||
pub block_size: usize, | ||
} | ||
|
||
impl Iterator for BlockRangeIter { | ||
type Item = BlockRange; | ||
|
||
fn next(&mut self) -> Option<<Self as Iterator>::Item> { | ||
// Exit condition | ||
if self.begin >= self.end { | ||
return None; | ||
} | ||
|
||
// Construct sub-range of next block | ||
let sub_range = { | ||
let block_id = self.begin / self.block_size; | ||
let begin = self.begin % self.block_size; | ||
let end = if block_id == self.end / self.block_size { | ||
self.end % self.block_size | ||
} else { | ||
self.block_size | ||
}; | ||
let block_size = self.block_size; | ||
BlockRange { | ||
block_id, | ||
begin, | ||
end, | ||
block_size, | ||
} | ||
}; | ||
|
||
self.begin += sub_range.len(); | ||
Some(sub_range) | ||
} | ||
} | ||
|
||
impl BlockRange { | ||
/// Return block length. | ||
pub fn len(&self) -> usize { | ||
self.end - self.begin | ||
} | ||
|
||
/// Whether the range covers a whole block. | ||
pub fn is_full(&self) -> bool { | ||
self.len() == self.block_size | ||
} | ||
|
||
/// Whether the range is empty. | ||
pub fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
} | ||
|
||
/// Describe the begin offset of this block from the whole range. | ||
pub fn origin_begin(&self) -> usize { | ||
self.block_id * self.block_size + self.begin | ||
} | ||
|
||
/// Describe the end offset of this block from the whole range. | ||
pub fn origin_end(&self) -> usize { | ||
self.block_id * self.block_size + self.end | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn block_range_iter() { | ||
let mut iter = BlockRangeIter { | ||
begin: 0x125, | ||
end: 0x2025, | ||
block_size: BLOCK_SIZE, | ||
}; | ||
|
||
assert_eq!( | ||
iter.next(), | ||
Some(BlockRange { | ||
block_id: 0, | ||
begin: 0x125, | ||
end: 0x1000, | ||
block_size: BLOCK_SIZE, | ||
}) | ||
); | ||
|
||
assert_eq!( | ||
iter.next(), | ||
Some(BlockRange { | ||
block_id: 1, | ||
begin: 0, | ||
end: 0x1000, | ||
block_size: BLOCK_SIZE, | ||
}) | ||
); | ||
|
||
assert_eq!( | ||
iter.next(), | ||
Some(BlockRange { | ||
block_id: 2, | ||
begin: 0, | ||
end: 0x25, | ||
block_size: BLOCK_SIZE, | ||
}) | ||
); | ||
|
||
assert_eq!(iter.next(), None); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
[package] | ||
name = "page-cache" | ||
version = "0.1.0" | ||
authors = ["Song Shaowei <songshaowei.ssw@antgroup.com>"] | ||
edition = "2018" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[features] | ||
default = ["libc", "lru/default"] | ||
sgx = ["sgx_types", "sgx_tstd", "sgx_trts", "sgx_libc", "async-io/sgx", "async-rt/sgx", "lru/sgx"] | ||
|
||
[dependencies] | ||
async-trait = "0.1.52" | ||
lazy_static = { version = "1.4.0", features = ["spin_no_std"] } | ||
libc = { version = "0.2", optional = true } | ||
log = "0.4" | ||
lru = { path = "../../../../deps/lru-rs", default-features = false } | ||
spin = "0.7" | ||
|
||
async-io = { path = "../async-io" } | ||
async-rt = { path = "../async-rt" } | ||
block-device = { path = "../block-device" } | ||
errno = { path = "../errno" } | ||
object-id = { path = "../object-id" } | ||
|
||
sgx_types = { path = "../../../../deps/rust-sgx-sdk/sgx_types", optional = true } | ||
sgx_tstd = { path = "../../../../deps/rust-sgx-sdk/sgx_tstd", optional = true, features = ["backtrace"] } | ||
sgx_trts = { path = "../../../../deps/rust-sgx-sdk/sgx_trts", optional = true } | ||
sgx_libc = { path = "../../../../deps/rust-sgx-sdk/sgx_libc", optional = true } | ||
|
||
[dev-dependencies] | ||
async-rt = { path = "../async-rt", features = ["auto_run"] } | ||
|
||
[lib] | ||
doctest = false |
Oops, something went wrong.