https://github.com/sklose/magic-buffer
Virtual Ring Buffer implementation for Rust
https://github.com/sklose/magic-buffer
allocation networking ring-buffer
Last synced: 6 months ago
JSON representation
Virtual Ring Buffer implementation for Rust
- Host: GitHub
- URL: https://github.com/sklose/magic-buffer
- Owner: sklose
- License: mit
- Created: 2023-06-01T21:03:57.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-14T20:55:10.000Z (10 months ago)
- Last Synced: 2025-04-15T00:12:52.118Z (6 months ago)
- Topics: allocation, networking, ring-buffer
- Language: Rust
- Homepage:
- Size: 58.6 KB
- Stars: 8
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/sklose/magic-buffer/actions/workflows/ci.yml)
[](https://crates.io/crates/magic-buffer)
[](https://docs.rs/magic-buffer)# Magic Buffer
A Magic Ring Buffer (or Virtual Ring Buffer) implementation for Rust. `magic-buffer` provides a simplified
way to deal with buffers that wrap around by delegating that logic to hardware.
The same underlying buffer is mapped twice into memory at adjacent addresses. This allows to wrap around the
buffer while still reading forward in the virtual address space.This behavior is useful for a variety of applications:
- network protocol parsers with a fixed size buffer
- VecDec implementations that can provide a consecutive slice of memory
(e.g. [SliceDeq](https://github.com/gnzlbg/slice_deque))
- IPC ring buffer implementations```toml
[dependencies]
magic-buffer = "0.1"
```## Examples
### Allocating a Buffer
Buffer lens have to be page aligned and follow the allocation
granularity of the operating system```rust
use magic_buffer::*;
let buf = MagicBuffer::new(1 << 16).unwrap();
```| OS | Architecture | Min Buffer Len |
|---------|--------------|----------------|
| Windows | x86_64 | 64 KiB |
| Linux | x86_64 | 4 KiB |
| OSX | x86_64 | 4 KiB |
| OSX | aarch64 | 16 KiB |** PRs welcome to complete this list
### Indexing into a Buffer
```rust
use magic_buffer::*;
let mut buf = MagicBuffer::new(1 << 16).unwrap();
buf[0] = b'1';
buf[1] = b'2';// index wraps around
assert_eq!(buf[0], buf[1 << 16]);
assert_eq!(buf[1], buf[(1 << 16) + 1]);
```### Slices
```rust
use magic_buffer::*;
let buf = MagicBuffer::new(1 << 16).unwrap();// the whole underlying buffer starting at pos 0
let a = &buf[..];// the whole underlying buffer starting at pos 1
// then wrapping around with the first byte at the end
let b = &buf[1..];
```