aya-bpf/maps: Add `get_ptr` and `get_mut_ptr` methods to Array

Signed-off-by: Michal Rostecki <vadorovsky@gmail.com>
pull/402/head
Michal Rostecki 2 years ago
parent 897957ac84
commit 33baf7ef22

@ -47,14 +47,28 @@ impl<T> Array<T> {
} }
} }
#[inline(always)]
pub fn get(&self, index: u32) -> Option<&T> { pub fn get(&self, index: u32) -> Option<&T> {
unsafe { // FIXME: alignment
let value = bpf_map_lookup_elem( unsafe { self.lookup(index).map(|p| p.as_ref()) }
self.def.get() as *mut _, }
&index as *const _ as *const c_void,
); #[inline(always)]
// FIXME: alignment pub fn get_ptr(&self, index: u32) -> Option<*const T> {
NonNull::new(value as *mut T).map(|p| p.as_ref()) unsafe { self.lookup(index).map(|p| p.as_ptr() as *const T) }
} }
#[inline(always)]
pub fn get_ptr_mut(&self, index: u32) -> Option<*mut T> {
unsafe { self.lookup(index).map(|p| p.as_ptr()) }
}
#[inline(always)]
unsafe fn lookup(&self, index: u32) -> Option<NonNull<T>> {
let ptr = bpf_map_lookup_elem(
self.def.get() as *mut _,
&index as *const _ as *const c_void,
);
NonNull::new(ptr as *mut T)
} }
} }

Loading…
Cancel
Save