diff --git a/sys/include/ringbuffer.h b/sys/include/ringbuffer.h index af8c20e419..8641c318ed 100644 --- a/sys/include/ringbuffer.h +++ b/sys/include/ringbuffer.h @@ -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. diff --git a/sys/lib/ringbuffer.c b/sys/lib/ringbuffer.c index 8cdcc02f41..2e7a14ca98 100644 --- a/sys/lib/ringbuffer.c +++ b/sys/lib/ringbuffer.c @@ -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_;