Skip to main content

PacketPool

Struct PacketPool 

Source
pub struct PacketPool { /* private fields */ }
Expand description

Pre-allocated packet buffer pool.

Uses an MpmcRing of free indices for lock-free allocation and deallocation. The Vyukov bounded MPMC algorithm used by MpmcRing provides ABA safety via per-slot sequence numbers, so no custom tagged pointers or Treiber stacks are needed.

All buffers are pre-allocated at construction time to avoid runtime memory allocation in the hot path.

Implementations§

Source§

impl PacketPool

Source

pub fn new(capacity: usize) -> Result<Self>

Creates a new packet pool with the specified capacity.

§Errors

Returns an error if allocation fails.

Source

pub fn with_default_capacity() -> Result<Self>

Creates a new pool with the default capacity.

§Errors

Returns an error if allocation fails.

Source

pub const fn capacity(&self) -> usize

Returns the pool capacity.

Source

pub fn free_count(&self) -> usize

Returns the number of free buffers.

Source

pub fn allocated_count(&self) -> usize

Returns the number of allocated buffers.

Source

pub fn alloc(&self) -> Option<PacketRef<'_>>

Allocates a buffer from the pool.

Returns None if the pool is empty. The returned PacketRef auto-frees the buffer on drop; use PacketRef::into_index to transfer ownership by index without auto-freeing.

Source

pub fn alloc_index(&self) -> Option<u32>

Allocates a buffer and returns its index.

Returns None if the pool is empty.

Source

pub fn alloc_with_data(&self, data: &[u8]) -> Result<PacketRef<'_>>

Allocates a buffer and initializes it with data.

§Errors

Returns an error if the pool is empty or data is too large.

Source

pub unsafe fn free_by_index(&self, idx: u32)

Returns a buffer to the pool by index.

§Safety

The buffer at this index must not be in use elsewhere.

Source

pub unsafe fn get(&self, idx: u32) -> &PacketBuffer

Gets a buffer by index.

§Safety

The caller must ensure the buffer is allocated.

Source

pub unsafe fn get_mut(&self, idx: u32) -> &mut PacketBuffer

Gets a mutable buffer by index.

§Safety

The caller must ensure the buffer is allocated, has exclusive access, and no PacketRef exists for the same index. Violating this creates aliasing &mut references, which is instant UB.

Source

pub fn alloc_batch_indices(&self, out: &mut [u32]) -> usize

Allocates multiple buffer indices at once.

Returns the number of indices actually allocated.

Trait Implementations§

Source§

impl Debug for PacketPool

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Send for PacketPool

Source§

impl Sync for PacketPool

Auto Trait Implementations§

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 = 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.