Skip to contentSkip to conversation

rust: add a ring buffer implementation

PATCH New Andreas Hindborg 1 participant 1 message Nov 26

Subsystems (2)
lore
Share & tools
lore

Andreas Hindborg

author reviewer
#0 lore raw

PATCH v4 1/1 rust: add a ring buffer implementation

Add a fixed-capacity FIFO ring buffer. The implementation uses a circular buffer with head and tail pointers, providing constant-time push and pop operations.

The module includes a few tests covering basic operations, wrap-around behavior, interleaved push/pop sequences, and edge cases such as single-capacity buffers.

---
Changes in v4:
- Add an // INVARIANT: comment to RingBuffer::new and populate the slots before constructing the buffer (Sashiko, Miguel).
- Add // INVARIANT: comments to push_head and pop_tail.
- Describe slots as occupied and vacant instead of full and empty.
- Add a module-level example (Miguel).
- Add examples to new, push_head and pop_tail that show both the success and the failure path (Miguel).
- Use intra-doc links for true and None (Miguel).
- Link to v3: https://msgid.link/[email protected]

Changes in v3:
- Lay out the imports vertically, as the coding guidelines require (Onur).
- Mark the trivial accessors #[inline] (Onur).
- Rebase on v7.4-rc1.
- Link to v2: https://msgid.link/[email protected]

Changes in v2:
- Move the module into rust/kernel/alloc/, next to Vec (Alice).
- Drop the redundant size field; use nodes.len() instead (Alice).
- Return ENOMEM when capacity + 1 overflows usize instead of panicking inside with_capacity (Alice).
- Simplify the head < tail branch of free_count to tail - head (Alice).
- Remove the manual Drop impl; the backing KVec already drops the stored slots (Alice).
- Make RingBuffer generic over the backing allocator. Add KRingBuffer, VRingBuffer and KVRingBuffer aliases and re-export them from kernel::alloc. The constructor now takes an additional Flags argument (Daniel, Danilo).
- Link to v1: https://msgid.link/[email protected]

---
 rust/kernel/alloc.rs            |   6 +
 rust/kernel/alloc/ringbuffer.rs | 452 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 458 insertions(+)
rust/kernel/alloc.rs +6 -0
index 68d80f3fa6c8..c27eda267317 100644
@@ -6,6 +6,7 @@
 pub mod kbox;
 pub mod kvec;
 pub mod layout;
+pub mod ringbuffer;
 
 pub use self::kbox::Box;
 pub use self::kbox::KBox;
@@ -18,6 +19,11 @@
 pub use self::kvec::VVec;
 pub use self::kvec::Vec;
 
+pub use self::ringbuffer::KRingBuffer;
+pub use self::ringbuffer::KVRingBuffer;
+pub use self::ringbuffer::RingBuffer;
+pub use self::ringbuffer::VRingBuffer;
+
 /// Indicates an allocation error.
 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
 pub struct AllocError;
rust/kernel/alloc/ringbuffer.rs new file +452 -0

452 lines not on this page · raw message

---
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
change-id: 20260215-ringbuffer-42455964aaf2

Best regards,

Signature · 1 line
Andreas Hindborg <[email protected]>