---
name: "buf_ring"
section: "9"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/buf_ring"
---

# buf\_ring(9)

multi-producer, {single, multi}-consumer lock-less ring buffer

## 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**, **buf\_ring\_drops** – multi-producer, {single, multi}-consumer lock-less ring buffer

## SYNOPSIS

**#include \<sys/param.h\>**

**#include \<sys/buf\_ring.h\>**

*struct* *buf\_ring* *\**\

**buf\_ring\_alloc**(*int count*, *struct malloc\_type \*type*, *int flags*, *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*);

*uint64\_t*\

**buf\_ring\_drops**(*struct buf\_ring \*br*);

## DESCRIPTION

The **buf\_ring** functions provide a lock-less multi-producer and lock-less multi-consumer as well as single-consumer ring buffer.

The **buf\_ring\_alloc**() function is used to allocate a buf\_ring ring buffer with *count* slots using malloc\_type *type* and memory flags *flags*. The single consumer interface is protected by *lock*. Supported lock types are [mutex(9)](/mutex/9.md), [rwlock(9)](/rwlock/9.md), and [rmlock(9)](/rmlock/9.md).

The **buf\_ring\_free**() function is used to free a buf\_ring. The user is responsible for freeing any enqueued items.

The **buf\_ring\_enqueue**() function is used to enqueue a buffer to a buf\_ring.

The **buf\_ring\_dequeue\_mc**() function is a multi-consumer safe way of dequeueing elements from a buf\_ring.

The **buf\_ring\_dequeue\_sc**() function is a single-consumer interface to dequeue elements - requiring the user to serialize accesses with a lock.

The **buf\_ring\_count**() function returns the number of elements in a buf\_ring.

The **buf\_ring\_empty**() function returns TRUE if the buf\_ring is empty, FALSE otherwise.

The **buf\_ring\_full**() function returns TRUE if no more items can be enqueued, FALSE otherwise.

The **buf\_ring\_peek**() function returns a pointer to the last element in the buf\_ring if the buf\_ring is not empty, NULL otherwise.

The **buf\_ring\_drops**() functions returns how many times the **buf\_ring\_enqueue**() was not able to queue an element due to no available slots.

## RETURN VALUES

The **buf\_ring\_enqueue**() function return ENOBUFS if there are no available slots in the buf\_ring.

## HISTORY

These functions were introduced in FreeBSD 8.0.
