diff options
| author | Quentin Young <qlyoung@cumulusnetworks.com> | 2018-01-02 18:19:40 +0000 | 
|---|---|---|
| committer | Quentin Young <qlyoung@cumulusnetworks.com> | 2018-01-03 14:04:43 -0500 | 
| commit | cb94eaebffb96aded4784fd89221b11f22336c6a (patch) | |
| tree | 29b6275f54f9144b0ced04ef7d701da364419466 /lib | |
| parent | 9bc82f11fbd9d2ebbb1094ff2f83c5b878fad590 (diff) | |
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>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/ringbuf.c | 9 | ||||
| -rw-r--r-- | lib/ringbuf.h | 10 | 
2 files changed, 19 insertions, 0 deletions
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; diff --git a/lib/ringbuf.h b/lib/ringbuf.h index 2288a2716f..15049e3eea 100644 --- a/lib/ringbuf.h +++ b/lib/ringbuf.h @@ -99,6 +99,16 @@ 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.   *   * @param buf  | 
