Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
ring_buffer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Nicolas Pitre <npitre@baylibre.com>
3 * 2026 Måns Ansgariusson <mansgariusson@gmail.com>
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef ZEPHYR_INCLUDE_SYS_RING_BUFFER_H_
9#define ZEPHYR_INCLUDE_SYS_RING_BUFFER_H_
10
11#include <errno.h>
12#include <stdbool.h>
13#include <stdint.h>
14#include <string.h>
15
16#include <zephyr/sys/util.h>
17#include <zephyr/toolchain.h>
18#include <zephyr/sys/__assert.h>
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
42
44#ifdef CONFIG_RING_BUFFER_LARGE
45typedef uint32_t ring_buf_idx_t;
46typedef uint32_t ring_buf_size_t;
47/* read_idx/write_idx + n must not overflow size_t on 32-bit targets (index < 2N, n <= N) */
48#define RING_BUFFER_MAX_SIZE (UINT32_MAX / 4U)
49#define RING_BUFFER_SIZE_ASSERT_MSG "Size too large"
50#else
51typedef uint16_t ring_buf_idx_t;
52typedef uint16_t ring_buf_size_t;
53/* read_idx/write_idx live in [0, 2N) */
54#define RING_BUFFER_MAX_SIZE (UINT16_MAX / 2U)
55#define RING_BUFFER_SIZE_ASSERT_MSG \
56 "Size too large, please enable CONFIG_RING_BUFFER_LARGE"
57#endif
59
68struct ring_buf {
70 uint8_t *buffer;
71 ring_buf_size_t size;
72 ring_buf_idx_t read_idx;
73 ring_buf_idx_t write_idx;
74#ifdef CONFIG_RING_BUFFER
75 ring_buf_idx_t put_claimed;
76 ring_buf_idx_t get_claimed;
77#endif /* CONFIG_RING_BUFFER */
79};
80
82
83/*
84 * Advance a free-running index into [0, 2N) and convert it to the index type.
85 * Takes the buffer capacity N and forms the 2N wrap limit internally, so the
86 * [0, 2N) convention stays in one place. Callers guarantee value < 4N (index
87 * < 2N plus an increment <= N), so a single conditional subtraction suffices.
88 */
89static ALWAYS_INLINE ring_buf_idx_t rb_idx_advance(size_t value, ring_buf_size_t size)
90{
91 size_t lim = 2U * size;
92
93 return (ring_buf_idx_t)(value >= lim ? value - lim : value);
94}
95
96/*
97 * Index accessors for the single-producer/single-consumer protocol.
98 *
99 * On SMP, the index of the opposite side is read with an acquire load and the
100 * own index is published with a release store, which orders the data accesses
101 * across CPUs.
102 *
103 * On a single CPU, producer and consumer (thread/thread or thread/ISR) observe
104 * each other's accesses in program order, so only the compiler must be kept
105 * from reordering, caching or tearing the accesses. This is done like
106 * READ_ONCE()/WRITE_ONCE() in Linux: a volatile access combined with a
107 * compiler barrier.
108 *
109 * rb_load_relaxed() is only used for the index that the calling side itself
110 * owns (write_idx in the producer, read_idx in the consumer). No other context
111 * writes it, so a plain load is sufficient.
112 */
113static ALWAYS_INLINE ring_buf_idx_t rb_load_acquire(const ring_buf_idx_t *p)
114{
115 ring_buf_idx_t v;
116
117#if IS_ENABLED(CONFIG_SMP) && (CONFIG_MP_MAX_NUM_CPUS > 1)
118 v = __atomic_load_n(p, __ATOMIC_ACQUIRE);
119#else
120 v = *(const volatile ring_buf_idx_t *)p;
121 compiler_barrier();
122#endif
123 return v;
124}
125
126static ALWAYS_INLINE ring_buf_idx_t rb_load_relaxed(const ring_buf_idx_t *p)
127{
128#if IS_ENABLED(CONFIG_SMP) && (CONFIG_MP_MAX_NUM_CPUS > 1)
129 return __atomic_load_n(p, __ATOMIC_RELAXED);
130#else
131 return *p;
132#endif
133}
134
135static ALWAYS_INLINE void rb_store_release(ring_buf_idx_t *p, ring_buf_idx_t v)
136{
137#if IS_ENABLED(CONFIG_SMP) && (CONFIG_MP_MAX_NUM_CPUS > 1)
138 __atomic_store_n(p, v, __ATOMIC_RELEASE);
139#else
140 compiler_barrier();
141 *(volatile ring_buf_idx_t *)p = v;
142#endif
143}
144
145static ALWAYS_INLINE void rb_store_relaxed(ring_buf_idx_t *p, ring_buf_idx_t v)
146{
147#if IS_ENABLED(CONFIG_SMP) && (CONFIG_MP_MAX_NUM_CPUS > 1)
148 __atomic_store_n(p, v, __ATOMIC_RELAXED);
149#else
150 *p = v;
151#endif
152}
153
155
165#define RING_BUF_INIT(buf, sz) \
166{ \
167 .buffer = (buf), \
168 .size = (ring_buf_size_t)(sz), \
169}
170
186#define RING_BUF_DECLARE(name, size8) \
187 BUILD_ASSERT((size8) <= RING_BUFFER_MAX_SIZE, RING_BUFFER_SIZE_ASSERT_MSG); \
188 static uint8_t __noinit _ring_buffer_data_##name[(size8)]; \
189 struct ring_buf name = RING_BUF_INIT(_ring_buffer_data_##name, (size8))
190
198static inline uint32_t ring_buf_capacity_get(const struct ring_buf *rb)
199{
200 return rb->size;
201}
202
210static inline uint32_t ring_buf_size_get(const struct ring_buf *rb)
211{
212 ring_buf_idx_t write_idx = rb_load_acquire(&rb->write_idx);
213 ring_buf_idx_t read_idx = rb_load_acquire(&rb->read_idx);
214 ring_buf_size_t occ = write_idx - read_idx;
215
216 if (write_idx < read_idx) {
217 occ += 2U * rb->size;
218 }
219 return occ;
220}
221
229static inline uint32_t ring_buf_space_get(const struct ring_buf *rb)
230{
232}
233
241static inline bool ring_buf_is_empty(const struct ring_buf *rb)
242{
243 ring_buf_idx_t write_idx = rb_load_acquire(&rb->write_idx);
244 ring_buf_idx_t read_idx = rb_load_acquire(&rb->read_idx);
245
246 return write_idx == read_idx;
247}
248
256static inline bool ring_buf_is_full(const struct ring_buf *rb)
257{
259}
260
266static inline void ring_buf_reset(struct ring_buf *rb)
267{
268 rb_store_relaxed(&rb->read_idx, 0);
269 rb_store_relaxed(&rb->write_idx, 0);
270#ifdef CONFIG_RING_BUFFER
271 rb->put_claimed = 0;
272 rb->get_claimed = 0;
273#endif /* CONFIG_RING_BUFFER */
274}
275
288static inline void ring_buf_init(struct ring_buf *rb, uint32_t size, uint8_t *data)
289{
290 __ASSERT(size <= RING_BUFFER_MAX_SIZE, RING_BUFFER_SIZE_ASSERT_MSG);
291
292 rb->size = (ring_buf_size_t)size;
293 rb->buffer = data;
294 ring_buf_reset(rb);
295}
296
314static inline uint32_t ring_buf_put_ptr(struct ring_buf *rb, uint8_t **data, size_t offset)
315{
316 ring_buf_idx_t write_idx = rb_load_relaxed(&rb->write_idx);
317 ring_buf_idx_t read_idx = rb_load_acquire(&rb->read_idx);
318 ring_buf_idx_t off, avail;
319
320 __ASSERT_NO_MSG(offset <= ring_buf_space_get(rb));
321 if (offset > 0) {
322 write_idx = rb_idx_advance((size_t)write_idx + offset, rb->size);
323 }
324
325 if (write_idx >= rb->size) {
326 off = write_idx - rb->size;
327 avail = (read_idx >= rb->size ? rb->size : read_idx) - off;
328 } else {
329 off = write_idx;
330 avail = (read_idx >= rb->size ? read_idx - rb->size : rb->size) - off;
331 }
332 *data = &rb->buffer[off];
333 return avail;
334}
335
345static inline void ring_buf_commit(struct ring_buf *rb, size_t size)
346{
347 size_t write_idx = rb_load_relaxed(&rb->write_idx);
348
349 __ASSERT_NO_MSG(size <= ring_buf_space_get(rb));
350 rb_store_release(&rb->write_idx, rb_idx_advance(write_idx + size, rb->size));
351}
352
370static inline uint32_t ring_buf_get_ptr(struct ring_buf *rb, uint8_t **data, size_t offset)
371{
372 ring_buf_idx_t read_idx = rb_load_relaxed(&rb->read_idx);
373 ring_buf_idx_t write_idx = rb_load_acquire(&rb->write_idx);
374 ring_buf_idx_t off, avail;
375
376 __ASSERT_NO_MSG(offset <= ring_buf_size_get(rb));
377 if (offset > 0) {
378 read_idx = rb_idx_advance((size_t)read_idx + offset, rb->size);
379 }
380
381 if (read_idx >= rb->size) {
382 off = read_idx - rb->size;
383 avail = (write_idx >= rb->size ? write_idx : 2U * rb->size) - read_idx;
384 } else {
385 off = read_idx;
386 avail = (write_idx >= rb->size ? rb->size : write_idx) - read_idx;
387 }
388 *data = &rb->buffer[off];
389 return avail;
390}
391
401static inline void ring_buf_consume(struct ring_buf *rb, size_t size)
402{
403 size_t read_idx = rb_load_relaxed(&rb->read_idx);
404
405 __ASSERT_NO_MSG(size <= ring_buf_size_get(rb));
406 rb_store_release(&rb->read_idx, rb_idx_advance(read_idx + size, rb->size));
407}
408
418static inline uint32_t ring_buf_put(struct ring_buf *rb, const uint8_t *data, uint32_t size)
419{
420 uint8_t *dst;
421 uint32_t total = 0;
422 uint32_t chunk;
423 uint32_t avail;
424
425 do {
426 avail = ring_buf_put_ptr(rb, &dst, total);
427 chunk = MIN(avail, size - total);
428 if (chunk == 0U) {
429 break;
430 }
431 memcpy(dst, &data[total], chunk);
432 total += chunk;
433 } while (total < size);
434 ring_buf_commit(rb, total);
435
436 return total;
437}
438
448static inline uint32_t ring_buf_get(struct ring_buf *rb, uint8_t *data, uint32_t size)
449{
450 uint8_t *src;
451 uint32_t chunk;
452 uint32_t avail;
453 uint32_t total = 0;
454
455 do {
456 avail = ring_buf_get_ptr(rb, &src, total);
457 chunk = MIN(avail, size - total);
458 if (chunk == 0U) {
459 break;
460 }
461 if (!IS_ENABLED(CONFIG_RING_BUFFER) || data != NULL) {
462 memcpy(&data[total], src, chunk);
463 }
464 total += chunk;
465 } while (total < size);
466 ring_buf_consume(rb, total);
467
468 return total;
469}
470
483static inline uint32_t ring_buf_peek(const struct ring_buf *rb, uint8_t *data, uint32_t size)
484{
485 uint8_t *src;
486 uint32_t chunk;
487 uint32_t avail;
488 uint32_t total = 0;
489
490 do {
491 avail = ring_buf_get_ptr((struct ring_buf *)rb, &src, total);
492 chunk = MIN(avail, size - total);
493 if (chunk == 0U) {
494 break;
495 }
496 memcpy(&data[total], src, chunk);
497 total += chunk;
498 } while (total < size);
499
500 return total;
501}
502
503#ifdef CONFIG_RING_BUFFER
504
520#define RING_BUF_ITEM_DECLARE(name, size32) \
521 BUILD_ASSERT((size32) <= RING_BUFFER_MAX_SIZE / 4, RING_BUFFER_SIZE_ASSERT_MSG); \
522 static uint32_t __noinit _ring_buffer_data_##name[(size32)]; \
523 struct ring_buf name = RING_BUF_INIT((uint8_t *)_ring_buffer_data_##name, 4 * (size32))
524
534#define RING_BUF_ITEM_DECLARE_SIZE(name, size32) \
535 RING_BUF_ITEM_DECLARE(name, (size32))
536
547#define RING_BUF_ITEM_DECLARE_POW2(name, pow) \
548 RING_BUF_ITEM_DECLARE(name, BIT(pow))
549
558#define RING_BUF_ITEM_SIZEOF(expr) DIV_ROUND_UP(sizeof(expr), sizeof(uint32_t))
559
573__deprecated /* use #include <zephyr/sys/ringq.h> instead */
574static inline void ring_buf_item_init(struct ring_buf *rb, uint32_t size, uint32_t *data)
575{
576 __ASSERT(size <= RING_BUFFER_MAX_SIZE / 4, RING_BUFFER_SIZE_ASSERT_MSG);
577 ring_buf_init(rb, 4 * size, (uint8_t *)data);
578}
579
587__deprecated /* use #include <zephyr/sys/ringq.h> instead */
588static inline uint32_t ring_buf_item_space_get(const struct ring_buf *rb)
589{
590 return ring_buf_space_get(rb) / 4;
591}
592
617__deprecated /* use ring_buf_put_ptr(...) & ring_buf_commit(...) instead */
618static inline uint32_t ring_buf_put_claim(struct ring_buf *rb, uint8_t **data, uint32_t size)
619{
620 uint32_t claimed = MIN(ring_buf_put_ptr(rb, data, rb->put_claimed), size);
621
622 rb->put_claimed += claimed;
623 return claimed;
624}
625
648__deprecated /* use ring_buf_put_ptr(...) & ring_buf_commit(...) instead */
649static inline int ring_buf_put_finish(struct ring_buf *rb, uint32_t size)
650{
651 if (rb->put_claimed < size) {
652 return -EINVAL;
653 }
654 ring_buf_commit(rb, size);
655 rb->put_claimed = 0;
656 return 0;
657}
658
659
684__deprecated /* use ring_buf_get_ptr(...) & ring_buf_consume(...) instead */
685static inline uint32_t ring_buf_get_claim(struct ring_buf *rb, uint8_t **data, uint32_t size)
686{
687 uint32_t claimed = MIN(ring_buf_get_ptr(rb, data, rb->get_claimed), size);
688
689 rb->get_claimed += claimed;
690 return claimed;
691}
692
715__deprecated /* use ring_buf_get_ptr(...) & ring_buf_consume(...) instead */
716static inline int ring_buf_get_finish(struct ring_buf *rb, uint32_t size)
717{
718 if (rb->get_claimed < size) {
719 return -EINVAL;
720 }
721
722 ring_buf_consume(rb, size);
723 rb->get_claimed = 0;
724 return 0;
725}
726
728struct ring_element {
729 uint32_t type : 16;
730 uint32_t length : 8;
731 uint32_t value : 8;
732};
733
739static inline void z_rb_write_no_commit(struct ring_buf *rb, const uint8_t *data,
740 uint32_t size, uint32_t offset)
741{
742 uint32_t avail;
743 uint32_t chunk;
744 uint8_t *dst;
745 uint32_t off = 0;
746
747 while (off < size) {
748 avail = ring_buf_put_ptr(rb, &dst, offset + off);
749 chunk = MIN(avail, size - off);
750
751 memcpy(dst, &data[off], chunk);
752 off += chunk;
753 }
754}
755
757
779__deprecated /* use #include <zephyr/sys/ringq.h> instead */
780static inline int ring_buf_item_put(struct ring_buf *buf, uint16_t type, uint8_t value,
781 uint32_t *data, uint8_t size32)
782{
783 struct ring_element header;
784 uint32_t size = size32 * 4;
785
786 if (size + sizeof(header) > ring_buf_space_get(buf)) {
787 return -EMSGSIZE;
788 }
789
790 header.type = type;
791 header.length = size32;
792 header.value = value;
793 z_rb_write_no_commit(buf, (const uint8_t *)&header, sizeof(header), 0);
794 z_rb_write_no_commit(buf, (const uint8_t *)data, size, sizeof(header));
795 ring_buf_commit(buf, sizeof(header) + size);
796 return 0;
797}
798
823__deprecated /* use #include <zephyr/sys/ringq.h> instead */
824static inline int ring_buf_item_get(struct ring_buf *buf, uint16_t *type, uint8_t *value,
825 uint32_t *data, uint8_t *size32)
826{
827 struct ring_element header;
828
829 if (ring_buf_is_empty(buf)) {
830 return -EAGAIN;
831 }
832
833 ring_buf_peek(buf, (uint8_t *)&header, sizeof(header));
834 if (data != NULL && (header.length > *size32)) {
835 *size32 = header.length;
836 return -EMSGSIZE;
837 }
838
839 *size32 = header.length;
840 *type = header.type;
841 *value = header.value;
842
843 ring_buf_consume(buf, sizeof(header));
844 ring_buf_get(buf, (uint8_t *)data, header.length * 4);
845 return 0;
846}
847
849
858static inline void ring_buf_internal_reset(struct ring_buf *rb, ring_buf_idx_t value)
859{
860 ring_buf_size_t lim = 2U * rb->size;
861
862 value = (lim != 0U) ? (ring_buf_idx_t)(value % lim) : 0U;
863 rb_store_relaxed(&rb->read_idx, value);
864 rb_store_relaxed(&rb->write_idx, value);
865 rb->put_claimed = 0;
866 rb->get_claimed = 0;
867}
868
870
871#else /* CONFIG_RING_BUFFER */
872
874#define Z_RING_BUF_CLAIM_REMOVED(name) \
875 ({ \
876 BUILD_ASSERT(0, #name "() is deprecated and not available " \
877 "when CONFIG_RING_BUFFER=n. Enable " \
878 "CONFIG_RING_BUFFER to keep it during the " \
879 "deprecation period, or migrate as described in the " \
880 "Zephyr 4.5 migration guide."); \
881 0; \
882 })
883
884#define Z_RING_BUF_DECLARE_REMOVED(name) \
885 BUILD_ASSERT(0, #name " is deprecated and not available when " \
886 "CONFIG_RING_BUFFER=n. Enable CONFIG_RING_BUFFER " \
887 "to keep it during the deprecation period, or migrate as " \
888 "described in the Zephyr 4.5 migration guide.")
889
890#define ring_buf_put_claim(...) Z_RING_BUF_CLAIM_REMOVED(ring_buf_put_claim)
891#define ring_buf_put_finish(...) Z_RING_BUF_CLAIM_REMOVED(ring_buf_put_finish)
892#define ring_buf_get_claim(...) Z_RING_BUF_CLAIM_REMOVED(ring_buf_get_claim)
893#define ring_buf_get_finish(...) Z_RING_BUF_CLAIM_REMOVED(ring_buf_get_finish)
894#define ring_buf_item_init(...) Z_RING_BUF_CLAIM_REMOVED(ring_buf_item_init)
895#define ring_buf_item_put(...) Z_RING_BUF_CLAIM_REMOVED(ring_buf_item_put)
896#define ring_buf_item_get(...) Z_RING_BUF_CLAIM_REMOVED(ring_buf_item_get)
897#define ring_buf_item_space_get(...) Z_RING_BUF_CLAIM_REMOVED(ring_buf_item_space_get)
898#define ring_buf_internal_reset(...) Z_RING_BUF_CLAIM_REMOVED(ring_buf_internal_reset)
899
900#define RING_BUF_ITEM_DECLARE(...) Z_RING_BUF_DECLARE_REMOVED(RING_BUF_ITEM_DECLARE)
901#define RING_BUF_ITEM_DECLARE_SIZE(...) Z_RING_BUF_DECLARE_REMOVED(RING_BUF_ITEM_DECLARE_SIZE)
902#define RING_BUF_ITEM_DECLARE_POW2(...) Z_RING_BUF_DECLARE_REMOVED(RING_BUF_ITEM_DECLARE_POW2)
904#endif /* CONFIG_RING_BUFFER */
905
909
910#ifdef __cplusplus
911}
912#endif
913
914#endif /* ZEPHYR_INCLUDE_SYS_RING_BUFFER_H_ */
System error numbers.
static uint32_t ring_buf_capacity_get(const struct ring_buf *rb)
Return ring buffer capacity.
Definition ring_buffer.h:198
static bool ring_buf_is_empty(const struct ring_buf *rb)
Determine if a ring buffer is empty.
Definition ring_buffer.h:241
static uint32_t ring_buf_size_get(const struct ring_buf *rb)
Determine size of available data in a ring buffer.
Definition ring_buffer.h:210
static uint32_t ring_buf_space_get(const struct ring_buf *rb)
Determine free space in a ring buffer.
Definition ring_buffer.h:229
static void ring_buf_commit(struct ring_buf *rb, size_t size)
Indicate number of bytes written to a ring buffer.
Definition ring_buffer.h:345
static uint32_t ring_buf_get(struct ring_buf *rb, uint8_t *data, uint32_t size)
Read data from a ring buffer.
Definition ring_buffer.h:448
static uint32_t ring_buf_put_ptr(struct ring_buf *rb, uint8_t **data, size_t offset)
Get address of region for writing data to a ring buffer.
Definition ring_buffer.h:314
static void ring_buf_consume(struct ring_buf *rb, size_t size)
Indicate number of bytes consumed from a ring buffer.
Definition ring_buffer.h:401
static bool ring_buf_is_full(const struct ring_buf *rb)
Determine if a ring buffer is full.
Definition ring_buffer.h:256
static void ring_buf_reset(struct ring_buf *rb)
Reset ring buffer state.
Definition ring_buffer.h:266
static void ring_buf_init(struct ring_buf *rb, uint32_t size, uint8_t *data)
Initialize a ring buffer for byte data.
Definition ring_buffer.h:288
static uint32_t ring_buf_get_ptr(struct ring_buf *rb, uint8_t **data, size_t offset)
Get address of valid data within a ring buffer.
Definition ring_buffer.h:370
static uint32_t ring_buf_peek(const struct ring_buf *rb, uint8_t *data, uint32_t size)
Peek at data from a ring buffer without consuming it.
Definition ring_buffer.h:483
static uint32_t ring_buf_put(struct ring_buf *rb, const uint8_t *data, uint32_t size)
Write (copy) data to a ring buffer.
Definition ring_buffer.h:418
#define IS_ENABLED(config_macro)
Check for macro definition in compiler-visible expressions.
Definition util_macro.h:166
#define MIN(a, b)
Obtain the minimum of two values.
Definition util.h:394
#define EINVAL
Invalid argument.
Definition errno.h:61
#define EMSGSIZE
Message size.
Definition errno.h:107
#define EAGAIN
No more contexts.
Definition errno.h:50
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
void * memcpy(void *ZRESTRICT d, const void *ZRESTRICT s, size_t n)
A structure to represent a ring buffer.
Definition ring_buffer.h:68
Misc utilities.
Macros to abstract toolchain specific capabilities.