1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys: lib: introduce ringbuffer_remove()

This commit adds a function that deletes n elements from the ringbuffer
without actually writing them somewhere.
This commit is contained in:
Kaspar Schleiser 2014-09-11 13:31:39 +02:00
parent b7fc0df5d3
commit e4257284ad
2 changed files with 27 additions and 0 deletions

View File

@ -86,6 +86,14 @@ int ringbuffer_get_one(ringbuffer_t *restrict rb);
*/
unsigned ringbuffer_get(ringbuffer_t *restrict rb, char *buf, unsigned n);
/**
* @brief Remove a number of elements from the ringbuffer.
* @param[in,out] rb Ringbuffer to operate on.
* @param[in] n Read at most n elements.
* @returns Number of elements actually removed.
*/
unsigned ringbuffer_remove(ringbuffer_t *restrict rb, unsigned n);
/**
* @brief Test if the ringbuffer is empty.
* @param[in,out] rb Ringbuffer to operate on.

View File

@ -102,6 +102,25 @@ unsigned ringbuffer_get(ringbuffer_t *restrict rb, char *buf, unsigned n)
return n;
}
unsigned ringbuffer_remove(ringbuffer_t *restrict rb, unsigned n)
{
if (n > rb->avail) {
n = rb->avail;
rb->start = rb->avail = 0;
}
else {
rb->start -= n;
rb->avail -= n;
/* compensate underflow */
if (rb->start > rb->size) {
rb->start += rb->size;
}
}
return n;
}
int ringbuffer_peek_one(const ringbuffer_t *restrict rb_)
{
ringbuffer_t rb = *rb_;