#[macro_use]
extern crate log;
#[macro_use]
extern crate serde;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate nydus_api;
use std::convert::{Into, TryFrom, TryInto};
use std::time::Duration;
pub use self::exec::*;
pub use self::inode_bitmap::InodeBitmap;
pub use self::reader::*;
pub use self::types::*;
pub mod async_helper;
pub mod compact;
pub mod compress;
#[cfg(feature = "encryption")]
pub mod crypt;
pub mod digest;
pub mod exec;
pub mod filemap;
pub mod inode_bitmap;
pub mod logger;
pub mod metrics;
pub mod mpmc;
pub mod reader;
pub mod trace;
pub mod types;
pub mod verity;
pub fn div_round_up(n: u64, d: u64) -> u64 {
debug_assert!(d != 0);
debug_assert!(d.is_power_of_two());
(n + d - 1) / d
}
pub fn round_up(n: u64, d: u64) -> u64 {
debug_assert!(d != 0);
debug_assert!(d.is_power_of_two());
(n + d - 1) / d * d
}
pub fn round_up_usize(n: usize, d: usize) -> usize {
debug_assert!(d != 0);
debug_assert!(d.is_power_of_two());
(n + d - 1) / d * d
}
pub fn try_round_up_4k<U: TryFrom<u64>, T: Into<u64>>(x: T) -> Option<U> {
let t = 4095u64;
if let Some(v) = x.into().checked_add(t) {
let z = v & (!t);
z.try_into().ok()
} else {
None
}
}
pub fn round_down_4k(x: u64) -> u64 {
x & (!4095u64)
}
pub fn round_down(n: u64, d: u64) -> u64 {
debug_assert!(d != 0);
debug_assert!(d.is_power_of_two());
n / d * d
}
pub enum DelayType {
Fixed,
BackOff,
}
pub struct Delayer {
r#type: DelayType,
attempts: u32,
time: Duration,
}
impl Delayer {
pub fn new(t: DelayType, time: Duration) -> Self {
Delayer {
r#type: t,
attempts: 0,
time,
}
}
pub fn delay(&mut self) {
use std::thread::sleep;
match self.r#type {
DelayType::Fixed => sleep(self.time),
DelayType::BackOff => sleep((1 << self.attempts) * self.time),
}
self.attempts += 1;
}
}
struct LazyDrop<T> {
v: T,
}
unsafe impl<T> Send for LazyDrop<T> {}
pub fn lazy_drop<T: 'static>(v: T) {
let v = LazyDrop { v };
std::thread::spawn(move || {
std::thread::sleep(Duration::from_secs(600));
let _ = v.v;
});
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_rounders() {
assert_eq!(round_down_4k(0), 0);
assert_eq!(round_down_4k(100), 0);
assert_eq!(round_down_4k(4300), 4096);
assert_eq!(round_down_4k(4096), 4096);
assert_eq!(round_down_4k(4095), 0);
assert_eq!(round_down_4k(4097), 4096);
assert_eq!(round_down_4k(u64::MAX - 1), u64::MAX - 4095);
assert_eq!(round_down_4k(u64::MAX - 4095), u64::MAX - 4095);
assert_eq!(try_round_up_4k::<i32, _>(0u32), Some(0i32));
assert_eq!(try_round_up_4k::<u32, _>(0u32), Some(0u32));
assert_eq!(try_round_up_4k::<u32, _>(1u32), Some(4096u32));
assert_eq!(try_round_up_4k::<u32, _>(100u32), Some(4096u32));
assert_eq!(try_round_up_4k::<u32, _>(4100u32), Some(8192u32));
assert_eq!(try_round_up_4k::<u32, _>(4096u32), Some(4096u32));
assert_eq!(try_round_up_4k::<u32, _>(4095u32), Some(4096u32));
assert_eq!(try_round_up_4k::<u32, _>(4097u32), Some(8192u32));
assert_eq!(try_round_up_4k::<u32, _>(u32::MAX), None);
assert_eq!(try_round_up_4k::<u64, _>(u32::MAX), Some(0x1_0000_0000u64));
assert_eq!(try_round_up_4k::<u32, _>(u64::MAX - 1), None);
assert_eq!(try_round_up_4k::<u32, _>(u64::MAX), None);
assert_eq!(try_round_up_4k::<u32, _>(u64::MAX - 4097), None);
assert_eq!(
try_round_up_4k::<u64, _>(u64::MAX - 4096),
Some(u64::MAX - 4095)
);
assert_eq!(try_round_up_4k::<u64, _>(u64::MAX - 1), None);
assert_eq!(try_round_up_4k::<u32, _>(u64::MAX - 4096), None);
}
}