pub struct RingBuffer<'a, T> { /* private fields */ }
Expand description
Ring buffer for allocating varying length byte slices in a sequential, FIFO fashion.
Allocations are represented by RingSlice
s, which are lifetimeless
handles to this buffer, and can be used to get a &mut [u8]
that will hold
the memory until the RingSlice
is passed into RingBuffer::free
. The
slices must be freed in the same order as they were allocated. As such,
dropping a RingSlice
will cause the slice to never be reclaimed, which
will “clog” the ring buffer.
The sum of the lengths of the slices allocated from this buffer, when full, may be less than the total capacity, since the individual slices are contiguous and can’t span across the end of the backing buffer. These gaps could be prevented with memory mapping trickery in the future.
Implementations§
Source§impl<T> RingBuffer<'_, T>
impl<T> RingBuffer<'_, T>
Sourcepub fn new(
allocator: &'static LinearAllocator<'_>,
capacity: usize,
) -> Option<RingBuffer<'static, T>>
pub fn new( allocator: &'static LinearAllocator<'_>, capacity: usize, ) -> Option<RingBuffer<'static, T>>
Allocates a new ring buffer with the given capacity.
Sourcepub unsafe fn from_mut(buffer: &mut [MaybeUninit<T>]) -> RingBuffer<'_, T>
pub unsafe fn from_mut(buffer: &mut [MaybeUninit<T>]) -> RingBuffer<'_, T>
Creates a new ring buffer with the given buffer.
§Safety
All allocations made from this RingBuffer
must be passed back into
RingBuffer::free
before it is dropped, as the backing memory is only
borrowed for its lifetime, and the Box
references could leak.
Source§impl<T: Zeroable> RingBuffer<'_, T>
impl<T: Zeroable> RingBuffer<'_, T>
Sourcepub fn allocate(&mut self, len: usize) -> Option<RingSlice<T>>
pub fn allocate(&mut self, len: usize) -> Option<RingSlice<T>>
Allocates and zeroes out a slice of the given length if there’s enough contiguous free space.
Sourcepub fn free(&mut self, slice: RingSlice<T>) -> Result<(), RingSlice<T>>
pub fn free(&mut self, slice: RingSlice<T>) -> Result<(), RingSlice<T>>
Reclaims the memory occupied by the given slice. Returns the slice back
in an Err
if the slice isn’t the current head of the allocated span,
and the memory is not reclaimed.
§Panics
Panics if the RingSlice
was allocated from a different
RingBuffer
.
Source§impl<T> RingBuffer<'_, T>
impl<T> RingBuffer<'_, T>
Sourcepub fn allocate_box(&mut self, value: T) -> Result<RingBox<T>, T>
pub fn allocate_box(&mut self, value: T) -> Result<RingBox<T>, T>
Allocates space for one T if there’s free space, and boxes it.
Sourcepub fn free_box(&mut self, boxed: RingBox<T>) -> Result<(), RingBox<T>>
pub fn free_box(&mut self, boxed: RingBox<T>) -> Result<(), RingBox<T>>
Reclaims the memory occupied by the given box. Returns the box back in
an Err
if the slice isn’t the current head of the allocated span, and
the memory is not reclaimed.
§Panics
Panics if the RingBox
was allocated from a different RingBuffer
.