diff options
Diffstat (limited to '')
-rw-r--r-- | src/libnetdata/ringbuffer/ringbuffer.c (renamed from src/aclk/mqtt_websockets/c-rbuf/cringbuffer.c) | 44 | ||||
-rw-r--r-- | src/libnetdata/ringbuffer/ringbuffer.h (renamed from src/aclk/mqtt_websockets/c-rbuf/cringbuffer.h) | 11 | ||||
-rw-r--r-- | src/libnetdata/ringbuffer/ringbuffer_internal.h | 26 |
3 files changed, 49 insertions, 32 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)) diff --git a/src/aclk/mqtt_websockets/c-rbuf/cringbuffer.h b/src/libnetdata/ringbuffer/ringbuffer.h index eb98035a..340112a8 100644 --- a/src/aclk/mqtt_websockets/c-rbuf/cringbuffer.h +++ b/src/libnetdata/ringbuffer/ringbuffer.h @@ -1,11 +1,10 @@ -// Copyright: SPDX-License-Identifier: GPL-3.0-only +// SPDX-License-Identifier: GPL-3.0-or-later -#ifndef CRINGBUFFER_H -#define CRINGBUFFER_H +#ifndef RINGBUFFER_H +#define RINGBUFFER_H +#include "../libnetdata.h" -#include <stddef.h> - -typedef struct rbuf_t *rbuf_t; +typedef struct rbuf *rbuf_t; rbuf_t rbuf_create(size_t size); void rbuf_free(rbuf_t buffer); diff --git a/src/libnetdata/ringbuffer/ringbuffer_internal.h b/src/libnetdata/ringbuffer/ringbuffer_internal.h new file mode 100644 index 00000000..0cc254aa --- /dev/null +++ b/src/libnetdata/ringbuffer/ringbuffer_internal.h @@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +#ifndef RINGBUFFER_INTERNAL_H +#define RINGBUFFER_INTERNAL_H + +#include "ringbuffer.h" + +struct rbuf { + char *data; + + // points to next byte where we can write + char *head; + // points to oldest (next to be poped) readable byte + char *tail; + + // to avoid calculating data + size + // all the time + char *end; + + size_t size; + size_t size_data; +}; + +typedef struct rbuf *rbuf_t; + +#endif |