summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Braun <rbraun@sceen.net>2018-08-31 01:12:57 +0200
committerRichard Braun <rbraun@sceen.net>2018-08-31 01:12:57 +0200
commit027263f583353c7911aa04917f5c9283de16d822 (patch)
tree93759343f6abef682594aec31a72a47a0a667076
parent92e969d589e5dc44ae8ea8d2c087289eaa1a3f96 (diff)
cbuf: make read functions accept NULL output buffers
-rw-r--r--src/cbuf.c19
-rw-r--r--src/cbuf.h17
2 files changed, 28 insertions, 8 deletions
diff --git a/src/cbuf.c b/src/cbuf.c
index 0f9fd15..20d505d 100644
--- a/src/cbuf.c
+++ b/src/cbuf.c
@@ -128,7 +128,11 @@ cbuf_popb(struct cbuf *cbuf, void *bytep)
}
ptr = bytep;
- *ptr = cbuf->buf[cbuf_index(cbuf, cbuf->start)];
+
+ if (ptr) {
+ *ptr = cbuf->buf[cbuf_index(cbuf, cbuf->start)];
+ }
+
cbuf->start++;
return 0;
}
@@ -197,13 +201,20 @@ cbuf_read(const struct cbuf *cbuf, size_t index, void *buf, size_t *sizep)
size = *sizep;
} else {
size = buf_end - start;
- memcpy(buf, start, size);
- buf += size;
+
+ if (buf) {
+ memcpy(buf, start, size);
+ buf += size;
+ }
+
start = cbuf->buf;
size = *sizep - size;
}
- memcpy(buf, start, size);
+ if (buf) {
+ memcpy(buf, start, size);
+ }
+
return 0;
}
diff --git a/src/cbuf.h b/src/cbuf.h
index 577f57b..354cc34 100644
--- a/src/cbuf.h
+++ b/src/cbuf.h
@@ -117,10 +117,13 @@ int cbuf_push(struct cbuf *cbuf, const void *buf, size_t size, bool erase);
* Pop data from a circular buffer.
*
* On entry, the sizep argument points to the size of the output buffer.
- * On exit, it is updated to the number of bytes actually transferred.
+ * On return, it is updated to the number of bytes actually transferred.
*
- * If the buffer is empty, EAGAIN is returned, and the size of the
- * output buffer is undefined.
+ * If the buffer is empty, EAGAIN is returned, and the size of the output
+ * buffer is unmodified.
+ *
+ * The output buffer may be NULL, in which case this function acts as if
+ * it wasn't, but without writing output data.
*/
int cbuf_pop(struct cbuf *cbuf, void *buf, size_t *sizep);
@@ -136,6 +139,9 @@ int cbuf_pushb(struct cbuf *cbuf, uint8_t byte, bool erase);
* Pop a byte from a circular buffer.
*
* If the buffer is empty, EAGAIN is returned.
+ *
+ * The output byte pointer may be NULL, in which case this function acts
+ * as if it wasn't, but without writing output data.
*/
int cbuf_popb(struct cbuf *cbuf, void *bytep);
@@ -152,11 +158,14 @@ int cbuf_write(struct cbuf *cbuf, size_t index, const void *buf, size_t size);
* Read from a circular buffer at a specific location.
*
* On entry, the sizep argument points to the size of the output buffer.
- * On exit, it is updated to the number of bytes actually transferred.
+ * On return, it is updated to the number of bytes actually transferred.
*
* If the given index is outside buffer boundaries, EINVAL is returned.
*
* The circular buffer isn't changed by this operation.
+ *
+ * The output buffer may be NULL, in which case this function acts as if
+ * it wasn't, but without writing output data.
*/
int cbuf_read(const struct cbuf *cbuf, size_t index, void *buf, size_t *sizep);