From 9cccf6bf8e9a67cc323bbacae264aa14231f4e77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Thu, 11 Sep 2014 14:12:06 +0200 Subject: [PATCH] sys:ringbuffer: use memcpy in ringbuffer_get --- sys/lib/ringbuffer.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/sys/lib/ringbuffer.c b/sys/lib/ringbuffer.c index 7ccc7e4f6..cd6a38cb4 100644 --- a/sys/lib/ringbuffer.c +++ b/sys/lib/ringbuffer.c @@ -18,6 +18,8 @@ #include "ringbuffer.h" +#include + /** * @brief Add an element to the end of the ringbuffer. * @details This helper function does not check the pre-requirements for adding, @@ -87,9 +89,23 @@ unsigned ringbuffer_get(ringbuffer_t *restrict rb, char *buf, unsigned n) if (n > rb->avail) { n = rb->avail; } - - for (unsigned i = 0; i < n; ++i) { - buf[i] = get_head(rb); + if (n > 0) { + unsigned bytes_till_end = rb->size - rb->start; + if (bytes_till_end >= n) { + memcpy(buf, rb->buf + rb->start, n); + if (bytes_till_end == n) { + rb->start = 0; + } + else { + rb->start += n; + } + } + else { + memcpy(buf, rb->buf + rb->start, bytes_till_end); + rb->start = n - bytes_till_end; + memcpy(buf + bytes_till_end, rb->buf, rb->start); + } + rb->avail -= n; } return n; }