From 6173fe7a73a8823cde971c5145dc59a504c39a86 Mon Sep 17 00:00:00 2001 From: Krzysztof Chruscinski Date: Wed, 6 Feb 2019 16:47:29 +0100 Subject: [PATCH] ring_buffer: Add functions for getting capacity and reseting Extend ring_buffer with following functions: - getting capacity of the ring buffer (which is smaller than buffer size) - resetting ring buffer to initial state Signed-off-by: Krzysztof Chruscinski --- include/ring_buffer.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/include/ring_buffer.h b/include/ring_buffer.h index d54e29a8d28..a953fafc63f 100644 --- a/include/ring_buffer.h +++ b/include/ring_buffer.h @@ -195,6 +195,19 @@ static inline int ring_buf_is_empty(struct ring_buf *buf) { return (buf->head == buf->tail); } + +/** + * @brief Reset ring buffer state. + * + * @param buf Address of ring buffer. + */ +static inline void ring_buf_reset(struct ring_buf *buf) +{ + buf->head = 0; + buf->tail = 0; + memset(&buf->misc, 0, sizeof(buf->misc)); +} + /** @deprecated Renamed to ring_buf_is_empty. */ __deprecated static inline int sys_ring_buf_is_empty(struct ring_buf *buf) { @@ -213,6 +226,19 @@ static inline int ring_buf_space_get(struct ring_buf *buf) return z_ring_buf_custom_space_get(buf->size, buf->head, buf->tail); } +/** + * @brief Return ring buffer capacity. + * + * @param buf Address of ring buffer. + * + * @return Ring buffer capacity (in 32-bit words or bytes). + */ +static inline int ring_buf_capacity_get(struct ring_buf *buf) +{ + /* One element is used to distinguish between empty and full state. */ + return buf->size - 1; +} + /** @deprecated Renamed to ring_buf_space_get. */ __deprecated static inline int sys_ring_buf_space_get(struct ring_buf *buf) {