fastalloc 1.5.0

High-performance memory pooling library with type-safe handles, predictable latency, and zero fragmentation. Perfect for game engines, real-time systems, and high-churn workloads.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Fixed-size memory pool implementation.

use crate::allocator::{Allocator, StackAllocator};
use crate::config::PoolConfig;
use crate::error::{Error, Result};
use crate::handle::OwnedHandle;
use crate::traits::Poolable;
use alloc::vec::Vec;
use core::cell::RefCell;
use core::marker::PhantomData;
use core::mem::MaybeUninit;
use core::ptr;

#[cfg(feature = "stats")]
use crate::stats::PoolStatistics;

/// 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
///
/// ```rust
/// 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
pub struct FixedPool<T> {
    /// Storage for pool objects
    storage: RefCell<Vec<MaybeUninit<T>>>,
    /// Allocator for managing free slots
    allocator: RefCell<StackAllocator>,
    /// Total capacity
    capacity: usize,
    /// Pool configuration
    #[allow(dead_code)]
    config: PoolConfig<T>,
    /// Statistics collector
    #[cfg(feature = "stats")]
    stats: RefCell<crate::stats::StatisticsCollector>,
    /// Marker for lifetime and Send/Sync bounds
    _marker: PhantomData<T>,
}

impl<T: Poolable> FixedPool<T> {
    /// Creates a new fixed-size pool with the specified capacity.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fastalloc::FixedPool;
    ///
    /// let pool = FixedPool::<String>::new(100).unwrap();
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if capacity is 0.
    pub fn new(capacity: usize) -> Result<Self> {
        let config = PoolConfig::builder().capacity(capacity).build()?;
        Self::with_config(config)
    }

    /// Creates a new fixed-size pool with the specified configuration.
    ///
    /// # Examples
    ///
    /// ```rust
    /// 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();
    /// ```
    pub fn with_config(config: PoolConfig<T>) -> Result<Self> {
        let capacity = config.capacity();

        // Allocate storage
        let mut storage = Vec::with_capacity(capacity);
        storage.resize_with(capacity, MaybeUninit::uninit);

        let pool = Self {
            storage: RefCell::new(storage),
            allocator: RefCell::new(StackAllocator::new(capacity)),
            capacity,
            config,
            #[cfg(feature = "stats")]
            stats: RefCell::new(crate::stats::StatisticsCollector::new(capacity)),
            _marker: PhantomData,
        };

        Ok(pool)
    }

    /// Allocates an object from the pool with the given initial value.
    ///
    /// # Examples
    ///
    /// ```rust
    /// 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.
    #[inline]
    pub fn allocate(&self, mut value: T) -> Result<OwnedHandle<'_, T>> {
        // Try to allocate a slot
        let index = self
            .allocator
            .borrow_mut()
            .allocate()
            .ok_or(Error::PoolExhausted {
                capacity: self.capacity,
                allocated: self.capacity,
            })?;

        // Call on_acquire hook before borrowing storage
        value.on_acquire();

        // Combine storage write and stats update to reduce borrows
        {
            let mut storage = self.storage.borrow_mut();
            storage[index].write(value);
        }

        #[cfg(feature = "stats")]
        self.stats.borrow_mut().record_allocation();

        Ok(OwnedHandle::new(self, index))
    }

    /// 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.
    pub fn allocate_batch(
        &self,
        values: alloc::vec::Vec<T>,
    ) -> Result<alloc::vec::Vec<OwnedHandle<'_, T>>> {
        // Check if we have enough capacity upfront
        if values.len() > self.available() {
            return Err(Error::PoolExhausted {
                capacity: self.capacity,
                allocated: self.allocated(),
            });
        }

        let mut handles = alloc::vec::Vec::with_capacity(values.len());

        for value in values {
            // We know these won't fail due to the check above
            match self.allocate(value) {
                Ok(handle) => handles.push(handle),
                Err(e) => {
                    // This shouldn't happen, but if it does, clean up
                    drop(handles);
                    return Err(e);
                }
            }
        }

        Ok(handles)
    }

    /// 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);
    /// };
    /// ```
    #[inline]
    pub fn try_allocate(&self, value: T) -> Option<OwnedHandle<'_, T>> {
        self.allocate(value).ok()
    }

    /// Returns the total capacity of the pool.
    #[inline]
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Returns the number of available (free) slots in the pool.
    #[inline]
    pub fn available(&self) -> usize {
        self.allocator.borrow().available()
    }

    /// Returns the number of currently allocated objects.
    #[inline]
    pub fn allocated(&self) -> usize {
        self.capacity - self.available()
    }

    /// Returns whether the pool is full (no available slots).
    #[inline]
    pub fn is_full(&self) -> bool {
        self.allocator.borrow().is_full()
    }

    /// Returns whether the pool is empty (all slots available).
    #[inline]
    pub fn is_empty(&self) -> bool {
        self.allocator.borrow().is_empty()
    }

    /// Gets a reference to an object at the given index.
    ///
    /// # Safety
    ///
    /// This is internal and should only be called with valid allocated indices.
    #[inline(always)]
    pub(crate) fn get(&self, index: usize) -> &T {
        let storage = self.storage.borrow();
        // Safety: index is valid and initialized by allocate()
        // We extend the lifetime beyond the borrow - safe because pool owns the data
        unsafe {
            let ptr = storage.as_ptr();
            &*ptr.add(index).cast::<T>()
        }
    }

    /// Gets a mutable reference to an object at the given index.
    ///
    /// # Safety
    ///
    /// This is internal and should only be called with valid allocated indices.
    #[inline(always)]
    #[allow(clippy::mut_from_ref)]
    pub(crate) fn get_mut(&self, index: usize) -> &mut T {
        let storage = self.storage.borrow_mut();
        // Safety: index is valid and initialized by allocate()
        // We extend the lifetime beyond the borrow - safe because pool owns the data
        unsafe {
            let ptr = storage.as_ptr() as *mut MaybeUninit<T>;
            &mut *ptr.add(index).cast::<T>()
        }
    }

    /// Returns an object to the pool (called by handle Drop).
    ///
    /// # Safety
    ///
    /// This is internal and should only be called once per allocation.
    pub(crate) fn return_to_pool(&self, index: usize) {
        // Get the value and call on_release
        let mut storage = self.storage.borrow_mut();

        // Safety: index is valid and was initialized
        unsafe {
            let value_ptr = storage[index].as_mut_ptr();
            (*value_ptr).on_release();
            ptr::drop_in_place(value_ptr);
        }

        // Mark the slot as free
        self.allocator.borrow_mut().free(index);

        #[cfg(feature = "stats")]
        self.stats.borrow_mut().record_deallocation();
    }

    /// Get current pool statistics.
    #[cfg(feature = "stats")]
    #[cfg_attr(docsrs, doc(cfg(feature = "stats")))]
    pub fn statistics(&self) -> PoolStatistics {
        let mut stats = self.stats.borrow().snapshot();
        stats.current_usage = self.allocated();
        stats
    }

    /// Reset statistics counters.
    #[cfg(feature = "stats")]
    #[cfg_attr(docsrs, doc(cfg(feature = "stats")))]
    pub fn reset_statistics(&self) {
        self.stats.borrow_mut().reset();
    }
}

impl<T> Drop for FixedPool<T> {
    fn drop(&mut self) {
        // Clean up any remaining allocated objects
        // Note: In this implementation, objects are dropped when handles are dropped
        // The pool itself doesn't need additional cleanup
        let _allocator = self.allocator.borrow();

        // All handles should be dropped before the pool
        // Any remaining objects will be dropped with the storage Vec
    }
}

// Safety: FixedPool is Send if T is Send (storage is behind RefCell)
unsafe impl<T: Send> Send for FixedPool<T> {}

// Note: FixedPool is NOT Sync because it uses RefCell internally
// Use ThreadSafePool for concurrent access

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_pool() {
        let pool = FixedPool::<i32>::new(100).unwrap();
        assert_eq!(pool.capacity(), 100);
        assert_eq!(pool.available(), 100);
        assert_eq!(pool.allocated(), 0);
        assert!(pool.is_empty());
        assert!(!pool.is_full());
    }

    #[test]
    fn allocate_and_drop() {
        let pool = FixedPool::new(10).unwrap();

        {
            let handle = pool.allocate(42).unwrap();
            assert_eq!(*handle, 42);
            assert_eq!(pool.allocated(), 1);
            assert_eq!(pool.available(), 9);
        }

        // After drop, should be returned to pool
        assert_eq!(pool.allocated(), 0);
        assert_eq!(pool.available(), 10);
    }

    #[test]
    fn allocate_until_full() {
        let pool = FixedPool::new(3).unwrap();

        let _h1 = pool.allocate(1).unwrap();
        let _h2 = pool.allocate(2).unwrap();
        let _h3 = pool.allocate(3).unwrap();

        assert!(pool.is_full());

        let result = pool.allocate(4);
        assert!(matches!(result, Err(Error::PoolExhausted { .. })));
    }

    #[test]
    fn reuse_after_free() {
        let pool = FixedPool::new(2).unwrap();

        let h1 = pool.allocate(1).unwrap();
        drop(h1);

        let h2 = pool.allocate(2).unwrap();
        assert_eq!(*h2, 2);
    }

    #[test]
    fn modify_value() {
        let pool = FixedPool::new(10).unwrap();

        let mut handle = pool.allocate(10).unwrap();
        assert_eq!(*handle, 10);

        *handle = 20;
        assert_eq!(*handle, 20);
    }
}