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