Zephyr Project API 3.7.0
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
ring_buffer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_SYS_RING_BUFFER_H_
8#define ZEPHYR_INCLUDE_SYS_RING_BUFFER_H_
9
10#include <zephyr/kernel.h>
11#include <zephyr/sys/util.h>
12#include <errno.h>
13
14#ifdef __cplusplus
15extern "C" {
16#endif
17
19/* The limit is used by algorithm for distinguishing between empty and full
20 * state.
21 */
22#define RING_BUFFER_MAX_SIZE 0x80000000U
23
24#define RING_BUFFER_SIZE_ASSERT_MSG \
25 "Size too big"
41struct ring_buf {
44 int32_t put_head;
45 int32_t put_tail;
46 int32_t put_base;
47 int32_t get_head;
48 int32_t get_tail;
49 int32_t get_base;
50 uint32_t size;
52};
53
59static inline void ring_buf_internal_reset(struct ring_buf *buf, int32_t value)
60{
61 buf->put_head = buf->put_tail = buf->put_base = value;
62 buf->get_head = buf->get_tail = buf->get_base = value;
63}
64
79#define RING_BUF_DECLARE(name, size8) \
80 BUILD_ASSERT(size8 < RING_BUFFER_MAX_SIZE,\
81 RING_BUFFER_SIZE_ASSERT_MSG); \
82 static uint8_t __noinit _ring_buffer_data_##name[size8]; \
83 struct ring_buf name = { \
84 .buffer = _ring_buffer_data_##name, \
85 .size = size8 \
86 }
87
103#define RING_BUF_ITEM_DECLARE(name, size32) \
104 BUILD_ASSERT((size32) < RING_BUFFER_MAX_SIZE / 4,\
105 RING_BUFFER_SIZE_ASSERT_MSG); \
106 static uint32_t __noinit _ring_buffer_data_##name[size32]; \
107 struct ring_buf name = { \
108 .buffer = (uint8_t *) _ring_buffer_data_##name, \
109 .size = 4 * (size32) \
110 }
111
121#define RING_BUF_ITEM_DECLARE_SIZE(name, size32) \
122 RING_BUF_ITEM_DECLARE(name, size32)
123
134#define RING_BUF_ITEM_DECLARE_POW2(name, pow) \
135 RING_BUF_ITEM_DECLARE(name, BIT(pow))
136
145#define RING_BUF_ITEM_SIZEOF(expr) DIV_ROUND_UP(sizeof(expr), sizeof(uint32_t))
146
157static inline void ring_buf_init(struct ring_buf *buf,
158 uint32_t size,
159 uint8_t *data)
160{
161 __ASSERT(size < RING_BUFFER_MAX_SIZE, RING_BUFFER_SIZE_ASSERT_MSG);
162
163 buf->size = size;
164 buf->buffer = data;
166}
167
184static inline void ring_buf_item_init(struct ring_buf *buf,
185 uint32_t size,
186 uint32_t *data)
187{
188 __ASSERT(size < RING_BUFFER_MAX_SIZE / 4, RING_BUFFER_SIZE_ASSERT_MSG);
189 ring_buf_init(buf, 4 * size, (uint8_t *)data);
190}
191
199static inline bool ring_buf_is_empty(struct ring_buf *buf)
200{
201 return buf->get_head == buf->put_tail;
202}
203
209static inline void ring_buf_reset(struct ring_buf *buf)
210{
212}
213
221static inline uint32_t ring_buf_space_get(struct ring_buf *buf)
222{
223 return buf->size - (buf->put_head - buf->get_tail);
224}
225
233static inline uint32_t ring_buf_item_space_get(struct ring_buf *buf)
234{
235 return ring_buf_space_get(buf) / 4;
236}
237
245static inline uint32_t ring_buf_capacity_get(struct ring_buf *buf)
246{
247 return buf->size;
248}
249
257static inline uint32_t ring_buf_size_get(struct ring_buf *buf)
258{
259 return buf->put_tail - buf->get_head;
260}
261
287 uint8_t **data,
288 uint32_t size);
289
312int ring_buf_put_finish(struct ring_buf *buf, uint32_t size);
313
335
361 uint8_t **data,
362 uint32_t size);
363
386int ring_buf_get_finish(struct ring_buf *buf, uint32_t size);
387
409
437
459int ring_buf_item_put(struct ring_buf *buf, uint16_t type, uint8_t value,
460 uint32_t *data, uint8_t size32);
461
486int ring_buf_item_get(struct ring_buf *buf, uint16_t *type, uint8_t *value,
487 uint32_t *data, uint8_t *size32);
488
493#ifdef __cplusplus
494}
495#endif
496
497#endif /* ZEPHYR_INCLUDE_SYS_RING_BUFFER_H_ */
System error numbers.
static uint32_t ring_buf_space_get(struct ring_buf *buf)
Determine free space in a ring buffer.
Definition ring_buffer.h:221
static void ring_buf_internal_reset(struct ring_buf *buf, int32_t value)
Function to force ring_buf internal states to given value.
Definition ring_buffer.h:59
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_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_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:245
static void ring_buf_reset(struct ring_buf *buf)
Reset ring buffer state.
Definition ring_buffer.h:209
static void ring_buf_item_init(struct ring_buf *buf, uint32_t size, uint32_t *data)
Initialize an "item based" ring buffer.
Definition ring_buffer.h:184
static uint32_t ring_buf_item_space_get(struct ring_buf *buf)
Determine free space in an "item based" ring buffer.
Definition ring_buffer.h:233
static bool ring_buf_is_empty(struct ring_buf *buf)
Determine if a ring buffer is empty.
Definition ring_buffer.h:199
static void ring_buf_init(struct ring_buf *buf, uint32_t size, uint8_t *data)
Initialize a ring buffer for byte data.
Definition ring_buffer.h:157
static uint32_t ring_buf_size_get(struct ring_buf *buf)
Determine used space in a ring buffer.
Definition ring_buffer.h:257
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.
Public kernel APIs.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
A structure to represent a ring buffer.
Definition ring_buffer.h:41
static fdata_t data[2]
Definition test_fifo_contexts.c:15
static ZTEST_BMEM char buffer[8]
Definition test_mbox_api.c:433
Misc utilities.