Add ringbuf_size API

This might become useful if we want to dynamically
allocate an array that stores the entire queue.
This commit is contained in:
Felix Kopp 2020-10-30 17:51:19 +01:00
parent fd75357944
commit c031bf8611
No known key found for this signature in database
GPG key ID: C478BA0A85F75728
2 changed files with 28 additions and 8 deletions

View file

@ -15,7 +15,7 @@ struct ringbuf {
/** /**
* Create and initialize a new ring buffer. * 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. * @returns The newly allocated buffer, or `NULL` if OOM.
*/ */
struct ringbuf *rungbuf_create(size_t size); 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`. * 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); 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. * 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. * If there are less than `len` bytes stored in the buffer, reading stops early.
* *
* @param dest Where to write the data to. * @param dest: Where to write the data to.
* @param buf The buffer to read from. * @param buf: The buffer to read from.
* @param len The maximum amount of bytes to read. * @param len: The maximum amount of bytes to read.
* @returns The actual amount of bytes read. * @returns The actual amount of bytes read.
*/ */
size_t ringbuf_read(uint8_t *dest, struct ringbuf *buf, size_t len); 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. * Write up to `len` bytes to the buffer.
* If the buffer would overflow, writing stops early. * If the buffer would overflow, writing stops early.
* *
* @param buf The buffer to write to. * @param buf: The buffer to write to.
* @param src The data to write. * @param src: The data to write.
* @param len The length of `src`. * @param len: The length of `src`.
* @returns The actual amount of bytes that were written. * @returns The actual amount of bytes that were written.
*/ */
size_t ringbuf_write(struct ringbuf *buf, const uint8_t *src, size_t len); 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 <sandtler@sandtler.club> * Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
* *

View file

@ -58,6 +58,18 @@ size_t ringbuf_write(struct ringbuf *buf, const uint8_t *src, size_t len)
return n; 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 <sandtler@sandtler.club> * Copyright (c) 2020 Felix Kopp <sandtler@sandtler.club>
* *