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 /rb | |
parent | 5934814945eb4cec144db95bd076814869dbb4f2 (diff) | |
download | librb-f4d9899c8d24d87644d4cc0c13c3bba9d8f81bc3.tar.gz librb-f4d9899c8d24d87644d4cc0c13c3bba9d8f81bc3.tar.bz2 librb-f4d9899c8d24d87644d4cc0c13c3bba9d8f81bc3.zip |
add: rb_discard function
Diffstat (limited to 'rb')
-rw-r--r-- | rb/rb.c | 55 |
1 files changed, 55 insertions, 0 deletions
@@ -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 ========================================================================== */ |