use core::{marker::PhantomData, mem}; use crate::{ bindings::{bpf_map_def, bpf_map_type::BPF_MAP_TYPE_QUEUE}, helpers::{bpf_map_pop_elem, bpf_map_push_elem}, }; #[repr(transparent)] pub struct Queue { def: bpf_map_def, _t: PhantomData, } impl Queue { pub const fn with_max_entries(max_entries: u32, flags: u32) -> Queue { Queue { def: bpf_map_def { type_: BPF_MAP_TYPE_QUEUE, key_size: 0, value_size: mem::size_of::() as u32, max_entries, map_flags: flags, id: 0, pinning: 0, }, _t: PhantomData, } } pub unsafe fn push(&mut self, value: &T, flags: u64) -> Result<(), i64> { let ret = bpf_map_push_elem( &mut self.def as *mut _ as *mut _, value as *const _ as *const _, flags, ); if ret < 0 { Err(ret) } else { Ok(()) } } pub unsafe fn pop(&mut self) -> Option { let mut value = mem::MaybeUninit::uninit(); let ret = bpf_map_pop_elem( &mut self.def as *mut _ as *mut _, &mut value as *mut _ as *mut _, ); if ret < 0 { None } else { Some(value.assume_init()) } } }