From c031bf861111b8e3fd1d5e605cac9407d3497f75 Mon Sep 17 00:00:00 2001 From: Felix Kopp Date: Fri, 30 Oct 2020 17:51:19 +0100 Subject: [PATCH] Add ringbuf_size API This might become useful if we want to dynamically allocate an array that stores the entire queue. --- include/ardix/ringbuf.h | 24 ++++++++++++++++-------- kernel/ringbuf.c | 12 ++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/include/ardix/ringbuf.h b/include/ardix/ringbuf.h index 1e74296..efef0ee 100644 --- a/include/ardix/ringbuf.h +++ b/include/ardix/ringbuf.h @@ -15,7 +15,7 @@ struct ringbuf { /** * Create and initialize a new ring buffer. * - * @param size Total buffer size in bytes. + * @param size: Total buffer size in bytes. * @returns The newly allocated buffer, or `NULL` if OOM. */ struct ringbuf *rungbuf_create(size_t size); @@ -23,7 +23,7 @@ struct ringbuf *rungbuf_create(size_t size); /** * Destroy a ring buffer previously created with `ringbuf_create`. * - * @param buf The buffer to destroy. + * @param buf: The buffer to destroy. */ void ringbuf_destroy(struct ringbuf *buf); @@ -32,9 +32,9 @@ void ringbuf_destroy(struct ringbuf *buf); * Any byte that has been read once is discarded from the buffer. * If there are less than `len` bytes stored in the buffer, reading stops early. * - * @param dest Where to write the data to. - * @param buf The buffer to read from. - * @param len The maximum amount of bytes to read. + * @param dest: Where to write the data to. + * @param buf: The buffer to read from. + * @param len: The maximum amount of bytes to read. * @returns The actual amount of bytes read. */ size_t ringbuf_read(uint8_t *dest, struct ringbuf *buf, size_t len); @@ -43,13 +43,21 @@ size_t ringbuf_read(uint8_t *dest, struct ringbuf *buf, size_t len); * Write up to `len` bytes to the buffer. * If the buffer would overflow, writing stops early. * - * @param buf The buffer to write to. - * @param src The data to write. - * @param len The length of `src`. + * @param buf: The buffer to write to. + * @param src: The data to write. + * @param len: The length of `src`. * @returns The actual amount of bytes that were written. */ size_t ringbuf_write(struct ringbuf *buf, const uint8_t *src, size_t len); +/** + * Get the amount of bytes currently stored in the ring buffer. + * + * @param buf: The buffer. + * @returns The amount of bytes that are available for reading. + */ +size_t ringbuf_size(struct ringbuf *buf); + /* * Copyright (c) 2020 Felix Kopp * diff --git a/kernel/ringbuf.c b/kernel/ringbuf.c index 55230a0..31d1ad5 100644 --- a/kernel/ringbuf.c +++ b/kernel/ringbuf.c @@ -58,6 +58,18 @@ size_t ringbuf_write(struct ringbuf *buf, const uint8_t *src, size_t len) return n; } +size_t ringbuf_size(struct ringbuf *buf) +{ + ptrdiff_t size = (ptrdiff_t)(buf->wpos) - (ptrdiff_t)(buf->rpos); + if (size < 0) { + /* wpos has wrapped around already, but rpos has not */ + size = (ptrdiff_t)(buf->wpos) - (ptrdiff_t)(&buf->data[0]); + size += (ptrdiff_t)(buf->size) - ((ptrdiff_t)(buf->rpos) - (ptrdiff_t)(&buf->data[0])); + } + + return (size_t)size; +} + /* * Copyright (c) 2020 Felix Kopp *