Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
ring_buffer.h
Go to the documentation of this file.
1/* ring_buffer.h: Simple ring buffer API */
2
3/*
4 * Copyright (c) 2015 Intel Corporation
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
10#ifndef ZEPHYR_INCLUDE_SYS_RING_BUFFER_H_
11#define ZEPHYR_INCLUDE_SYS_RING_BUFFER_H_
12
13#include <kernel.h>
14#include <sys/util.h>
15#include <errno.h>
16#include <string.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22#define SIZE32_OF(x) (sizeof((x))/sizeof(uint32_t))
23
24/* The limit is used by algorithm for distinguishing between empty and full
25 * state.
26 */
27#define RING_BUFFER_MAX_SIZE 0x80000000
28
29#define RING_BUFFER_SIZE_ASSERT_MSG \
30 "Size too big, if it is the ring buffer test check custom max size"
34struct ring_buf {
54 } buf;
58};
59
82#define RING_BUF_ITEM_DECLARE_POW2(name, pow) \
83 BUILD_ASSERT((1 << pow) < RING_BUFFER_MAX_SIZE,\
84 RING_BUFFER_SIZE_ASSERT_MSG); \
85 static uint32_t __noinit _ring_buffer_data_##name[BIT(pow)]; \
86 struct ring_buf name = { \
87 .size = (BIT(pow)), \
88 .mask = (BIT(pow)) - 1, \
89 .buf = { .buf32 = _ring_buffer_data_##name } \
90 }
91
106#define RING_BUF_ITEM_DECLARE_SIZE(name, size32) \
107 BUILD_ASSERT(size32 < RING_BUFFER_MAX_SIZE,\
108 RING_BUFFER_SIZE_ASSERT_MSG); \
109 static uint32_t __noinit _ring_buffer_data_##name[size32]; \
110 struct ring_buf name = { \
111 .size = size32, \
112 .buf = { .buf32 = _ring_buffer_data_##name} \
113 }
114
128#define RING_BUF_DECLARE(name, size8) \
129 BUILD_ASSERT(size8 < RING_BUFFER_MAX_SIZE,\
130 RING_BUFFER_SIZE_ASSERT_MSG); \
131 static uint8_t __noinit _ring_buffer_data_##name[size8]; \
132 struct ring_buf name = { \
133 .size = size8, \
134 .buf = { .buf8 = _ring_buffer_data_##name} \
135 }
136
137
154static inline void ring_buf_init(struct ring_buf *buf,
155 uint32_t size,
156 void *data)
157{
159
160 memset(buf, 0, sizeof(struct ring_buf));
161 buf->size = size;
162 buf->buf.buf32 = (uint32_t *)data;
163 if (is_power_of_two(size)) {
164 buf->mask = size - 1U;
165 } else {
166 buf->mask = 0U;
167 }
168}
169
178
184static inline void ring_buf_reset(struct ring_buf *buf)
185{
186 buf->head = 0;
187 buf->tail = 0;
188 memset(&buf->misc, 0, sizeof(buf->misc));
189}
190
199
207static inline uint32_t ring_buf_capacity_get(struct ring_buf *buf)
208{
209 return buf->size;
210}
211
220
242int ring_buf_item_put(struct ring_buf *buf, uint16_t type, uint8_t value,
243 uint32_t *data, uint8_t size32);
244
269int ring_buf_item_get(struct ring_buf *buf, uint16_t *type, uint8_t *value,
270 uint32_t *data, uint8_t *size32);
271
297 uint8_t **data,
298 uint32_t size);
299
318int ring_buf_put_finish(struct ring_buf *buf, uint32_t size);
319
341
367 uint8_t **data,
368 uint32_t size);
369
388int ring_buf_get_finish(struct ring_buf *buf, uint32_t size);
389
411
439
444#ifdef __cplusplus
445}
446#endif
447
448#endif /* ZEPHYR_INCLUDE_SYS_RING_BUFFER_H_ */
System error numbers.
uint32_t ring_buf_get(struct ring_buf *buf, uint8_t *data, uint32_t size)
Read data from a ring buffer.
int ring_buf_get_finish(struct ring_buf *buf, uint32_t size)
Indicate number of bytes read from claimed buffer.
int ring_buf_put_finish(struct ring_buf *buf, uint32_t size)
Indicate number of bytes written to allocated buffers.
uint32_t ring_buf_space_get(struct ring_buf *buf)
Determine free space in a ring buffer.
uint32_t ring_buf_put(struct ring_buf *buf, const uint8_t *data, uint32_t size)
Write (copy) data to a ring buffer.
int ring_buf_item_put(struct ring_buf *buf, uint16_t type, uint8_t value, uint32_t *data, uint8_t size32)
Write a data item to a ring buffer.
uint32_t ring_buf_get_claim(struct ring_buf *buf, uint8_t **data, uint32_t size)
Get address of a valid data in a ring buffer.
uint32_t ring_buf_size_get(struct ring_buf *buf)
Determine used space in a ring buffer.
uint32_t ring_buf_peek(struct ring_buf *buf, uint8_t *data, uint32_t size)
Peek at data from a ring buffer.
static uint32_t ring_buf_capacity_get(struct ring_buf *buf)
Return ring buffer capacity.
Definition: ring_buffer.h:207
static void ring_buf_reset(struct ring_buf *buf)
Reset ring buffer state.
Definition: ring_buffer.h:184
int ring_buf_is_empty(struct ring_buf *buf)
Determine if a ring buffer is empty.
int ring_buf_item_get(struct ring_buf *buf, uint16_t *type, uint8_t *value, uint32_t *data, uint8_t *size32)
Read a data item from a ring buffer.
uint32_t ring_buf_put_claim(struct ring_buf *buf, uint8_t **data, uint32_t size)
Allocate buffer for writing data to a ring buffer.
static void ring_buf_init(struct ring_buf *buf, uint32_t size, void *data)
Initialize a ring buffer.
Definition: ring_buffer.h:154
static bool is_power_of_two(unsigned int x)
Is x a power of two?
Definition: util.h:196
#define RING_BUFFER_SIZE_ASSERT_MSG
Definition: ring_buffer.h:29
#define RING_BUFFER_MAX_SIZE
Definition: ring_buffer.h:27
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT8_TYPE__ uint8_t
Definition: stdint.h:58
__UINT16_TYPE__ uint16_t
Definition: stdint.h:59
void * memset(void *buf, int c, size_t n)
Kernel Spin Lock.
Definition: spinlock.h:29
uint32_t tmp_head
Definition: ring_buffer.h:46
uint32_t tmp_tail
Definition: ring_buffer.h:45
uint32_t dropped_put_count
Definition: ring_buffer.h:39
A structure to represent a ring buffer.
Definition: ring_buffer.h:34
uint32_t tail
Definition: ring_buffer.h:36
uint32_t mask
Definition: ring_buffer.h:55
struct k_spinlock lock
Definition: ring_buffer.h:57
union ring_buf::ring_buf_buffer buf
union ring_buf::ring_buf_misc misc
uint32_t size
Definition: ring_buffer.h:49
uint32_t head
Definition: ring_buffer.h:35
static fdata_t data[2]
Definition: test_fifo_contexts.c:15
Definition: ring_buffer.h:51
uint8_t * buf8
Definition: ring_buffer.h:53
uint32_t * buf32
Definition: ring_buffer.h:52
Definition: ring_buffer.h:37
struct ring_buf::ring_buf_misc::ring_buf_misc_item_mode item_mode
struct ring_buf::ring_buf_misc::ring_buf_misc_byte_mode byte_mode
Misc utilities.