Skip to content

Commit

Permalink
Introduce OpenMap::initial_value_mut()
Browse files Browse the repository at this point in the history
Similar to what we have with OpenMap::initial_value(), introduce a
version that returns a mutable slice.

Signed-off-by: Daniel Müller <[email protected]>
  • Loading branch information
d-e-s-o committed Mar 21, 2024
1 parent 77cc68c commit 3dfb148
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion libbpf-rs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Unreleased
- Added `vendored` feature to use vendored copies of all needed libraries
- Added `Program::attach_ksyscall` for attaching to ksyscall handlers
- Added `Program::test_run` as a way for test-running programs
- Added `OpenMap::initial_value` for retrieving a map's initial value
- Added `OpenMap::initial_value{,_mut}` for retrieving a map's initial value
- Added `replace` functionality to `Xdp` type
- Added low-level `consume_raw` and `poll_raw` methods to `RingBuffer` type
- Added `recursion_misses` attribute to `query::ProgramInfo` type
Expand Down
25 changes: 20 additions & 5 deletions libbpf-rs/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,31 @@ impl OpenMap {
}
}

/// Retrieve the initial value of the map.
pub fn initial_value(&self) -> Option<&[u8]> {
fn initial_value_raw(&self) -> (*mut u8, usize) {
let mut size = 0u64;
let value = unsafe {
let ptr = unsafe {
libbpf_sys::bpf_map__initial_value(self.ptr.as_ptr(), &mut size as *mut _ as _)
};
if value.is_null() {
(ptr.cast(), size as _)
}
/// Retrieve the initial value of the map.
pub fn initial_value(&self) -> Option<&[u8]> {
let (ptr, size) = self.initial_value_raw();
if ptr.is_null() {
None
} else {
let data = unsafe { slice::from_raw_parts(ptr.cast::<u8>(), size) };
Some(data)
}
}

/// Retrieve the initial value of the map.
pub fn initial_value_mut(&mut self) -> Option<&mut [u8]> {
let (ptr, size) = self.initial_value_raw();
if ptr.is_null() {
None
} else {
let data = unsafe { slice::from_raw_parts(value.cast::<u8>(), size as _) };
let data = unsafe { slice::from_raw_parts_mut(ptr.cast::<u8>(), size) };
Some(data)
}
}
Expand Down

0 comments on commit 3dfb148

Please sign in to comment.