diff options
author | Richard Braun <rbraun@sceen.net> | 2018-08-31 01:11:21 +0200 |
---|---|---|
committer | Richard Braun <rbraun@sceen.net> | 2018-08-31 01:11:21 +0200 |
commit | 92e969d589e5dc44ae8ea8d2c087289eaa1a3f96 (patch) | |
tree | 7668556ca28b5ff867c2c5a51c07f798267f10a5 /src | |
parent | 9d5114c068344962725b6d39319f4e68d855227d (diff) |
cbuf: new start/end assignment functions
Diffstat (limited to 'src')
-rw-r--r-- | src/cbuf.c | 23 | ||||
-rw-r--r-- | src/cbuf.h | 14 |
2 files changed, 35 insertions, 2 deletions
@@ -55,12 +55,19 @@ cbuf_index(const struct cbuf *cbuf, size_t abs_index) static void cbuf_update_start(struct cbuf *cbuf) { - /* Mind integer overflows */ if (cbuf_size(cbuf) > cbuf->capacity) { cbuf->start = cbuf->end - cbuf->capacity; } } +static void +cbuf_update_end(struct cbuf *cbuf) +{ + if (cbuf_size(cbuf) > cbuf->capacity) { + cbuf->end = cbuf->start + cbuf->capacity; + } +} + int cbuf_push(struct cbuf *cbuf, const void *buf, size_t size, bool erase) { @@ -199,3 +206,17 @@ cbuf_read(const struct cbuf *cbuf, size_t index, void *buf, size_t *sizep) memcpy(buf, start, size); return 0; } + +void +cbuf_set_start(struct cbuf *cbuf, size_t start) +{ + cbuf->start = start; + cbuf_update_start(cbuf); +} + +void +cbuf_set_end(struct cbuf *cbuf, size_t end) +{ + cbuf->end = end; + cbuf_update_end(cbuf); +} @@ -23,7 +23,7 @@ * http://git.sceen.net/rbraun/librbraun.git/ * * - * Circular byte buffer. + * FIFO circular byte buffer. */ #ifndef CBUF_H @@ -160,4 +160,16 @@ int cbuf_write(struct cbuf *cbuf, size_t index, const void *buf, size_t size); */ int cbuf_read(const struct cbuf *cbuf, size_t index, void *buf, size_t *sizep); +/* + * Set the value of the start/end index. + * + * These functions provide low level access to the circular buffer boundaries + * while making sure its size doesn't exceed its capacity. + * + * Users should try and find a higher level way to manipulate the circular + * buffer, and only resort to using these functions if there's no other choice. + */ +void cbuf_set_start(struct cbuf *cbuf, size_t start); +void cbuf_set_end(struct cbuf *cbuf, size_t end); + #endif /* CBUF_H */ |