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>
impl<T: Poolable> FixedPool<T>
Sourcepub fn with_config(config: PoolConfig<T>) -> Result<Self>
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();Sourcepub fn allocate(&self, value: T) -> Result<OwnedHandle<'_, T>>
pub fn allocate(&self, value: T) -> Result<OwnedHandle<'_, T>>
Sourcepub fn allocate_batch(&self, values: Vec<T>) -> Result<Vec<OwnedHandle<'_, T>>>
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.
Sourcepub fn try_allocate(&self, value: T) -> Option<OwnedHandle<'_, T>>
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);
};Sourcepub fn statistics(&self) -> PoolStatistics
Available on crate feature stats only.
pub fn statistics(&self) -> PoolStatistics
stats only.Get current pool statistics.
Sourcepub fn reset_statistics(&self)
Available on crate feature stats only.
pub fn reset_statistics(&self)
stats only.Reset statistics counters.
Trait Implementations§
impl<T: Poolable> PoolInterface<T> for FixedPool<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more