use crate::error::VZResult;
use std::ffi::c_void;
pub struct MemoryBalloonDeviceConfiguration {
inner: *mut c_void,
}
unsafe impl Send for MemoryBalloonDeviceConfiguration {}
impl MemoryBalloonDeviceConfiguration {
pub fn new() -> VZResult<Self> {
let obj = unsafe { crate::shim_ffi::abx_balloon_config_new() };
Ok(Self { inner: obj })
}
#[must_use]
pub(crate) fn into_ptr(self) -> *mut c_void {
let ptr = self.inner;
std::mem::forget(self);
ptr
}
}
impl Default for MemoryBalloonDeviceConfiguration {
fn default() -> Self {
Self::new().expect("Failed to create balloon device configuration")
}
}
impl Drop for MemoryBalloonDeviceConfiguration {
fn drop(&mut self) {
if !self.inner.is_null() {
unsafe { crate::shim_ffi::abx_object_release(self.inner.cast()) };
}
}
}
pub struct MemoryBalloonDevice {
balloon_box: *mut std::ffi::c_void,
}
unsafe impl Send for MemoryBalloonDevice {}
unsafe impl Sync for MemoryBalloonDevice {}
impl MemoryBalloonDevice {
pub(crate) fn from_box(balloon_box: *mut std::ffi::c_void) -> Self {
Self { balloon_box }
}
pub fn set_target_memory_size(&self, bytes: u64) {
unsafe { crate::shim_ffi::abx_balloon_set_target(self.balloon_box, bytes) };
tracing::debug!(
"Set balloon target memory to {} bytes ({}MB)",
bytes,
bytes / (1024 * 1024)
);
}
pub fn target_memory_size(&self) -> u64 {
unsafe { crate::shim_ffi::abx_balloon_target(self.balloon_box) }
}
}
impl Drop for MemoryBalloonDevice {
fn drop(&mut self) {
if !self.balloon_box.is_null() {
unsafe { crate::shim_ffi::abx_object_release(self.balloon_box) };
}
}
}