Skip to main content

scirs2_core/memory_pool/
mod.rs

1//! GPU-style memory pool with defragmentation and async allocation.
2//!
3//! This module provides a pure-Rust simulation of GPU memory management patterns,
4//! implementing concepts commonly found in graphics and compute APIs:
5//!
6//! * **Arena allocator** with size-class free lists (see [`arena`])
7//! * **Defragmentation** — both full compaction and incremental (see [`defragmenter`])
8//! * **Buddy allocator** for large power-of-2 allocations (see [`defragmenter::BuddyAllocator`])
9//! * **Async allocation queue** with priority scheduling and pressure callbacks (see [`async_pool`])
10//!
11//! # Quick Start
12//!
13//! ```rust
14//! use scirs2_core::memory_pool::{
15//!     arena::ArenaAllocator,
16//!     types::{PoolConfig, AsyncAllocRequest},
17//!     async_pool::AsyncPool,
18//! };
19//!
20//! // Arena allocator
21//! let mut arena = ArenaAllocator::new(PoolConfig::default());
22//! let id = arena.alloc(1024, 256).expect("alloc failed");
23//! let stats = arena.stats();
24//! println!("used: {} / total: {}", stats.used, stats.total);
25//! arena.free(id).expect("free failed");
26//!
27//! // Async pool with priority queue
28//! let mut pool = AsyncPool::new(PoolConfig::default());
29//! let req = AsyncAllocRequest::new(4096, /* priority */ 10);
30//! let handle = pool.enqueue(req).expect("enqueue failed");
31//! let completed = pool.process_queue(1);
32//! if let Some(alloc_id) = pool.get_result(handle) {
33//!     pool.free(alloc_id).expect("free failed");
34//! }
35//! ```
36
37pub mod arena;
38pub mod async_pool;
39pub mod defragmenter;
40pub mod types;
41
42// Convenient re-exports.
43pub use arena::ArenaAllocator;
44pub use async_pool::AsyncPool;
45pub use defragmenter::{compact, fragmentation_score, incremental_defrag, BuddyAllocator};
46pub use types::{
47    AllocError, AllocationId, AllocationStats, AsyncAllocRequest, BlockState, MemoryBlock,
48    PoolConfig, RequestHandle,
49};