pub struct Buffer { /* private fields */ }
Expand description
Buffer is a mutable byte container that is aligned to crate::ALIGNMENT bytes, and has a padding so the size of the underlying allocation is a multiple of crate::ALIGNMENT.
The alignment is because we want it to be aligned to the cacheline. The padding is because we want it to be easily usable with SIMD instructions without checking length.
Implementations§
Source§impl Buffer
impl Buffer
Sourcepub fn new(len: usize) -> Self
pub fn new(len: usize) -> Self
Create a new buffer. This function will allocate a padded and aligned memory chunk that is able to hold the given len number of bytes. The allocated memory will be zeroed.
Doesn’t allocate if len
is zero. In this case the buffer will have a dangling pointer.
§Panics
Panics if len
overflows isize
when padded to crate::ALIGNMENT bytes. Or if memory
can’t be allocated.
Sourcepub fn as_ptr(&self) -> *const u8
pub fn as_ptr(&self) -> *const u8
Get a pointer to the underlying memory.
The underlying memory is aligned to crate::ALIGNMENT bytes and padded to crate::ALIGNMENT bytes.
Sourcepub fn as_mut_ptr(&mut self) -> *mut u8
pub fn as_mut_ptr(&mut self) -> *mut u8
Get a mutable pointer to the underlying memory.
The underlying memory is aligned to crate::ALIGNMENT bytes and padded to crate::ALIGNMENT bytes.
Sourcepub fn as_mut_slice(&mut self) -> &mut [u8] ⓘ
pub fn as_mut_slice(&mut self) -> &mut [u8] ⓘ
Get a mutable slice to the underlying memory
Sourcepub fn cold_load(&mut self, src: &[u8])
pub fn cold_load(&mut self, src: &[u8])
Loads data from src
to the underlying memory. Lenghth of self
must be greater than or equal to the length of src
This might be faster than the regular memcopy, especially for large copies. Because it bypasses the
CPU cache when writing if possible. This is only advantageus if the user won’t be reading the memory
stored in self
, for example it can be good when reading bulk data from disk but not so much if
summing small/medium (fits in CPU cache) sized integer arrays and then doing other computation with them.
§Panics
Panics if self.len() < src.len()
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Length of the buffer. Keep in mind that the underlying memory is padded to crate::ALIGNMENT bytes so might be bigger than the returned value.
Sourcepub fn from_slice(src: &[u8]) -> Self
pub fn from_slice(src: &[u8]) -> Self
Create a Buffer from given slice.
Sourcepub fn from_slice_cold(src: &[u8]) -> Self
pub fn from_slice_cold(src: &[u8]) -> Self
Similar to Self::from_slice but bypasses CPU cache for writes if possible.
See Self::cold_load for tradeoffs.