summaryrefslogtreecommitdiffstats
path: root/libnetdata/circular_buffer
diff options
context:
space:
mode:
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