diff options
Diffstat (limited to '')
-rw-r--r-- | src/libnetdata/ringbuffer/ringbuffer.c (renamed from src/aclk/mqtt_websockets/c-rbuf/cringbuffer.c) | 44 |
1 files changed, 18 insertions, 26 deletions
diff --git a/src/aclk/mqtt_websockets/c-rbuf/cringbuffer.c b/src/libnetdata/ringbuffer/ringbuffer.c index 8950c690..b30b3c39 100644 --- a/src/aclk/mqtt_websockets/c-rbuf/cringbuffer.c +++ b/src/libnetdata/ringbuffer/ringbuffer.c @@ -1,33 +1,14 @@ -// Copyright: SPDX-License-Identifier: GPL-3.0-only +// SPDX-License-Identifier: GPL-3.0-or-later -#include "cringbuffer.h" -#include "cringbuffer_internal.h" - -#include <stdlib.h> -#include <assert.h> -#include <string.h> - -#define MIN(a,b) (((a)<(b))?(a):(b)) -#define MAX(a,b) (((a)>(b))?(a):(b)) - -// this allows user to use their own -// custom memory allocation functions -#ifdef RBUF_CUSTOM_MALLOC -#include "../../helpers/ringbuffer_pal.h" -#else -#define crbuf_malloc(...) malloc(__VA_ARGS__) -#define crbuf_free(...) free(__VA_ARGS__) -#endif +#include "../libnetdata.h" +#include "ringbuffer_internal.h" rbuf_t rbuf_create(size_t size) { - rbuf_t buffer = crbuf_malloc(sizeof(struct rbuf_t) + size); - if (!buffer) - return NULL; - - memset(buffer, 0, sizeof(struct rbuf_t)); + rbuf_t buffer = mallocz(sizeof(struct rbuf) + size); + memset(buffer, 0, sizeof(struct rbuf)); - buffer->data = ((char*)buffer) + sizeof(struct rbuf_t); + buffer->data = ((char*)buffer) + sizeof(struct rbuf); buffer->head = buffer->data; buffer->tail = buffer->data; @@ -39,7 +20,7 @@ rbuf_t rbuf_create(size_t size) void rbuf_free(rbuf_t buffer) { - crbuf_free(buffer); + freez(buffer); } void rbuf_flush(rbuf_t buffer) @@ -81,6 +62,17 @@ int rbuf_bump_head(rbuf_t buffer, size_t bytes) return 1; } +int rbuf_bump_tail_noopt(rbuf_t buffer, size_t bytes) +{ + if (bytes > buffer->size_data) + return 0; + int i = buffer->tail - buffer->data; + buffer->tail = &buffer->data[(i + bytes) % buffer->size]; + buffer->size_data -= bytes; + + return 1; +} + int rbuf_bump_tail(rbuf_t buffer, size_t bytes) { if(!rbuf_bump_tail_noopt(buffer, bytes)) |