Re: [PATCH v4] rust: add a ring buffer implementation
From: Danilo Krummrich
Date: Tue Dec 01 2026 - 11:42:29 EST
On Thu Nov 26, 2026 at 1:53 PM CET, Andreas Hindborg wrote:
> 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.
>
> Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
NIT: Please add the "alloc:" prefix to the patch subject.
> + pub fn new(capacity: usize, flags: Flags) -> Result<Self> {
> + let slots = capacity.checked_add(1).ok_or(ENOMEM)?;
> + let mut nodes = Vec::with_capacity(slots, flags)?;
> +
> + for _ in 0..nodes.capacity() {
I think this should be 0..slots. When Option<T> is zero-sized, nodes.capacity()
returns usize::MAX, so the ring reports usize::MAX - 1 free slots instead of the
requested capacity.
> + nodes.push_within_capacity(None)?;
> + }
> +
> + // INVARIANT: `nodes` holds `nodes.capacity() >= slots >= 1` slots, all
> + // of which are vacant. Thus:
> + // - `head` points at a vacant slot.
> + // - `tail` need not point at an occupied slot, since the buffer is
> + // empty.
> + // - The buffer is empty and `head == tail`.
> + // - The buffer has at least one vacant slot.
> + Ok(Self {
> + nodes,
> + head: 0,
> + tail: 0,
> + })
> + }
[snip]
> +#[kunit_tests(rust_kernel_ringbuffer)]
> +mod tests {
This needs a separate Kconfig.
I did consider fixing this up on my end, but didn't want to make too many
changes without having it on the list.
I'm happy to still pick it up if you want to provide a quick diff (or simply a
v5).
Thanks,
Danilo