From 8d9cb25ed7a69017f45bc3673f45c83095fd6896 Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Fri, 31 Aug 2018 10:35:02 +0200 Subject: [PATCH] tsrb: add drop function The get function does not support passing NULL as an input buffer. to be able to drop bytes from the buffer, a dedicated drop function is required --- sys/include/tsrb.h | 8 ++++++++ sys/tsrb/tsrb.c | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/sys/include/tsrb.h b/sys/include/tsrb.h index f0d7f698aa..0dcc04730f 100644 --- a/sys/include/tsrb.h +++ b/sys/include/tsrb.h @@ -127,6 +127,14 @@ int tsrb_get_one(tsrb_t *rb); */ int tsrb_get(tsrb_t *rb, char *dst, size_t n); +/** + * @brief Drop bytes from ringbuffer + * @param[in] rb Ringbuffer to operate on + * @param[in] n max number of bytes to drop + * @return nr of bytes dropped + */ +int tsrb_drop(tsrb_t *rb, size_t n); + /** * @brief Add a byte to ringbuffer * @param[in] rb Ringbuffer to operate on diff --git a/sys/tsrb/tsrb.c b/sys/tsrb/tsrb.c index 334550fa4a..e85ea52664 100644 --- a/sys/tsrb/tsrb.c +++ b/sys/tsrb/tsrb.c @@ -49,6 +49,16 @@ int tsrb_get(tsrb_t *rb, char *dst, size_t n) return (n - tmp); } +int tsrb_drop(tsrb_t *rb, size_t n) +{ + size_t tmp = n; + while (tmp && !tsrb_empty(rb)) { + _pop(rb); + tmp--; + } + return (n - tmp); +} + int tsrb_add_one(tsrb_t *rb, char c) { if (!tsrb_full(rb)) {