BUF_RING(9)
π BUF_RING(9) β the lock-less ring buffer that could π
okay so bestie, listen up, because we're about to get into buf_ring(9) from the FreeBSD Kernel Developer's Manual and honestly? she's iconic. this whole page is about a multi-producer, {single, multi}-consumer lock-less ring buffer, and it's giving main character energy in the kernel. no cap, let's go. π
β¨ NAME β¨
so the squad here is: 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, and buf_ring_peek. they all team up to bring you a multi-producer, {single, multi}-consumer lock-less ring buffer. periodt. π
πΈ SYNOPSIS πΈ
here's the vibe code-wise, bestie. copy it exactly, no edits:
#include <sys/param.h>
#include <sys/buf_ring.h>
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 serve up a lock-less multi-producer and lock-less multi-consumer as well as single-consumer ring buffer. that's the whole vibe. β¨
here's the breakdown, one by one, because we don't skip:
buf_ring_alloc()β this one allocates abuf_ringring buffer withcountslots, using the malloc typetypeand memory flagsflags. and plot twist: the single consumer interface is protected bysc_lock. πbuf_ring_free()β frees abuf_ring. β οΈ but bestie, listen: the user is responsible for freeing any enqueued items. don't just free the ring and ghost the contents, that's not cute. πbuf_ring_enqueue()β enqueues a buffer onto abuf_ring. simple as that, slay. πbuf_ring_dequeue_mc()β the multi-consumer safe way to dequeue elements from abuf_ring. safe for the whole group chat, no drama. π«Άbuf_ring_dequeue_sc()β the single-consumer interface to dequeue elements, and this one requires the user to serialize accesses with a lock. no cap, you gotta lock it yourself. πbuf_ring_count()β returns the number of elements in abuf_ring. the headcount, basically. π’buf_ring_empty()β returnsTRUEif thebuf_ringis empty,FALSEotherwise. is the party over? check here. πbuf_ring_full()β returnsTRUEif no more items can be enqueued,FALSEotherwise. the velvet rope is up, bestie. π«buf_ring_peek()β returns a pointer to the last element in thebuf_ringif thebuf_ringis not empty, andNULLotherwise. just a lil look, no dequeue. π
π RETURN VALUES π
buf_ring_enqueue() returns ENOBUFS if there are no available slots in the buf_ring. that's it, that's the tea. β
π HISTORY π
these functions were introduced in FreeBSD 8.0. a classic, an icon, a legend. β
okay bestie, that's the whole page. stay lock-less, stay iconic, and remember β always free your enqueued items. π
<div align="right">β with love from the Debian fam, dated September 27, 2012, signing off as BUF_RING(9) πβ¨</div>
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright (c) 2009 Bitgravity Inc
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms