BUF_RING(9)
π BUF_RING π
okay bestie, welcome to the buf_ring page from the Kernel Developer's Manual (that's section 9, kernel dev territory, we're in the big leagues now π). this is the lock-less ring buffer situation and honestly? it's giving main character energy. let's get into it π¦
β¨ NAME β¨
buf_ring, buf_ring_alloc, buf_ring_free, buf_ring_enqueue, buf_ring_dequeue_mc, buf_ring_dequeue_sc, buf_ring_count, buf_ring_empty, buf_ring_full, buf_ring_peek β the multi-producer, {single, multi}-consumer lock-less ring buffer π
β¨ SYNOPSIS β¨
first things first, you gotta pull in the headers, no cap:
#include <sys/param.h>
#include <sys/buf_ring.h>
and here's the whole squad of function signatures, character for character, don't come for me:
struct buf_ring *
buf_ring_alloc(int count, struct malloc_type *type, int flags,
struct mtx *sc_lock);
void
buf_ring_free(struct buf_ring *br, struct malloc_type *type);
int
buf_ring_enqueue(struct buf_ring *br, void *buf);
void *
buf_ring_dequeue_mc(struct buf_ring *br);
void *
buf_ring_dequeue_sc(struct buf_ring *br);
int
buf_ring_count(struct buf_ring *br);
int
buf_ring_empty(struct buf_ring *br);
int
buf_ring_full(struct buf_ring *br);
void *
buf_ring_peek(struct buf_ring *br);
β¨ DESCRIPTION β¨
so the buf_ring functions hand you a lock-less multi-producer and lock-less multi-consumer ring buffer, plus a single-consumer one too. yes, lock-less. yes, that's iconic. π
the functions, one by one π
buf_ring_alloc()β this is the one that actually makes the ring buffer happen. it allocates abuf_ringwithcountslots, using malloc typetypeand memory flagsflags. and listen: the single consumer interface is protected bysc_lock. protect it, bestie π«Άbuf_ring_free()β frees abuf_ring. important though: you, the user, are responsible for freeing any enqueued items. it will not do that for you. it's not your mom, it's not your bestie, it's a free function. clean up after yourself fr fr πbuf_ring_enqueue()β enqueues a buffer onto abuf_ring. in it goes πbuf_ring_dequeue_mc()β the multi-consumer safe way to dequeue elements from abuf_ring. multiple consumers? no problem, she's built different.buf_ring_dequeue_sc()β the single-consumer interface for dequeueing elements. β οΈ catch: it requires you to serialize accesses with a lock. yes you. with a lock. don't skip this part πbuf_ring_count()β returns the number of elements currently chilling in thebuf_ring.buf_ring_empty()β returnsTRUEif thebuf_ringis empty,FALSEotherwise. vibe check: is it empty?buf_ring_full()β returnsTRUEif no more items can be enqueued,FALSEotherwise. she's full, she can't take anymore πbuf_ring_peek()β returns a pointer to the last element in thebuf_ringif thebuf_ringis not empty, andNULLotherwise. just a lil peek, no dequeue, no strings attached π
β¨ RETURN VALUES β¨
buf_ring_enqueue()returnsENOBUFSif there are no available slots in thebuf_ring. that's it, that's the whole list. no room at the inn, bestie π«
β¨ HISTORY β¨
these functions were introduced in FreeBSD 8.0. vintage, but make it lock-less. β
okay that's the whole page, you ate that. go forth and ring buffer responsibly π
sign-off time: this lil manual page comes from Debian, dated September 27, 2012 β so yeah, she's been around a minute, and she still slays. πβ¨
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2009 Bitgravity Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms