Skip to content

Commit

Permalink
ffi: Make map_reserve! zero-initialize reserved bytes
Browse files Browse the repository at this point in the history
Valgrind was complaining about this. I don't think this should make a
difference for actual soundness, but this seems reasonable enough to do.

This reduces the number of Valgrind errors on the `properties` example
from `95265 errors from 172 contexts` to `78 errors from 2 contexts`.
  • Loading branch information
ids1024 authored and Drakulix committed May 7, 2024
1 parent 74a9bd3 commit 0db7924
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions drm-ffi/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,30 @@ macro_rules! map_len {
};
}

/// Takes an `Option<&mut Vec<T>>` style buffer and shrinks it.
/// Takes an `Option<&mut Vec<T>>` style buffer and reserves space.
macro_rules! map_reserve {
($buffer:expr, $size:expr) => {
match $buffer {
Some(ref mut b) => b.reserve_exact($size - b.len()),
Some(ref mut b) => crate::utils::map_reserve_inner(b, $size),
_ => (),
}
};
}

pub(crate) fn map_reserve_inner<T>(b: &mut Vec<T>, size: usize) {
let old_len = b.len();
if size <= old_len {
return;
}
b.reserve_exact(size - old_len);

// `memset` to 0, at least so Valgrind doesn't complain
unsafe {
let ptr = b.as_mut_ptr().add(old_len) as *mut u8;
ptr.write_bytes(0, (size - old_len) * std::mem::size_of::<T>());
}
}

/// Takes an `Option<&mut Vec<T>>` style buffer and shrinks it.
macro_rules! map_set {
($buffer:expr, $min:expr) => {
Expand Down

0 comments on commit 0db7924

Please sign in to comment.