use nix::errno::Errno;
use std::ops::{Add, Mul, Sub};
#[derive(Debug, Copy, Clone, PartialEq, Default, PartialOrd, Eq)]
#[repr(transparent)]
pub struct Block(pub u32);
pub fn div_rounded_up(a: u64, b: u64) -> u64 {
(a + b - 1) / b
}
impl Add<Self> for Block {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
Self(self.0 + rhs.0)
}
}
impl Sub<Self> for Block {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
Self(self.0 - rhs.0)
}
}
impl Mul<u32> for Block {
type Output = Self;
fn mul(self, rhs: u32) -> Self::Output {
Self(self.0 * rhs)
}
}
pub fn err_if_zero(x: Block) -> Result<Block, Errno> {
if x == Block(0) {
Err(Errno::EBADF)
} else {
Ok(x)
}
}
#[inline(always)]
pub fn align_next(t: u64, on: u64) -> u64 {
debug_assert!(on.is_power_of_two());
if t & (on - 1) == 0 {
t
} else {
t + (on - (t & (on - 1)))
}
}
#[inline(always)]
pub fn u32_align_next(t: u32, on: u32) -> u32 {
debug_assert!(on.is_power_of_two());
if t & (on - 1) == 0 {
t
} else {
t + (on - (t & (on - 1)))
}
}