ruapc_bufpool/lib.rs
1//! # ruapc-bufpool
2//!
3//! A high-performance memory pool using buddy memory allocation algorithm for efficient
4//! fixed-size buffer management. This crate is part of the [ruapc](https://github.com/SF-Zhou/ruapc) project.
5//!
6//! ## Features
7//!
8//! - **Buddy Memory Allocation**: Supports allocation of 1MiB, 4MiB, 16MiB, and 64MiB buffers
9//! - **Both Sync and Async APIs**: Designed for tokio environments with async-first design
10//! - **Automatic Memory Reclamation**: Buffers are automatically returned to the pool on drop
11//! - **Memory Limits**: Configurable maximum memory usage with async waiting when limits are reached
12//! - **Custom Allocators**: Pluggable allocator trait for memory allocation backend
13//! - **O(1) Buddy Merging**: Intrusive doubly-linked list with O(1) free/merge operations
14//!
15//! ## Example
16//!
17//! ```rust
18//! use ruapc_bufpool::{BufferPool, BufferPoolBuilder};
19//!
20//! # fn main() -> std::io::Result<()> {
21//! // Create a buffer pool with 256MiB max memory
22//! let pool = BufferPoolBuilder::new()
23//! .max_memory(256 * 1024 * 1024)
24//! .build();
25//!
26//! // Allocate a 1MiB buffer synchronously
27//! let buffer = pool.allocate(1024 * 1024)?;
28//! assert!(buffer.len() >= 1024 * 1024);
29//!
30//! // Buffer is automatically returned to the pool when dropped
31//! drop(buffer);
32//! # Ok(())
33//! # }
34//! ```
35//!
36//! ## Async Example
37//!
38//! ```rust
39//! use ruapc_bufpool::{BufferPool, BufferPoolBuilder};
40//!
41//! # async fn example() -> std::io::Result<()> {
42//! let pool = BufferPoolBuilder::new()
43//! .max_memory(256 * 1024 * 1024)
44//! .build();
45//!
46//! // Allocate asynchronously - will wait if memory limit is reached
47//! let buffer = pool.async_allocate(4 * 1024 * 1024).await?;
48//! # Ok(())
49//! # }
50//! ```
51
52#![warn(missing_docs)]
53#![warn(clippy::all)]
54#![warn(clippy::pedantic)]
55#![deny(unsafe_op_in_unsafe_fn)]
56
57mod allocator;
58mod buddy;
59mod buffer;
60mod intrusive_list;
61mod pool;
62
63pub use allocator::{Allocator, DefaultAllocator};
64pub use buffer::Buffer;
65pub use pool::{BufferPool, BufferPoolBuilder};