diff options
author | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2018-02-02 20:03:48 +0100 |
---|---|---|
committer | Michał Łyszczek <michal.lyszczek@bofc.pl> | 2018-02-02 20:03:48 +0100 |
commit | f4d9899c8d24d87644d4cc0c13c3bba9d8f81bc3 (patch) | |
tree | 30dcb691cfc4c29f369d81faf5d5d76606ed6ed3 | |
parent | 5934814945eb4cec144db95bd076814869dbb4f2 (diff) | |
download | librb-f4d9899c8d24d87644d4cc0c13c3bba9d8f81bc3.tar.gz librb-f4d9899c8d24d87644d4cc0c13c3bba9d8f81bc3.tar.bz2 librb-f4d9899c8d24d87644d4cc0c13c3bba9d8f81bc3.zip |
add: rb_discard function
-rw-r--r-- | include/rb.h | 1 | ||||
-rw-r--r-- | rb/rb.c | 55 |
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 *); @@ -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 ========================================================================== */ |