mirror of https://github.com/aya-rs/aya
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.5 KiB
Rust
54 lines
1.5 KiB
Rust
use core::{cell::UnsafeCell, mem, ptr};
|
|
|
|
use crate::{
|
|
bindings::bpf_map_type::BPF_MAP_TYPE_STACK,
|
|
btf_maps::AyaBtfMapMarker,
|
|
helpers::{bpf_map_pop_elem, bpf_map_push_elem},
|
|
};
|
|
|
|
#[allow(dead_code)]
|
|
pub struct StackDef<T, const M: usize, const F: usize = 0> {
|
|
r#type: *const [i32; BPF_MAP_TYPE_STACK as usize],
|
|
value: *const T,
|
|
max_entries: *const [i32; M],
|
|
|
|
// Anonymize the struct.
|
|
_anon: AyaBtfMapMarker,
|
|
}
|
|
|
|
#[repr(transparent)]
|
|
pub struct Stack<T, const M: usize, const F: usize = 0>(UnsafeCell<StackDef<T, M, F>>);
|
|
|
|
impl<T, const M: usize, const F: usize> Stack<T, M, F> {
|
|
pub const fn new() -> Self {
|
|
Self(UnsafeCell::new(StackDef {
|
|
r#type: &[0i32; BPF_MAP_TYPE_STACK as usize],
|
|
value: ptr::null(),
|
|
max_entries: &[0i32; M],
|
|
_anon: AyaBtfMapMarker::new(),
|
|
}))
|
|
}
|
|
|
|
pub fn push(&mut self, value: &T, flags: u64) -> Result<(), i64> {
|
|
let ret = unsafe {
|
|
bpf_map_push_elem(
|
|
&mut self.0 as *mut _ as *mut _,
|
|
value as *const _ as *const _,
|
|
flags,
|
|
)
|
|
};
|
|
(ret == 0).then_some(()).ok_or(ret)
|
|
}
|
|
|
|
pub fn pop(&mut self) -> Option<T> {
|
|
unsafe {
|
|
let mut value = mem::MaybeUninit::uninit();
|
|
let ret = bpf_map_pop_elem(
|
|
&mut self.0 as *mut _ as *mut _,
|
|
value.as_mut_ptr() as *mut _,
|
|
);
|
|
(ret == 0).then_some(value.assume_init())
|
|
}
|
|
}
|
|
}
|