]> git.puffer.fish Git - mirror/frr.git/commitdiff
lib: add ringbuf_copy()
authorQuentin Young <qlyoung@cumulusnetworks.com>
Tue, 2 Jan 2018 18:19:40 +0000 (18:19 +0000)
committerQuentin Young <qlyoung@cumulusnetworks.com>
Wed, 3 Jan 2018 19:04:43 +0000 (14:04 -0500)
Quick 'n easy way to copy the contents of one ringbuf to another.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
lib/ringbuf.c
lib/ringbuf.h

index f8e9a9bd8a2a8aec4b4f01cac62270e64c2aca60..d4efbe05fbf5e13f0dd9b9346c196faafa8ad30d 100644 (file)
@@ -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;
index 2288a2716fcbd042fcc3f6b4e8c7db78709d0a7a..15049e3eeab7d426621c246d40fcd79c610b39f7 100644 (file)
@@ -98,6 +98,16 @@ size_t ringbuf_get(struct ringbuf *buf, void *data, size_t size);
 size_t ringbuf_peek(struct ringbuf *buf, size_t offset, void *data,
                    size_t size);
 
+/*
+ * Copy data from one ringbuf to another.
+ *
+ * @param to   destination ringbuf
+ * @param from source ringbuf
+ * @param size how much data to copy
+ * @return amount of data copied
+ */
+size_t ringbuf_copy(struct ringbuf *to, struct ringbuf *from, size_t size);
+
 /*
  * Reset buffer. Does not wipe.
  *