mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2024-08-21 07:21:15 +00:00
hypervisor: refactor vec_with_array_field function
refactor vec_with_array_field to common hypervisor code so that mshv can also make use of it. Signed-off-by: Vineeth Pillai <viremana@linux.microsoft.com>
This commit is contained in:
parent
b8c0cd5cd4
commit
7fad74cb04
@ -16,6 +16,7 @@ pub use crate::aarch64::{
|
|||||||
use crate::cpu;
|
use crate::cpu;
|
||||||
use crate::device;
|
use crate::device;
|
||||||
use crate::hypervisor;
|
use crate::hypervisor;
|
||||||
|
use crate::vec_with_array_field;
|
||||||
use crate::vm::{self, VmmOps};
|
use crate::vm::{self, VmmOps};
|
||||||
#[cfg(target_arch = "aarch64")]
|
#[cfg(target_arch = "aarch64")]
|
||||||
use crate::{arm64_core_reg_id, offset__of};
|
use crate::{arm64_core_reg_id, offset__of};
|
||||||
@ -110,36 +111,6 @@ pub struct KvmVm {
|
|||||||
state: KvmVmState,
|
state: KvmVmState,
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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> {
|
|
||||||
let rounded_size = (size_in_bytes + size_of::<T>() - 1) / size_of::<T>();
|
|
||||||
let mut v = Vec::with_capacity(rounded_size);
|
|
||||||
v.resize_with(rounded_size, T::default);
|
|
||||||
v
|
|
||||||
}
|
|
||||||
|
|
||||||
// The kvm API has many structs that resemble the following `Foo` structure:
|
|
||||||
//
|
|
||||||
// ```
|
|
||||||
// #[repr(C)]
|
|
||||||
// struct Foo {
|
|
||||||
// some_data: u32
|
|
||||||
// entries: __IncompleteArrayField<__u32>,
|
|
||||||
// }
|
|
||||||
// ```
|
|
||||||
//
|
|
||||||
// In order to allocate such a structure, `size_of::<Foo>()` would be too small because it would not
|
|
||||||
// include any space for `entries`. To make the allocation large enough while still being aligned
|
|
||||||
// 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;
|
|
||||||
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;
|
|
||||||
vec_with_size_in_bytes(vec_size_bytes)
|
|
||||||
}
|
|
||||||
|
|
||||||
///
|
///
|
||||||
/// Implementation of Vm trait for KVM
|
/// Implementation of Vm trait for KVM
|
||||||
/// Example:
|
/// Example:
|
||||||
|
@ -72,3 +72,33 @@ pub fn new() -> std::result::Result<Arc<dyn Hypervisor>, HypervisorError> {
|
|||||||
|
|
||||||
Ok(Arc::new(hv))
|
Ok(Arc::new(hv))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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> {
|
||||||
|
let rounded_size = (size_in_bytes + size_of::<T>() - 1) / size_of::<T>();
|
||||||
|
let mut v = Vec::with_capacity(rounded_size);
|
||||||
|
v.resize_with(rounded_size, T::default);
|
||||||
|
v
|
||||||
|
}
|
||||||
|
|
||||||
|
// The kvm API has many structs that resemble the following `Foo` structure:
|
||||||
|
//
|
||||||
|
// ```
|
||||||
|
// #[repr(C)]
|
||||||
|
// struct Foo {
|
||||||
|
// some_data: u32
|
||||||
|
// entries: __IncompleteArrayField<__u32>,
|
||||||
|
// }
|
||||||
|
// ```
|
||||||
|
//
|
||||||
|
// In order to allocate such a structure, `size_of::<Foo>()` would be too small because it would not
|
||||||
|
// include any space for `entries`. To make the allocation large enough while still being aligned
|
||||||
|
// 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;
|
||||||
|
vec_with_size_in_bytes(vec_size_bytes)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user