Struct BufferPool

Source
pub struct BufferPool<T: Buffer> {
    pub mode: PoolMode,
    /* 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.

Fields§

§mode: PoolMode

Tracks the current mode of memory allocation (back, front, or system).

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.

Source§

impl<T: Buffer> BufferPool<T>

Source

pub fn droppable(&self) -> bool

Determines if the BufferPool can be safely dropped.

Returns true if all memory slices managed by the buffer pool have been released (i.e., the shared_state is zero), indicating that all the slices are dropped. This check helps prevent dropping the buffer pool while it’s still in use.

Trait Implementations§

Source§

impl<T: Buffer> AsMut<[u8]> for BufferPool<T>

Source§

fn as_mut(&mut self) -> &mut [u8]

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<T: Buffer> AsRef<[u8]> for BufferPool<T>

Source§

fn as_ref(&self) -> &[u8]

Converts this type into a shared reference of the (usually inferred) input type.
Source§

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

Source§

type Slice = Slice

The type of slice that the buffer uses.
Source§

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

Borrows a mutable slice of the buffer, allowing the caller to write data into it. The caller specifies the length of the data they need to write.
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 get_data_by_ref(&mut self, len: usize) -> &mut [u8]

Provides a mutable reference to the written portion of the buffer, up to the specified length, without transferring ownership of the buffer. This allows the caller to modify the buffer’s contents directly without taking ownership.
Source§

fn get_data_by_ref_(&self, len: usize) -> &[u8]

Provides an immutable reference to the written portion of the buffer, up to the specified length, without transferring ownership of the buffer. This allows the caller to inspect the buffer’s contents without modifying or taking ownership.
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 danger_set_start(&mut self, index: usize)

Modifies the starting point of the buffer, effectively discarding data up to the given index. This can be useful for performance optimizations in situations where older data is no longer needed, but its use can be unsafe unless you understand its implications.
Source§

fn is_droppable(&self) -> bool

Determines if the buffer is safe to drop. This typically checks if the buffer contains essential data that still needs to be processed. Read more
Source§

fn is_empty(&self) -> bool

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

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

Source§

fn extend_from_slice(&mut self, other: &[u8]) -> Result<()>

Extend this buffer from the given slice
Source§

fn truncate(&mut self, len: usize)

Truncate this buffer to the given size
Source§

fn len(&self) -> usize

Get the length of the buffer
Source§

fn is_empty(&self) -> bool

Is the buffer empty?
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
Source§

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

Source§

fn drop(&mut self)

Executes the destructor for this type. 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> 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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.