diff options
| author | Shaun <[email protected]> | 2026-02-23 00:59:33 +0800 |
|---|---|---|
| committer | Rbb666 <[email protected]> | 2026-02-25 17:53:06 +0800 |
| commit | ffcd1cb3160b18ba789c6ae620b5e1c5d76052bc (patch) | |
| tree | d0821617a6a7ddea820a2520a095e23166874c3e /components/drivers/ipc | |
| parent | f610f18a3c796ba1bc404c886b408b01ca814cef (diff) | |
fix(drivers/ipc): correct rt_ringbuffer_peek to not consume data
Diffstat (limited to 'components/drivers/ipc')
| -rw-r--r-- | components/drivers/ipc/ringbuffer.c | 48 |
1 files changed, 44 insertions, 4 deletions
diff --git a/components/drivers/ipc/ringbuffer.c b/components/drivers/ipc/ringbuffer.c index 138eff055e..b5c2c358f2 100644 --- a/components/drivers/ipc/ringbuffer.c +++ b/components/drivers/ipc/ringbuffer.c @@ -219,16 +219,18 @@ rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, RTM_EXPORT(rt_ringbuffer_get); /** - * @brief Get the first readable byte of the ring buffer. + * @brief Get data from the ring buffer in zero-copy mode. * * @param rb A pointer to the ringbuffer. * @param ptr When this function return, *ptr is a pointer to the first readable byte of the ring buffer. * - * @note It is recommended to read only one byte, otherwise it may cause buffer overflow. + * @note This function returns a direct pointer to the internal buffer and consumes the data + * (advances read_index). It returns the contiguous readable data length. If data wraps + * around the buffer end, call this function again to get the remaining segment. * - * @return Return the size of the ring buffer. + * @return Return the contiguous readable data size we consumed from the ring buffer. */ -rt_size_t rt_ringbuffer_peek(struct rt_ringbuffer *rb, rt_uint8_t **ptr) +rt_size_t rt_ringbuffer_get_direct(struct rt_ringbuffer *rb, rt_uint8_t **ptr) { rt_size_t size; @@ -259,6 +261,44 @@ rt_size_t rt_ringbuffer_peek(struct rt_ringbuffer *rb, rt_uint8_t **ptr) return size; } +RTM_EXPORT(rt_ringbuffer_get_direct); + +/** + * @brief Get the first readable byte of the ring buffer. + * + * @param rb A pointer to the ringbuffer. + * @param ptr When this function return, *ptr is a pointer to the first readable byte of the ring buffer. + * + * @note It is recommended to read only one byte, otherwise it may cause buffer overflow. + * + * @return Return the size of the ring buffer. + */ +rt_size_t rt_ringbuffer_peek(struct rt_ringbuffer *rb, rt_uint8_t **ptr) +{ + rt_size_t size; + + RT_ASSERT(rb != RT_NULL); + + *ptr = RT_NULL; + + /* whether has enough data */ + size = rt_ringbuffer_data_len(rb); + + /* no data */ + if (size == 0) + return 0; + + *ptr = &rb->buffer_ptr[rb->read_index]; + + if ((rt_size_t)(rb->buffer_size - rb->read_index) > size) + { + return size; + } + + size = rb->buffer_size - rb->read_index; + + return size; +} RTM_EXPORT(rt_ringbuffer_peek); /** |
