summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlivier Gayot <duskcoder@gmail.com>2013-12-21 23:48:52 +0100
committerOlivier Gayot <duskcoder@gmail.com>2014-01-18 13:44:04 +0100
commit24dc23e78ca4b49d599093f7936ddcb9da818ab6 (patch)
tree2f60c337a403fb0225c794ec6bc3782febfeb155
parent3522c68dcd38e3eef393771d65aff6de2f815da3 (diff)
rb: allow to remove data from a ring buffer
we provide two new ways to remove data from a ring_buffer rb_remove(rb, n) or rb_get(rb, NULL, n) closes #2
-rw-r--r--rb.h9
-rw-r--r--src/rb.c8
2 files changed, 15 insertions, 2 deletions
diff --git a/rb.h b/rb.h
index f66dba8..5b1e0e1 100644
--- a/rb.h
+++ b/rb.h
@@ -46,4 +46,13 @@ size_t rb_get(t_rb *rb, void *dest, size_t n);
*/
size_t rb_peek(const t_rb *ring_buffer, void *dest, size_t size);
+/*
+ * remove a maximum of n bytes from the ring buffer pointed to by rb
+ * the functions returns the number of bytes actually removed
+ */
+static inline size_t rb_remove(t_rb *rb, size_t n)
+{
+ return rb_get(rb, NULL, n);
+}
+
#endif /* RING_BUFFER_H */
diff --git a/src/rb.c b/src/rb.c
index 505c373..ce269fb 100644
--- a/src/rb.c
+++ b/src/rb.c
@@ -96,7 +96,9 @@ size_t rb_peek(const t_rb *rb, void *dest, size_t n)
/* do not try to retrieve too much data */
size = MIN(n, rb_get_size(rb) - rb->off_r);
- memcpy(dest, &rb->buffer[rb->off_r], size);
+ if (dest != NULL) {
+ memcpy(dest, &rb->buffer[rb->off_r], size);
+ }
dest += size;
n -= size;
@@ -106,7 +108,9 @@ size_t rb_peek(const t_rb *rb, void *dest, size_t n)
size2 = MIN(n, (size_t)(rb->off_w - offset));
- memcpy(dest, &rb->buffer[offset], size2);
+ if (dest != NULL) {
+ memcpy(dest, &rb->buffer[offset], size2);
+ }
return size + size2;
}