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
impl PacketPool
Sourcepub fn new(capacity: usize) -> Result<Self>
pub fn new(capacity: usize) -> Result<Self>
Creates a new packet pool with the specified capacity.
§Errors
Returns an error if allocation fails.
Sourcepub fn with_default_capacity() -> Result<Self>
pub fn with_default_capacity() -> Result<Self>
Sourcepub fn free_count(&self) -> usize
pub fn free_count(&self) -> usize
Returns the number of free buffers.
Sourcepub fn allocated_count(&self) -> usize
pub fn allocated_count(&self) -> usize
Returns the number of allocated buffers.
Sourcepub fn alloc(&self) -> Option<PacketRef<'_>>
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.
Sourcepub fn alloc_index(&self) -> Option<u32>
pub fn alloc_index(&self) -> Option<u32>
Allocates a buffer and returns its index.
Returns None if the pool is empty.
Sourcepub fn alloc_with_data(&self, data: &[u8]) -> Result<PacketRef<'_>>
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.
Sourcepub unsafe fn free_by_index(&self, idx: u32)
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.
Sourcepub unsafe fn get(&self, idx: u32) -> &PacketBuffer
pub unsafe fn get(&self, idx: u32) -> &PacketBuffer
Sourcepub unsafe fn get_mut(&self, idx: u32) -> &mut PacketBuffer
pub unsafe fn get_mut(&self, idx: u32) -> &mut PacketBuffer
Sourcepub fn alloc_batch_indices(&self, out: &mut [u32]) -> usize
pub fn alloc_batch_indices(&self, out: &mut [u32]) -> usize
Allocates multiple buffer indices at once.
Returns the number of indices actually allocated.