diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/ringbuf.c | 15 | ||||
| -rw-r--r-- | lib/ringbuf.h | 10 |
2 files changed, 22 insertions, 3 deletions
diff --git a/lib/ringbuf.c b/lib/ringbuf.c index f8e9a9bd8a..11db502a94 100644 --- a/lib/ringbuf.c +++ b/lib/ringbuf.c @@ -58,7 +58,7 @@ size_t ringbuf_put(struct ringbuf *buf, const void *data, size_t size) size_t space = ringbuf_space(buf); size_t copysize = MIN(size, space); size_t tocopy = copysize; - if (tocopy > buf->size - buf->end) { + if (tocopy >= buf->size - buf->end) { size_t ts = buf->size - buf->end; memcpy(buf->data + buf->end, dp, ts); buf->end = 0; @@ -102,7 +102,7 @@ size_t ringbuf_peek(struct ringbuf *buf, size_t offset, void *data, size_t size) if (tocopy >= buf->size - cstart) { size_t ts = buf->size - cstart; memcpy(dp, buf->data + cstart, ts); - buf->start = cstart = 0; + cstart = 0; tocopy -= ts; dp += ts; } @@ -110,6 +110,16 @@ 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); + size_t put = ringbuf_put(to, cbuf, tocopy); + XFREE(MTYPE_TMP, cbuf); + return put; +} + void ringbuf_reset(struct ringbuf *buf) { buf->start = buf->end = 0; @@ -120,5 +130,4 @@ void ringbuf_wipe(struct ringbuf *buf) { memset(buf->data, 0x00, buf->size); ringbuf_reset(buf); - buf->empty = true; } 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 |
