misc: Replace div_round_up operation with div_ceil

As clippy of rust-toolchain version 1.83.0-beta.1 suggests, replace
manually implemented `div_round_up!` and the like with `div_ceil` from
std.

Signed-off-by: Ruoqing He <heruoqing@iscas.ac.cn>
This commit is contained in:
Ruoqing He
2024-10-18 17:22:20 +08:00
committed by Rob Bradford
parent 67fb786c54
commit 6164aa0885
13 changed files with 16 additions and 31 deletions
+1 -1
View File
@@ -74,7 +74,7 @@ impl RawFile {
fn round_up(&self, offset: u64) -> u64 { fn round_up(&self, offset: u64) -> u64 {
let align: u64 = self.alignment.try_into().unwrap(); let align: u64 = self.alignment.try_into().unwrap();
((offset + align - 1) / align) * align offset.div_ceil(align) * align
} }
fn round_down(&self, offset: u64) -> u64 { fn round_down(&self, offset: u64) -> u64 {
+2 -10
View File
@@ -17,12 +17,6 @@ use crate::vhdx::vhdx_io::VhdxIoError;
use crate::vhdx::vhdx_metadata::{DiskSpec, VhdxMetadataError}; use crate::vhdx::vhdx_metadata::{DiskSpec, VhdxMetadataError};
use crate::BlockBackend; use crate::BlockBackend;
macro_rules! div_round_up {
($n:expr,$d:expr) => {
($n + $d - 1) / $d
};
}
mod vhdx_bat; mod vhdx_bat;
mod vhdx_header; mod vhdx_header;
mod vhdx_io; mod vhdx_io;
@@ -105,8 +99,7 @@ impl Read for Vhdx {
/// Wrapper function to satisfy Read trait implementation for VHDx disk. /// Wrapper function to satisfy Read trait implementation for VHDx disk.
/// Convert the offset to sector index and buffer length to sector count. /// Convert the offset to sector index and buffer length to sector count.
fn read(&mut self, buf: &mut [u8]) -> std::result::Result<usize, std::io::Error> { fn read(&mut self, buf: &mut [u8]) -> std::result::Result<usize, std::io::Error> {
let sector_count = let sector_count = (buf.len() as u64).div_ceil(self.disk_spec.logical_sector_size as u64);
div_round_up!(buf.len() as u64, self.disk_spec.logical_sector_size as u64);
let sector_index = self.current_offset / self.disk_spec.logical_sector_size as u64; let sector_index = self.current_offset / self.disk_spec.logical_sector_size as u64;
vhdx_io::read( vhdx_io::read(
@@ -136,8 +129,7 @@ impl Write for Vhdx {
/// Wrapper function to satisfy Write trait implementation for VHDx disk. /// Wrapper function to satisfy Write trait implementation for VHDx disk.
/// Convert the offset to sector index and buffer length to sector count. /// Convert the offset to sector index and buffer length to sector count.
fn write(&mut self, buf: &[u8]) -> std::result::Result<usize, std::io::Error> { fn write(&mut self, buf: &[u8]) -> std::result::Result<usize, std::io::Error> {
let sector_count = let sector_count = (buf.len() as u64).div_ceil(self.disk_spec.logical_sector_size as u64);
div_round_up!(buf.len() as u64, self.disk_spec.logical_sector_size as u64);
let sector_index = self.current_offset / self.disk_spec.logical_sector_size as u64; let sector_index = self.current_offset / self.disk_spec.logical_sector_size as u64;
if self.first_write { if self.first_write {
+1 -1
View File
@@ -78,7 +78,7 @@ impl BatEntry {
// Calculate the number of entries in the BAT // Calculate the number of entries in the BAT
fn calculate_entries(block_size: u32, virtual_disk_size: u64, chunk_ratio: u64) -> u64 { fn calculate_entries(block_size: u32, virtual_disk_size: u64, chunk_ratio: u64) -> u64 {
let data_blocks_count = div_round_up!(virtual_disk_size, block_size as u64); let data_blocks_count = virtual_disk_size.div_ceil(block_size as u64);
data_blocks_count + (data_blocks_count - 1) / chunk_ratio data_blocks_count + (data_blocks_count - 1) / chunk_ratio
} }
+1 -1
View File
@@ -36,7 +36,7 @@ pub type Result<T> = std::result::Result<T, VhdxIoError>;
macro_rules! align { macro_rules! align {
($n:expr, $align:expr) => {{ ($n:expr, $align:expr) => {{
(($n + $align - 1) / $align) * $align $n.div_ceil($align) * $align
}}; }};
} }
+1 -1
View File
@@ -120,7 +120,7 @@ impl VirtioInterrupt for NoopVirtioInterrupt {
macro_rules! align { macro_rules! align {
($n:expr, $align:expr) => {{ ($n:expr, $align:expr) => {{
(($n + $align - 1) / $align) * $align $n.div_ceil($align) * $align
}}; }};
} }
+1 -1
View File
@@ -21,7 +21,7 @@ type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>;
macro_rules! align { macro_rules! align {
($n:expr, $align:expr) => {{ ($n:expr, $align:expr) => {{
(($n + $align - 1) / $align) * $align $n.div_ceil($align) * $align
}}; }};
} }
+1 -1
View File
@@ -19,7 +19,7 @@ type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>;
macro_rules! align { macro_rules! align {
($n:expr, $align:expr) => {{ ($n:expr, $align:expr) => {{
(($n + $align - 1) / $align) * $align $n.div_ceil($align) * $align
}}; }};
} }
+1 -1
View File
@@ -20,7 +20,7 @@ type GuestRegionMmap = vm_memory::GuestRegionMmap<AtomicBitmap>;
macro_rules! align { macro_rules! align {
($n:expr, $align:expr) => {{ ($n:expr, $align:expr) => {{
(($n + $align - 1) / $align) * $align $n.div_ceil($align) * $align
}}; }};
} }
+1 -1
View File
@@ -22,7 +22,7 @@ type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>;
macro_rules! align { macro_rules! align {
($n:expr, $align:expr) => {{ ($n:expr, $align:expr) => {{
(($n + $align - 1) / $align) * $align $n.div_ceil($align) * $align
}}; }};
} }
+1 -1
View File
@@ -19,7 +19,7 @@ type GuestMemoryMmap = vm_memory::GuestMemoryMmap<AtomicBitmap>;
macro_rules! align { macro_rules! align {
($n:expr, $align:expr) => {{ ($n:expr, $align:expr) => {{
(($n + $align - 1) / $align) * $align $n.div_ceil($align) * $align
}}; }};
} }
+1 -1
View File
@@ -90,7 +90,7 @@ pub fn new() -> std::result::Result<Arc<dyn Hypervisor>, HypervisorError> {
// Returns a `Vec<T>` with a size in bytes at least as large as `size_in_bytes`. // Returns a `Vec<T>` with a size in bytes at least as large as `size_in_bytes`.
fn vec_with_size_in_bytes<T: Default>(size_in_bytes: usize) -> Vec<T> { fn vec_with_size_in_bytes<T: Default>(size_in_bytes: usize) -> Vec<T> {
let rounded_size = (size_in_bytes + size_of::<T>() - 1) / size_of::<T>(); let rounded_size = size_in_bytes.div_ceil(size_of::<T>());
let mut v = Vec::with_capacity(rounded_size); let mut v = Vec::with_capacity(rounded_size);
v.resize_with(rounded_size, T::default); v.resize_with(rounded_size, T::default);
v v
+1 -8
View File
@@ -50,13 +50,6 @@ pub trait GuestDebuggable: vm_migration::Pausable {
} }
} }
#[macro_export]
macro_rules! div_round_up {
($n:expr,$d:expr) => {
($n + $d - 1) / $d
};
}
#[repr(C)] #[repr(C)]
#[derive(Default, Copy, Clone)] #[derive(Default, Copy, Clone)]
pub struct X86_64UserRegs { pub struct X86_64UserRegs {
@@ -314,7 +307,7 @@ pub trait Elf64Writable {
} }
fn elf_note_size(&self, hdr_size: u32, name_size: u32, desc_size: u32) -> u32 { fn elf_note_size(&self, hdr_size: u32, name_size: u32, desc_size: u32) -> u32 {
(div_round_up!(hdr_size, 4) + div_round_up!(name_size, 4) + div_round_up!(desc_size, 4)) * 4 (hdr_size.div_ceil(4) + name_size.div_ceil(4) + desc_size.div_ceil(4)) * 4
} }
fn get_note_size(&self, desc_type: NoteDescType, nr_cpus: u32) -> u32 { fn get_note_size(&self, desc_type: NoteDescType, nr_cpus: u32) -> u32 {
+3 -3
View File
@@ -1088,9 +1088,9 @@ impl MemoryManager {
} else { } else {
// Alignment must be "natural" i.e. same as size of block // Alignment must be "natural" i.e. same as size of block
let start_addr = GuestAddress( let start_addr = GuestAddress(
(start_of_device_area.0 + virtio_devices::VIRTIO_MEM_ALIGN_SIZE start_of_device_area
- 1) .0
/ virtio_devices::VIRTIO_MEM_ALIGN_SIZE .div_ceil(virtio_devices::VIRTIO_MEM_ALIGN_SIZE)
* virtio_devices::VIRTIO_MEM_ALIGN_SIZE, * virtio_devices::VIRTIO_MEM_ALIGN_SIZE,
); );