From e4257284ad9762bea8a4c06f6612e380525659f6 Mon Sep 17 00:00:00 2001 From: Kaspar Schleiser Date: Thu, 11 Sep 2014 13:31:39 +0200 Subject: [PATCH] sys: lib: introduce ringbuffer_remove() This commit adds a function that deletes n elements from the ringbuffer without actually writing them somewhere. --- sys/include/ringbuffer.h | 8 ++++++++ sys/lib/ringbuffer.c | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) 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_;