FixedPool

Struct FixedPool 

Source
pub struct FixedPool<T> { /* private fields */ }
Expand description

A fixed-size memory pool with O(1) allocation and deallocation.

This pool pre-allocates a fixed number of slots and does not grow. It provides the fastest possible allocation performance with zero fragmentation and predictable behavior.

§Examples

use fastalloc::FixedPool;

// Create a pool of 1000 integers
let pool = FixedPool::<i32>::new(1000).unwrap();

// Allocate from the pool
let mut handle = pool.allocate(42).unwrap();
assert_eq!(*handle, 42);

// Modify the value
*handle = 100;
assert_eq!(*handle, 100);

// Automatically returned to pool when dropped
drop(handle);

§Performance

  • Allocation: < 20ns per object (typical)
  • Deallocation: < 10ns per object (typical)
  • Memory overhead: ~8 bytes per slot + allocator metadata
  • Zero fragmentation

Implementations§

Source§

impl<T: Poolable> FixedPool<T>

Source

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

Creates a new fixed-size pool with the specified capacity.

§Examples
use fastalloc::FixedPool;

let pool = FixedPool::<String>::new(100).unwrap();
§Errors

Returns an error if capacity is 0.

Source

pub fn with_config(config: PoolConfig<T>) -> Result<Self>

Creates a new fixed-size pool with the specified configuration.

§Examples
use fastalloc::{FixedPool, PoolConfig};

let config = PoolConfig::builder()
    .capacity(1000)
    .alignment(64)
    .pre_initialize(false)
    .build()
    .unwrap();

let pool = FixedPool::<i32>::with_config(config).unwrap();
Source

pub fn allocate(&self, value: T) -> Result<OwnedHandle<'_, T>>

Allocates an object from the pool with the given initial value.

§Examples
use fastalloc::FixedPool;

let pool = FixedPool::new(10).unwrap();
let handle = pool.allocate(42).unwrap();
assert_eq!(*handle, 42);
§Errors

Returns Error::PoolExhausted if the pool is at capacity.

Source

pub fn allocate_batch(&self, values: Vec<T>) -> Result<Vec<OwnedHandle<'_, T>>>

Allocates multiple objects from the pool in a single operation.

This is more efficient than multiple individual allocate calls as it reduces the number of borrow operations.

§Examples
use fastalloc::FixedPool;

let pool = FixedPool::new(100).unwrap();
let values = vec![1, 2, 3, 4, 5];
let handles = pool.allocate_batch(values).unwrap();
assert_eq!(handles.len(), 5);
§Errors

Returns Error::PoolExhausted if there aren’t enough free slots.

Source

pub fn try_allocate(&self, value: T) -> Option<OwnedHandle<'_, T>>

Attempts to allocate from the pool, returning None if exhausted.

This is a convenience method that doesn’t return an error.

§Examples
use fastalloc::FixedPool;

let pool = FixedPool::new(10).unwrap();
if let Some(handle) = pool.try_allocate(42) {
    assert_eq!(*handle, 42);
};
Source

pub fn capacity(&self) -> usize

Returns the total capacity of the pool.

Source

pub fn available(&self) -> usize

Returns the number of available (free) slots in the pool.

Source

pub fn allocated(&self) -> usize

Returns the number of currently allocated objects.

Source

pub fn is_full(&self) -> bool

Returns whether the pool is full (no available slots).

Source

pub fn is_empty(&self) -> bool

Returns whether the pool is empty (all slots available).

Source

pub fn statistics(&self) -> PoolStatistics

Available on crate feature stats only.

Get current pool statistics.

Source

pub fn reset_statistics(&self)

Available on crate feature stats only.

Reset statistics counters.

Trait Implementations§

Source§

impl<T> Drop for FixedPool<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T: Poolable> PoolInterface<T> for FixedPool<T>

Source§

impl<T: Send> Send for FixedPool<T>

Auto Trait Implementations§

§

impl<T> !Freeze for FixedPool<T>

§

impl<T> !RefUnwindSafe for FixedPool<T>

§

impl<T> !Sync for FixedPool<T>

§

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

§

impl<T> !UnwindSafe for FixedPool<T>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more