Skip to contentSkip to conversation

rust: add a ring buffer implementation

PATCH In review Andreas Hindborg 2 participants 2 messages Nov 5 – Nov 10

Subsystems (2)
lore
Share & tools
lore

Andreas Hindborg

author reviewer
#0 lore raw

PATCH v3 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 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]

To: Danilo Krummrich <[email protected]>
To: Lorenzo Stoakes <[email protected]>
To: Vlastimil Babka <[email protected]>
To: "Liam R. Howlett" <[email protected]>
To: Uladzislau Rezki <[email protected]>
To: Miguel Ojeda <[email protected]>
To: Boqun Feng <[email protected]>
To: Gary Guo <[email protected]>
To: Björn Roy Baron <[email protected]>
To: Benno Lossin <[email protected]>
To: Andreas Hindborg <[email protected]>
To: Alice Ryhl <[email protected]>
To: Trevor Gross <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 rust/kernel/alloc.rs            |   6 +
 rust/kernel/alloc/ringbuffer.rs | 349 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 355 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 +349 -0

349 lines not on this page · raw message

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

Best regards,

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

rust/kernel/alloc/ringbuffer.rs line 6

Quoted 5 lines · Andreas Hindborg
rust/kernel/alloc/ringbuffer.rs new file +4 -0
+//! A fixed-capacity FIFO ring buffer.
+//!
+//! This module provides [`RingBuffer`], a circular buffer implementation that
+//! supports efficient push and pop operations at opposite ends of the buffer.

Please add at least an example here :) Maybe reusing part of the tests.

i.e. this is simple enough that we can use it as a simple showcase of how to write a module with docs, examples, and tests.

(And ideally on each method that may fail showing their Ok/Some and Err/None paths.)

Quoted 1 line

+/// # Invariants

Sashiko seems right: this is missing // INVARIANT:.

Quoted 1 line

+ /// Returns true if the buffer is full.

[true]

Quoted 1 line

+ /// Returns None if the buffer is empty. Values are returned in FIFO

[None]

Cheers,
Miguel