ibuf - High-performance Message Buffer Library
ibuf is a high-performance message buffer library similar to Linux mbuf, providing thread-safe memory management and efficient data processing capabilities.
Features
- MBuf: High-performance message buffer with dynamic expansion
- MPool: Thread-safe memory pool implementation to reduce memory allocation overhead
- Zero-copy: Supports direct access to underlying data, avoiding unnecessary memory copies
- Thread-safe: All core operations are thread-safe
Quick Start
Add Dependency
Add to Cargo.toml:
[]
= { = "https://github.com/your-repo/ibuf.git" }
Basic Usage
use ;
// Create a single MBuf
let mut buf = with_capacity;
buf.append;
// Use memory pool
let pool = new; // Initial 10 buffers, each 1024 bytes
let buf = pool.alloc;
// ... Use the buffer
pool.free;
Advanced Usage Examples
Network Data Processing
use ;
// Simulate receiving network packets
Batch Operations
use ;
// Create memory pool
let pool = new; // 100 buffers of 4KB each
// Batch allocation
let mut buffers = Vecnew;
for _ in 0..10
// Batch write data
for buf in &mut buffers
// Batch release
for buf in buffers
Stream Processing
use ;
use ;
let pool = new;
// Simulate data stream
let mut stream = pool.alloc;
stream.write_all.unwrap;
stream.write_all.unwrap;
// Read stream data
let mut read_buf = ;
let bytes_read = stream.read.unwrap;
println!;
pool.free;
Core Components
MBuf
- Dynamic expansion: Automatically grows by 1.5x when space is insufficient
- Reference counting: Supports multi-thread sharing
- Zero-copy access: Direct access to underlying data via Deref
MPool
- Thread-safe: Implemented with Arc+Mutex
- Statistics: Can query allocated and free buffer counts
- Auto-expansion: Creates new buffers when pool is empty
Performance Recommendations
- Prefer MPool for frequent allocation/release scenarios
- Estimate initial capacity to avoid frequent expansions
- Consider zero-copy access for large data operations
License
MIT License