summaryrefslogtreecommitdiffstats
path: root/libnetdata/circular_buffer
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2022-11-30 18:47:05 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2022-11-30 18:47:05 +0000
commit97e01009d69b8fbebfebf68f51e3d126d0ed43fc (patch)
tree02e8b836c3a9d89806f3e67d4a5fe9f52dbb0061 /libnetdata/circular_buffer
parentReleasing debian version 1.36.1-1. (diff)
downloadnetdata-97e01009d69b8fbebfebf68f51e3d126d0ed43fc.tar.xz
netdata-97e01009d69b8fbebfebf68f51e3d126d0ed43fc.zip
Merging upstream version 1.37.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'libnetdata/circular_buffer')
-rw-r--r--libnetdata/circular_buffer/circular_buffer.c11
-rw-r--r--libnetdata/circular_buffer/circular_buffer.h13
2 files changed, 19 insertions, 5 deletions
diff --git a/libnetdata/circular_buffer/circular_buffer.c b/libnetdata/circular_buffer/circular_buffer.c
index 998008db2..c791b420b 100644
--- a/libnetdata/circular_buffer/circular_buffer.c
+++ b/libnetdata/circular_buffer/circular_buffer.c
@@ -46,6 +46,11 @@ static int cbuffer_realloc_unsafe(struct circular_buffer *buf) {
return 0;
}
+size_t cbuffer_available_size_unsafe(struct circular_buffer *buf) {
+ size_t len = (buf->write >= buf->read) ? (buf->write - buf->read) : (buf->size - buf->read + buf->write);
+ return buf->max_size - len;
+}
+
int cbuffer_add_unsafe(struct circular_buffer *buf, const char *d, size_t d_len) {
size_t len = (buf->write >= buf->read) ? (buf->write - buf->read) : (buf->size - buf->read + buf->write);
while (d_len + len >= buf->size) {
@@ -78,8 +83,14 @@ void cbuffer_remove_unsafe(struct circular_buffer *buf, size_t num) {
size_t cbuffer_next_unsafe(struct circular_buffer *buf, char **start) {
if (start != NULL)
*start = buf->data + buf->read;
+
if (buf->read <= buf->write) {
return buf->write - buf->read; // Includes empty case
}
return buf->size - buf->read;
}
+
+void cbuffer_flush(struct circular_buffer*buf) {
+ buf->write = 0;
+ buf->read = 0;
+} \ No newline at end of file
diff --git a/libnetdata/circular_buffer/circular_buffer.h b/libnetdata/circular_buffer/circular_buffer.h
index ba37e0ebf..8c42aa807 100644
--- a/libnetdata/circular_buffer/circular_buffer.h
+++ b/libnetdata/circular_buffer/circular_buffer.h
@@ -8,9 +8,12 @@ struct circular_buffer {
char *data;
};
-extern struct circular_buffer *cbuffer_new(size_t initial, size_t max);
-extern void cbuffer_free(struct circular_buffer *buf);
-extern int cbuffer_add_unsafe(struct circular_buffer *buf, const char *d, size_t d_len);
-extern void cbuffer_remove_unsafe(struct circular_buffer *buf, size_t num);
-extern size_t cbuffer_next_unsafe(struct circular_buffer *buf, char **start);
+struct circular_buffer *cbuffer_new(size_t initial, size_t max);
+void cbuffer_free(struct circular_buffer *buf);
+int cbuffer_add_unsafe(struct circular_buffer *buf, const char *d, size_t d_len);
+void cbuffer_remove_unsafe(struct circular_buffer *buf, size_t num);
+size_t cbuffer_next_unsafe(struct circular_buffer *buf, char **start);
+size_t cbuffer_available_size_unsafe(struct circular_buffer *buf);
+void cbuffer_flush(struct circular_buffer*buf);
+
#endif