From cb94eaebffb96aded4784fd89221b11f22336c6a Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Tue, 2 Jan 2018 18:19:40 +0000 Subject: lib: add ringbuf_copy() Quick 'n easy way to copy the contents of one ringbuf to another. Signed-off-by: Quentin Young --- lib/ringbuf.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'lib/ringbuf.c') diff --git a/lib/ringbuf.c b/lib/ringbuf.c index f8e9a9bd8a..d4efbe05fb 100644 --- a/lib/ringbuf.c +++ b/lib/ringbuf.c @@ -110,6 +110,15 @@ size_t ringbuf_peek(struct ringbuf *buf, size_t offset, void *data, size_t size) return copysize; } +size_t ringbuf_copy(struct ringbuf *to, struct ringbuf *from, size_t size) +{ + size_t tocopy = MIN(ringbuf_space(to), size); + uint8_t *cbuf = XCALLOC(MTYPE_TMP, tocopy); + tocopy = ringbuf_peek(from, 0, cbuf, tocopy); + XFREE(MTYPE_TMP, cbuf); + return ringbuf_put(to, cbuf, tocopy); +} + void ringbuf_reset(struct ringbuf *buf) { buf->start = buf->end = 0; -- cgit v1.2.3