mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
misc: use prelude size_of
size_of is part of std::prelude as of Rust 1.80 (with size_of_val, align_of, align_of_val), and the workspace MSRV is 1.89, so qualifying it (mem::size_of, std::mem::size_of, core::mem::size_of) is unnecessary. Convert every qualified size_of call-site to the bare prelude form and drop the now-redundant `use std::mem::size_of;` imports, keeping `use std::mem;` where it still serves non-prelude items (transmute, swap, replace, take, zeroed, MaybeUninit, offset_of). size_of is the only one of the four currently used in the tree. Pure refactor, no behavioural change. Follow-up to the clippy::absolute_paths cleanup (#7670), as discussed in #8444. Signed-off-by: Henry Hrvoje Tonkovac <htonkovac@gmail.com> Assisted-by: Claude:Opus-4.8
This commit is contained in:
committed by
Bo Chen
parent
f56fa3a865
commit
f720e619c1
@@ -3,7 +3,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
use std::any::Any;
|
||||
use std::{mem, result};
|
||||
use std::result;
|
||||
|
||||
use serde::de::Error as SerdeError;
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -67,7 +67,7 @@ impl<'de> Deserialize<'de> for GicState {
|
||||
}
|
||||
|
||||
const {
|
||||
assert!(mem::size_of::<GicStateDefaultDeserialize>() == mem::size_of::<GicState>());
|
||||
assert!(size_of::<GicStateDefaultDeserialize>() == size_of::<GicState>());
|
||||
};
|
||||
|
||||
let value: serde_json::Value = Deserialize::deserialize(deserializer)?;
|
||||
|
||||
@@ -54,12 +54,12 @@ macro_rules! cmp_rm_r {
|
||||
state: &mut T,
|
||||
platform: &mut dyn PlatformEmulator<CpuState = T>,
|
||||
) -> Result<(), EmulationError<Exception>> {
|
||||
let op0_value = get_op(&insn, 0, std::mem::size_of::<$bound>(), state, platform)
|
||||
let op0_value = get_op(&insn, 0, size_of::<$bound>(), state, platform)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
let op1_value = get_op(&insn, 1, std::mem::size_of::<$bound>(), state, platform)
|
||||
let op1_value = get_op(&insn, 1, size_of::<$bound>(), state, platform)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
|
||||
let cpazso = calc_rflags_cpazso(op0_value, op1_value, std::mem::size_of::<$bound>());
|
||||
let cpazso = calc_rflags_cpazso(op0_value, op1_value, size_of::<$bound>());
|
||||
|
||||
state.set_flags((state.flags() & !FLAGS_MASK) | cpazso);
|
||||
|
||||
@@ -76,12 +76,12 @@ macro_rules! cmp_r_rm {
|
||||
state: &mut T,
|
||||
platform: &mut dyn PlatformEmulator<CpuState = T>,
|
||||
) -> Result<(), EmulationError<Exception>> {
|
||||
let op0_value = get_op(&insn, 0, std::mem::size_of::<$bound>(), state, platform)
|
||||
let op0_value = get_op(&insn, 0, size_of::<$bound>(), state, platform)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
let op1_value = get_op(&insn, 1, std::mem::size_of::<$bound>(), state, platform)
|
||||
let op1_value = get_op(&insn, 1, size_of::<$bound>(), state, platform)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
|
||||
let cpazso = calc_rflags_cpazso(op0_value, op1_value, std::mem::size_of::<$bound>());
|
||||
let cpazso = calc_rflags_cpazso(op0_value, op1_value, size_of::<$bound>());
|
||||
|
||||
state.set_flags((state.flags() & !FLAGS_MASK) | cpazso);
|
||||
|
||||
@@ -98,12 +98,12 @@ macro_rules! cmp_rm_imm {
|
||||
state: &mut T,
|
||||
platform: &mut dyn PlatformEmulator<CpuState = T>,
|
||||
) -> Result<(), EmulationError<Exception>> {
|
||||
let op0_value = get_op(&insn, 0, std::mem::size_of::<$bound>(), state, platform)
|
||||
let op0_value = get_op(&insn, 0, size_of::<$bound>(), state, platform)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
let op1_value = get_op(&insn, 1, std::mem::size_of::<$imm>(), state, platform)
|
||||
let op1_value = get_op(&insn, 1, size_of::<$imm>(), state, platform)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
|
||||
let cpazso = calc_rflags_cpazso(op0_value, op1_value, std::mem::size_of::<$bound>());
|
||||
let cpazso = calc_rflags_cpazso(op0_value, op1_value, size_of::<$bound>());
|
||||
|
||||
state.set_flags((state.flags() & !FLAGS_MASK) | cpazso);
|
||||
|
||||
|
||||
@@ -22,13 +22,13 @@ macro_rules! mov_rm_r {
|
||||
state: &mut T,
|
||||
platform: &mut dyn PlatformEmulator<CpuState = T>,
|
||||
) -> Result<(), EmulationError<Exception>> {
|
||||
let src_reg_value = get_op(&insn, 1, std::mem::size_of::<$bound>(), state, platform)
|
||||
let src_reg_value = get_op(&insn, 1, size_of::<$bound>(), state, platform)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
|
||||
set_op(
|
||||
&insn,
|
||||
0,
|
||||
std::mem::size_of::<$bound>(),
|
||||
size_of::<$bound>(),
|
||||
state,
|
||||
platform,
|
||||
src_reg_value,
|
||||
@@ -48,18 +48,11 @@ macro_rules! mov_rm_imm {
|
||||
state: &mut T,
|
||||
platform: &mut dyn PlatformEmulator<CpuState = T>,
|
||||
) -> Result<(), EmulationError<Exception>> {
|
||||
let imm = get_op(&insn, 1, std::mem::size_of::<$bound>(), state, platform)
|
||||
let imm = get_op(&insn, 1, size_of::<$bound>(), state, platform)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
|
||||
set_op(
|
||||
&insn,
|
||||
0,
|
||||
std::mem::size_of::<$bound>(),
|
||||
state,
|
||||
platform,
|
||||
imm,
|
||||
)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
set_op(&insn, 0, size_of::<$bound>(), state, platform, imm)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -74,19 +67,13 @@ macro_rules! movzx {
|
||||
state: &mut T,
|
||||
platform: &mut dyn PlatformEmulator<CpuState = T>,
|
||||
) -> Result<(), EmulationError<Exception>> {
|
||||
let src_value = get_op(
|
||||
&insn,
|
||||
1,
|
||||
std::mem::size_of::<$src_op_size>(),
|
||||
state,
|
||||
platform,
|
||||
)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
let src_value = get_op(&insn, 1, size_of::<$src_op_size>(), state, platform)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
|
||||
set_op(
|
||||
&insn,
|
||||
0,
|
||||
std::mem::size_of::<$dest_op_size>(),
|
||||
size_of::<$dest_op_size>(),
|
||||
state,
|
||||
platform,
|
||||
src_value,
|
||||
@@ -113,18 +100,11 @@ macro_rules! mov_r_imm {
|
||||
state: &mut T,
|
||||
platform: &mut dyn PlatformEmulator<CpuState = T>,
|
||||
) -> Result<(), EmulationError<Exception>> {
|
||||
let imm = get_op(&insn, 1, std::mem::size_of::<$bound>(), state, platform)
|
||||
let imm = get_op(&insn, 1, size_of::<$bound>(), state, platform)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
|
||||
set_op(
|
||||
&insn,
|
||||
0,
|
||||
std::mem::size_of::<$bound>(),
|
||||
state,
|
||||
platform,
|
||||
imm,
|
||||
)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
set_op(&insn, 0, size_of::<$bound>(), state, platform, imm)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ macro_rules! movs {
|
||||
.map_err(|e| EmulationError::InvalidOperand(anyhow!(e)))?;
|
||||
|
||||
let backwards = string_op_backwards(state.flags());
|
||||
let len = std::mem::size_of::<$bound>();
|
||||
let len = size_of::<$bound>();
|
||||
|
||||
while count > 0 {
|
||||
let mut memory: [u8; 8] = [0; 8];
|
||||
|
||||
@@ -20,23 +20,16 @@ macro_rules! or_rm_r {
|
||||
state: &mut T,
|
||||
platform: &mut dyn PlatformEmulator<CpuState = T>,
|
||||
) -> Result<(), EmulationError<Exception>> {
|
||||
let src_reg_value = get_op(&insn, 1, std::mem::size_of::<$bound>(), state, platform)
|
||||
let src_reg_value = get_op(&insn, 1, size_of::<$bound>(), state, platform)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
|
||||
let dst_value = get_op(&insn, 0, std::mem::size_of::<$bound>(), state, platform)
|
||||
let dst_value = get_op(&insn, 0, size_of::<$bound>(), state, platform)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
|
||||
let result = src_reg_value | dst_value;
|
||||
|
||||
set_op(
|
||||
&insn,
|
||||
0,
|
||||
std::mem::size_of::<$bound>(),
|
||||
state,
|
||||
platform,
|
||||
result,
|
||||
)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
set_op(&insn, 0, size_of::<$bound>(), state, platform, result)
|
||||
.map_err(EmulationError::PlatformEmulationError)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ macro_rules! stos {
|
||||
.map_err(|e| EmulationError::InvalidOperand(anyhow!(e)))?;
|
||||
|
||||
let backwards = string_op_backwards(state.flags());
|
||||
let len = std::mem::size_of::<$bound>();
|
||||
let len = size_of::<$bound>();
|
||||
let rax_bytes = rax.to_le_bytes();
|
||||
|
||||
while count > 0 {
|
||||
|
||||
@@ -53,7 +53,7 @@ macro_rules! arm64_core_reg_id {
|
||||
KVM_REG_ARM64 as u64
|
||||
| u64::from(KVM_REG_ARM_CORE)
|
||||
| $size
|
||||
| (($offset / mem::size_of::<u32>()) as u64)
|
||||
| (($offset / size_of::<u32>()) as u64)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, RwLock};
|
||||
#[cfg(target_arch = "aarch64")]
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
use std::{fs, io, mem, result};
|
||||
use std::{fs, io, result};
|
||||
|
||||
#[cfg(target_arch = "x86_64")]
|
||||
use anyhow::Context;
|
||||
@@ -1108,7 +1108,7 @@ impl vm::Vm for KvmVm {
|
||||
flags |= KVM_MEM_LOG_DIRTY_PAGES;
|
||||
}
|
||||
|
||||
const _: () = assert!(mem::size_of::<usize>() <= mem::size_of::<u64>());
|
||||
const _: () = assert!(size_of::<usize>() <= size_of::<u64>());
|
||||
|
||||
// Create a per-region guest_memfd when supported.
|
||||
// Each region gets its own fd sized exactly to memory_size
|
||||
@@ -1222,7 +1222,7 @@ impl vm::Vm for KvmVm {
|
||||
flags |= KVM_MEM_LOG_DIRTY_PAGES;
|
||||
}
|
||||
|
||||
const _: () = assert!(mem::size_of::<usize>() <= mem::size_of::<u64>());
|
||||
const _: () = assert!(size_of::<usize>() <= size_of::<u64>());
|
||||
|
||||
let mut region = kvm_userspace_memory_region2 {
|
||||
slot,
|
||||
@@ -2022,7 +2022,7 @@ impl cpu::Vcpu for KvmVcpu {
|
||||
.get_one_reg(arm64_core_reg_id!(KVM_REG_SIZE_U64, off), &mut bytes)
|
||||
.map_err(|e| cpu::HypervisorCpuError::GetAarchCoreRegister(e.into()))?;
|
||||
state.regs.regs[i] = u64::from_le_bytes(bytes);
|
||||
off += mem::size_of::<u64>();
|
||||
off += size_of::<u64>();
|
||||
}
|
||||
|
||||
// We are now entering the "Other register" section of the ARMv8-a architecture.
|
||||
@@ -2075,7 +2075,7 @@ impl cpu::Vcpu for KvmVcpu {
|
||||
.get_one_reg(arm64_core_reg_id!(KVM_REG_SIZE_U64, off), &mut bytes)
|
||||
.map_err(|e| cpu::HypervisorCpuError::GetAarchCoreRegister(e.into()))?;
|
||||
state.spsr[i] = u64::from_le_bytes(bytes);
|
||||
off += mem::size_of::<u64>();
|
||||
off += size_of::<u64>();
|
||||
}
|
||||
|
||||
Ok(state.into())
|
||||
@@ -2177,7 +2177,7 @@ impl cpu::Vcpu for KvmVcpu {
|
||||
&kvm_regs_state.regs.regs[i].to_le_bytes(),
|
||||
)
|
||||
.map_err(|e| cpu::HypervisorCpuError::SetAarchCoreRegister(e.into()))?;
|
||||
off += mem::size_of::<u64>();
|
||||
off += size_of::<u64>();
|
||||
}
|
||||
|
||||
let off = offset_of!(user_pt_regs, sp);
|
||||
@@ -2228,7 +2228,7 @@ impl cpu::Vcpu for KvmVcpu {
|
||||
&kvm_regs_state.spsr[i].to_le_bytes(),
|
||||
)
|
||||
.map_err(|e| cpu::HypervisorCpuError::SetAarchCoreRegister(e.into()))?;
|
||||
off += mem::size_of::<u64>();
|
||||
off += size_of::<u64>();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
@@ -3723,7 +3723,7 @@ impl KvmVcpu {
|
||||
.get_one_reg(arm64_core_reg_id!(KVM_REG_SIZE_U128, off), &mut bytes)
|
||||
.map_err(|e| cpu::HypervisorCpuError::GetAarchCoreRegister(e.into()))?;
|
||||
regs.fp_regs.vregs[i] = u128::from_le_bytes(bytes);
|
||||
off += mem::size_of::<u128>();
|
||||
off += size_of::<u128>();
|
||||
}
|
||||
|
||||
// Floating-point Status Register
|
||||
@@ -3756,7 +3756,7 @@ impl KvmVcpu {
|
||||
®s.fp_regs.vregs[i].to_le_bytes(),
|
||||
)
|
||||
.map_err(|e| cpu::HypervisorCpuError::SetAarchCoreRegister(e.into()))?;
|
||||
off += mem::size_of::<u128>();
|
||||
off += size_of::<u128>();
|
||||
}
|
||||
|
||||
// Floating-point Status Register
|
||||
|
||||
@@ -74,7 +74,7 @@ macro_rules! riscv64_reg_id {
|
||||
kvm_bindings::KVM_REG_RISCV as u64
|
||||
| u64::from($reg_type)
|
||||
| u64::from(kvm_bindings::KVM_REG_SIZE_U64)
|
||||
| (($offset / std::mem::size_of::<u64>()) as u64)
|
||||
| (($offset / size_of::<u64>()) as u64)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -291,7 +291,6 @@ impl SevFd {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use core::mem::size_of;
|
||||
|
||||
use super::*;
|
||||
|
||||
|
||||
@@ -116,7 +116,6 @@ fn vec_with_size_in_bytes<T: Default>(size_in_bytes: usize) -> Vec<T> {
|
||||
// for `Foo`, a `Vec<Foo>` is created. Only the first element of `Vec<Foo>` would actually be used
|
||||
// as a `Foo`. The remaining memory in the `Vec<Foo>` is for `entries`, which must be contiguous
|
||||
// with `Foo`. This function is used to make the `Vec<Foo>` with enough space for `count` entries.
|
||||
use std::mem::size_of;
|
||||
pub fn vec_with_array_field<T: Default, F>(count: usize) -> Vec<T> {
|
||||
let element_space = count * size_of::<F>();
|
||||
let vec_size_bytes = size_of::<T>() + element_space;
|
||||
|
||||
Reference in New Issue
Block a user