diff options
Diffstat (limited to 'libnetdata/circular_buffer/circular_buffer.c')
-rw-r--r-- | libnetdata/circular_buffer/circular_buffer.c | 30 |
1 files changed, 22 insertions, 8 deletions
diff --git a/libnetdata/circular_buffer/circular_buffer.c b/libnetdata/circular_buffer/circular_buffer.c index c791b420b0..b2bded1791 100644 --- a/libnetdata/circular_buffer/circular_buffer.c +++ b/libnetdata/circular_buffer/circular_buffer.c @@ -1,16 +1,24 @@ #include "../libnetdata.h" -struct circular_buffer *cbuffer_new(size_t initial, size_t max) { - struct circular_buffer *result = mallocz(sizeof(*result)); - result->size = initial; - result->data = mallocz(initial); - result->write = 0; - result->read = 0; - result->max_size = max; - return result; +struct circular_buffer *cbuffer_new(size_t initial, size_t max, size_t *statistics) { + struct circular_buffer *buf = mallocz(sizeof(struct circular_buffer)); + buf->size = initial; + buf->data = mallocz(initial); + buf->write = 0; + buf->read = 0; + buf->max_size = max; + buf->statistics = statistics; + + if(buf->statistics) + __atomic_add_fetch(buf->statistics, sizeof(struct circular_buffer) + buf->size, __ATOMIC_RELAXED); + + return buf; } void cbuffer_free(struct circular_buffer *buf) { + if(buf && buf->statistics) + __atomic_sub_fetch(buf->statistics, sizeof(struct circular_buffer) + buf->size, __ATOMIC_RELAXED); + freez(buf->data); freez(buf); } @@ -19,6 +27,8 @@ static int cbuffer_realloc_unsafe(struct circular_buffer *buf) { // Check that we can grow if (buf->size >= buf->max_size) return 1; + + size_t old_size = buf->size; size_t new_size = buf->size * 2; if (new_size > buf->max_size) new_size = buf->max_size; @@ -43,6 +53,10 @@ static int cbuffer_realloc_unsafe(struct circular_buffer *buf) { freez(buf->data); buf->data = new_data; buf->size = new_size; + + if(buf->statistics) + __atomic_add_fetch(buf->statistics, new_size - old_size, __ATOMIC_RELAXED); + return 0; } |