aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichał Łyszczek <michal.lyszczek@bofc.pl>2018-02-02 20:03:48 +0100
committerMichał Łyszczek <michal.lyszczek@bofc.pl>2018-02-02 20:03:48 +0100
commitf4d9899c8d24d87644d4cc0c13c3bba9d8f81bc3 (patch)
tree30dcb691cfc4c29f369d81faf5d5d76606ed6ed3
parent5934814945eb4cec144db95bd076814869dbb4f2 (diff)
downloadlibrb-f4d9899c8d24d87644d4cc0c13c3bba9d8f81bc3.tar.gz
librb-f4d9899c8d24d87644d4cc0c13c3bba9d8f81bc3.tar.bz2
librb-f4d9899c8d24d87644d4cc0c13c3bba9d8f81bc3.zip
add: rb_discard function
-rw-r--r--include/rb.h1
-rw-r--r--rb/rb.c55
2 files changed, 56 insertions, 0 deletions
diff --git a/include/rb.h b/include/rb.h
index 4e00305..aa17de8 100644
--- a/include/rb.h
+++ b/include/rb.h
@@ -36,6 +36,7 @@ long rb_send(struct rb *, const void *, size_t, unsigned long);
int rb_clear(struct rb *, int);
int rb_destroy(struct rb *);
int rb_stop(struct rb *);
+size_t rb_discard(struct rb *, size_t);
const char *rb_version(char *, char *, char *);
size_t rb_count(const struct rb *);
size_t rb_space(const struct rb *);
diff --git a/rb/rb.c b/rb/rb.c
index 9e32a92..e3efd24 100644
--- a/rb/rb.c
+++ b/rb/rb.c
@@ -988,6 +988,61 @@ int rb_stop
/* ==========================================================================
+ Function that discards data from tail of buffer. This works just like
+ rb_reads function, but is way faster as there is no copying involved
+ ========================================================================== */
+
+
+size_t rb_discard
+(
+ struct rb *rb, /* rb object */
+ size_t count /* number of elements to discard */
+)
+{
+ size_t rbcount; /* number of elements in rb */
+ size_t cnte; /* number of elements in rb until overlap */
+ /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
+
+
+ VALID(EINVAL, rb);
+ VALID(EINVAL, rb->buffer);
+
+#if ENABLE_THREADS
+ if ((rb->flags & O_NONBLOCK) == 0)
+ {
+ pthread_mutex_lock(&rb->lock);
+ }
+#endif
+
+ cnte = rb_count_end(rb);
+ rbcount = rb_count(rb);
+
+ if (count > rbcount)
+ {
+ count = rbcount;
+ }
+
+ if (count > cnte)
+ {
+ rb->tail = count - cnte;
+ }
+ else
+ {
+ rb->tail += count;
+ rb->tail &= rb->count -1;
+ }
+
+#if ENABLE_THREADS
+ if ((rb->flags & O_NONBLOCK) == 0)
+ {
+ pthread_mutex_unlock(&rb->lock);
+ }
+#endif
+
+}
+
+
+/* ==========================================================================
Returns version of the library
========================================================================== */