Skip to main content

BufferPool

Struct BufferPool 

Source
pub struct BufferPool<T: Buffer> { /* private fields */ }
Expand description

A pool of reusable memory buffers to optimize memory allocation for Sv2 message frames.

Manages memory slices across three pool modes: back (default), front, and system memory. It reuses preallocated memory slices, minimizing the need for frequent memory allocation. The pool is thread-safe, using atomic state tracking.

Type T implements the Buffer trait, which defines how memory is handled in the buffer pool. BufferFromSystemMemory is used as the default system memory allocator.

Implementations§

Source§

impl BufferPool<BufferFromSystemMemory>

Source

pub fn new(capacity: usize) -> Self

Creates a new BufferPool with the specified memory capacity, in bytes.

Initializes the buffer pool with pre-allocated memory (inner_memory) and sets the pool mode to the back. The buffer pool uses BufferFromSystemMemory as a fallback when the buffer sections are full.

Source§

impl<T: Buffer> BufferPool<T>

Source

pub fn is_front_mode(&self) -> bool

Checks if the buffer pool is operating in the front mode.

This mode indicates that the back of the buffer pool has been filled and the system is now using the front section for memory allocation. Returns true if the pool is in front mode, otherwise false.

Source

pub fn is_back_mode(&self) -> bool

Checks if the buffer pool is operating in the back mode.

The back mode is the default state, where the buffer pool first tries to allocate memory. Returns true if the pool is in back mode, otherwise false.

Source

pub fn is_alloc_mode(&self) -> bool

Checks if the buffer pool is operating in the system memory allocation mode.

This mode is used when both the back and front sections of the buffer pool are full, leading the system to allocate memory from the heap, which has performance trade-offs. Returns true if the pool is in alloc mode, otherwise false.

Trait Implementations§

Source§

impl<T: Buffer> Buffer for BufferPool<T>

Source§

type Slice = Slice

The type of slice that the buffer uses.
Source§

fn reserve(&mut self, len: usize) -> &mut [u8] ⓘ

Makes room for len more bytes after the data written so far and returns that space, without counting any of it as written. Read more
Source§

fn commit(&mut self, len: usize)

Counts the first len bytes of the last reservation as written, and ends that reservation. Read more
Source§

fn get_data_owned(&mut self) -> Self::Slice

Provides ownership of a slice in the buffer pool to the caller and updates the buffer pool’s state by modifying the position in shared_state that the slice occupies. The pool now points to the next set of uninitialized space.
Source§

fn frame(&self) -> &[u8] ⓘ

Returns the committed bytes of the frame being written, without transferring ownership of the buffer.
Source§

fn frame_mut(&mut self) -> &mut [u8] ⓘ

Returns the committed bytes of the frame being written, mutably, without transferring ownership of the buffer.
Source§

fn len(&self) -> usize

Returns the size of the written portion of the buffer. This is useful for tracking how much of the buffer has been filled with data. The number of bytes currently written in the buffer is returned.
Source§

fn truncate(&mut self, len: usize)

Drops the committed bytes past len, like Vec::truncate: a len at or past the committed length changes nothing, so the frame can only shrink.
Source§

fn is_empty(&self) -> bool

Returns true if the buffer is empty, false otherwise.
Source§

impl<T: Debug + Buffer> Debug for BufferPool<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for BufferPool<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for BufferPool<T>
where T: RefUnwindSafe,

§

impl<T> Send for BufferPool<T>
where T: Send,

§

impl<T> Sync for BufferPool<T>
where T: Sync,

§

impl<T> Unpin for BufferPool<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for BufferPool<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for BufferPool<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.