| Crates.io | ibuf |
| lib.rs | ibuf |
| version | 0.3.1 |
| created_at | 2025-04-08 06:02:31.896949+00 |
| updated_at | 2025-04-28 02:16:38.116887+00 |
| description | A memory buffer pool implementation |
| homepage | |
| repository | https://github.com/open1s/ibuf.git |
| max_upload_size | |
| id | 1625107 |
| size | 47,387 |
ibuf is a high-performance message buffer library similar to Linux mbuf, providing thread-safe memory management and efficient data processing capabilities.
Add to Cargo.toml:
[dependencies]
ibuf = { version = "0.2" }
use ibuf::{MBuf, MPool};
// Create a single MBuf
let mut buf = MBuf::with_capacity(1024);
buf.append(b"hello world");
// Use memory pool
let pool = MPool::new(10, 1024); // Initial 10 buffers, each 1024 bytes
let buf = pool.alloc();
// ... Use the buffer
pool.free(buf);
use ibuf::{MBuf, MPool};
// Simulate receiving network packets
fn handle_packet(pool: &MPool, packet_data: &[u8]) {
// Allocate buffer from pool
let mut buf = pool.alloc();
// Write data
buf.append(packet_data);
// Process data...
process_data(&buf);
// Release buffer
pool.free(buf);
}
fn process_data(buf: &MBuf) {
// Zero-copy data access
let data: &[u8] = &buf;
println!("Processing {} bytes of data", data.len());
}
use ibuf::{MBuf, MPool};
// Create memory pool
let pool = MPool::new(100, 4096); // 100 buffers of 4KB each
// Batch allocation
let mut buffers = Vec::new();
for _ in 0..10 {
buffers.push(pool.alloc());
}
// Batch write data
for buf in &mut buffers {
buf.append(b"batch data");
}
// Batch release
for buf in buffers {
pool.free(buf);
}
use ibuf::{MBuf, MPool};
use std::io::{Read, Write};
let pool = MPool::new(5, 1024);
// Simulate data stream
let mut stream = pool.alloc();
stream.write_all(b"stream data part 1").unwrap();
stream.write_all(b"stream data part 2").unwrap();
// Read stream data
let mut read_buf = [0u8; 32];
let bytes_read = stream.read(&mut read_buf).unwrap();
println!("Read {} bytes: {:?}", bytes_read, &read_buf[..bytes_read]);
pool.free(stream);
MIT License