| Crates.io | ruapc-bufpool |
| lib.rs | ruapc-bufpool |
| version | 0.1.0 |
| created_at | 2026-01-22 12:03:17.523588+00 |
| updated_at | 2026-01-22 12:03:17.523588+00 |
| description | A high-performance memory pool using buddy memory allocation algorithm for efficient fixed-size buffer management |
| homepage | |
| repository | https://github.com/SF-Zhou/ruapc-bufpool |
| max_upload_size | |
| id | 2061420 |
| size | 100,573 |
A high-performance memory pool using the buddy memory allocation algorithm for efficient fixed-size buffer management. This crate is part of the ruapc project.
tokio::sync::MutexBuffer type that implements Deref<Target = [u8]>Add this to your Cargo.toml:
[dependencies]
ruapc-bufpool = "0.1"
use ruapc_bufpool::BufferPoolBuilder;
fn main() -> std::io::Result<()> {
// Create a buffer pool with 256 MiB max memory (default)
// Returns Arc<BufferPool> for safe sharing across threads
let pool = BufferPoolBuilder::new()
.max_memory(256 * 1024 * 1024)
.build();
// Allocate a 1 MiB buffer
let mut buffer = pool.allocate(1024 * 1024)?;
// Write to the buffer
buffer[0] = 42;
buffer[1] = 43;
// Read from the buffer
assert_eq!(buffer[0], 42);
// Buffer is automatically returned to the pool when dropped
drop(buffer);
Ok(())
}
use ruapc_bufpool::BufferPoolBuilder;
#[tokio::main]
async fn main() -> std::io::Result<()> {
let pool = BufferPoolBuilder::new()
.max_memory(128 * 1024 * 1024)
.build();
// Allocate asynchronously - will wait if memory limit is reached
let buffer = pool.async_allocate(4 * 1024 * 1024).await?;
assert!(buffer.len() >= 4 * 1024 * 1024);
Ok(())
}
use ruapc_bufpool::{Allocator, BufferPoolBuilder};
use std::io::Result;
struct MyAllocator;
impl Allocator for MyAllocator {
fn allocate(&self, size: usize) -> Result<*mut u8> {
// Custom allocation logic
// ...
# unimplemented!()
}
unsafe fn deallocate(&self, ptr: *mut u8, size: usize) {
// Custom deallocation logic
// ...
}
}
fn main() {
let pool = BufferPoolBuilder::new()
.allocator(Box::new(MyAllocator))
.build();
}
The pool uses a buddy memory allocation algorithm with 4 levels:
| Level | Size | Blocks per 64 MiB |
|---|---|---|
| 0 | 1 MiB | 64 |
| 1 | 4 MiB | 16 |
| 2 | 16 MiB | 4 |
| 3 | 64 MiB | 1 |
When allocating:
When freeing:
Arc<BufferPool> reference, ensuring pool validitytokio::sync::Mutex for synchronizationblocking_lock()lock().awaitThe Buffer struct is optimized for minimal memory footprint (32 bytes):
ptr: 8 bytes (pointer to memory)pool: 8 bytes (Arc reference to pool)block: 8 bytes (pointer to buddy block)level: 1 byte (0-3, only 4 possible values)index: 1 byte (0-63, max index at level 0)See the documentation on docs.rs for detailed API reference.
Licensed under either of:
at your option.
Contributions are welcome! Please feel free to submit a Pull Request.