Zephyr API Documentation 4.0.99
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/sys/util.h>
11#include <errno.h>
12
13#ifdef __cplusplus
14extern "C" {
15#endif
16
18/* The limit is used by algorithm for distinguishing between empty and full
19 * state.
20 */
21#define RING_BUFFER_MAX_SIZE 0x80000000U
22
23#define RING_BUFFER_SIZE_ASSERT_MSG \
24 "Size too big"
40struct ring_buf {
42 uint8_t *buffer;
43 int32_t put_head;
44 int32_t put_tail;
45 int32_t put_base;
46 int32_t get_head;
47 int32_t get_tail;
48 int32_t get_base;
49 uint32_t size;
51};
52
58static inline void ring_buf_internal_reset(struct ring_buf *buf, int32_t value)
59{
60 buf->put_head = buf->put_tail = buf->put_base = value;
61 buf->get_head = buf->get_tail = buf->get_base = value;
62}
63
64#define RING_BUF_INIT(buf, size8) \
65{ \
66 .buffer = buf, \
67 .size = size8, \
68}
83#define RING_BUF_DECLARE(name, size8) \
84 BUILD_ASSERT(size8 < RING_BUFFER_MAX_SIZE,\
85 RING_BUFFER_SIZE_ASSERT_MSG); \
86 static uint8_t __noinit _ring_buffer_data_##name[size8]; \
87 struct ring_buf name = RING_BUF_INIT(_ring_buffer_data_##name, size8)
88
104#define RING_BUF_ITEM_DECLARE(name, size32) \
105 BUILD_ASSERT((size32) < RING_BUFFER_MAX_SIZE / 4,\
106 RING_BUFFER_SIZE_ASSERT_MSG); \
107 static uint32_t __noinit _ring_buffer_data_##name[size32]; \
108 struct ring_buf name = { \
109 .buffer = (uint8_t *) _ring_buffer_data_##name, \
110 .size = 4 * (size32) \
111 }
112
122#define RING_BUF_ITEM_DECLARE_SIZE(name, size32) \
123 RING_BUF_ITEM_DECLARE(name, size32)
124
135#define RING_BUF_ITEM_DECLARE_POW2(name, pow) \
136 RING_BUF_ITEM_DECLARE(name, BIT(pow))
137
146#define RING_BUF_ITEM_SIZEOF(expr) DIV_ROUND_UP(sizeof(expr), sizeof(uint32_t))
147
158static inline void ring_buf_init(struct ring_buf *buf,
159 uint32_t size,
160 uint8_t *data)
161{
162 __ASSERT(size < RING_BUFFER_MAX_SIZE, RING_BUFFER_SIZE_ASSERT_MSG);
163
164 buf->size = size;
165 buf->buffer = data;
167}
168
182static inline void ring_buf_item_init(struct ring_buf *buf,
183 uint32_t size,
184 uint32_t *data)
185{
186 __ASSERT(size < RING_BUFFER_MAX_SIZE / 4, RING_BUFFER_SIZE_ASSERT_MSG);
187 ring_buf_init(buf, 4 * size, (uint8_t *)data);
188}
189
197static inline bool ring_buf_is_empty(struct ring_buf *buf)
198{
199 return buf->get_head == buf->put_tail;
200}
201
207static inline void ring_buf_reset(struct ring_buf *buf)
208{
210}
211
219static inline uint32_t ring_buf_space_get(struct ring_buf *buf)
220{
221 return buf->size - (buf->put_head - buf->get_tail);
222}
223
231static inline uint32_t ring_buf_item_space_get(struct ring_buf *buf)
232{
233 return ring_buf_space_get(buf) / 4;
234}
235
243static inline uint32_t ring_buf_capacity_get(struct ring_buf *buf)
244{
245 return buf->size;
246}
247
255static inline uint32_t ring_buf_size_get(struct ring_buf *buf)
256{
257 return buf->put_tail - buf->get_head;
258}
259
285 uint8_t **data,
286 uint32_t size);
287
310int ring_buf_put_finish(struct ring_buf *buf, uint32_t size);
311
332uint32_t ring_buf_put(struct ring_buf *buf, const uint8_t *data, uint32_t size);
333
359 uint8_t **data,
360 uint32_t size);
361
384int ring_buf_get_finish(struct ring_buf *buf, uint32_t size);
385
407
435
457int ring_buf_item_put(struct ring_buf *buf, uint16_t type, uint8_t value,
458 uint32_t *data, uint8_t size32);
459
484int ring_buf_item_get(struct ring_buf *buf, uint16_t *type, uint8_t *value,
485 uint32_t *data, uint8_t *size32);
486
491#ifdef __cplusplus
492}
493#endif
494
495#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:219
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:58
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:243
static void ring_buf_reset(struct ring_buf *buf)
Reset ring buffer state.
Definition ring_buffer.h:207
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:182
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:231
static bool ring_buf_is_empty(struct ring_buf *buf)
Determine if a ring buffer is empty.
Definition ring_buffer.h:197
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:158
static uint32_t ring_buf_size_get(struct ring_buf *buf)
Determine used space in a ring buffer.
Definition ring_buffer.h:255
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.
__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:40
Misc utilities.