lib: add ringbuf_copy()

Quick 'n easy way to copy the contents of one ringbuf to another.

Signed-off-by: Quentin Young <qlyoung@cumulusnetworks.com>
This commit is contained in:
Quentin Young 2018-01-02 18:19:40 +00:00
parent 9bc82f11fb
commit cb94eaebff
No known key found for this signature in database
GPG Key ID: DAF48E0F57E0834F
2 changed files with 19 additions and 0 deletions

View File

@ -110,6 +110,15 @@ size_t ringbuf_peek(struct ringbuf *buf, size_t offset, void *data, size_t size)
return copysize; 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) void ringbuf_reset(struct ringbuf *buf)
{ {
buf->start = buf->end = 0; buf->start = buf->end = 0;

View 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 ringbuf_peek(struct ringbuf *buf, size_t offset, void *data,
size_t size); 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. * Reset buffer. Does not wipe.
* *