qemu_plugin/scoreboard/
mod.rs

1//! Scoreboard-related functionality for QEMU plugins
2
3#[cfg(not(any(feature = "plugin-api-v0", feature = "plugin-api-v1")))]
4use crate::VCPUIndex;
5#[cfg(not(any(feature = "plugin-api-v0", feature = "plugin-api-v1")))]
6use crate::sys::qemu_plugin_scoreboard;
7#[cfg(not(any(feature = "plugin-api-v0", feature = "plugin-api-v1")))]
8use std::{marker::PhantomData, mem::MaybeUninit};
9
10#[cfg(not(any(feature = "plugin-api-v0", feature = "plugin-api-v1")))]
11#[derive(Debug)]
12/// A wrapper structure for a `qemu_plugin_scoreboard *`. This is a way of having one
13/// entry per VCPU, the count of which is managed automatically by QEMU. Keep in mind
14/// that additional entries *and* existing entries will be allocated and reallocated by
15/// *qemu*, not by the plugin, so every use of a `T` should include a check for whether
16/// it is initialized.
17pub struct Scoreboard<'a, T>
18where
19    T: Sized,
20{
21    handle: usize,
22    marker: PhantomData<&'a T>,
23}
24
25#[cfg(not(any(feature = "plugin-api-v0", feature = "plugin-api-v1")))]
26impl<'a, T> Scoreboard<'a, T> {
27    /// Allocate a new scoreboard object. This must be freed by calling
28    /// `qemu_plugin_scoreboard_free` (or by being dropped).
29    pub fn new() -> Self {
30        let handle =
31            unsafe { crate::sys::qemu_plugin_scoreboard_new(std::mem::size_of::<T>()) as usize };
32
33        Self {
34            handle,
35            marker: PhantomData,
36        }
37    }
38
39    /// Returns a reference to entry of a scoreboard matching a given vcpu index. This address
40    /// is only valid until the next call to `get` or `set`.
41    pub fn find<'b>(&mut self, vcpu_index: VCPUIndex) -> &'b mut MaybeUninit<T> {
42        unsafe {
43            &mut *(crate::sys::qemu_plugin_scoreboard_find(
44                self.handle as *mut qemu_plugin_scoreboard,
45                vcpu_index,
46            ) as *mut MaybeUninit<T>)
47        }
48    }
49}
50
51#[cfg(not(any(feature = "plugin-api-v0", feature = "plugin-api-v1")))]
52impl<'a, T> Default for Scoreboard<'a, T> {
53    fn default() -> Self {
54        Self::new()
55    }
56}
57
58#[cfg(not(any(feature = "plugin-api-v0", feature = "plugin-api-v1")))]
59impl<'a, T> Drop for Scoreboard<'a, T> {
60    fn drop(&mut self) {
61        unsafe {
62            crate::sys::qemu_plugin_scoreboard_free(self.handle as *mut qemu_plugin_scoreboard)
63        }
64    }
65}