ACLK code cleanup (#18417)
* Code cleanup * More cleanup
Stelios Fragkakis committed
Aug 29, 2024 at 09:25 UTC
1813d02d511ca6e66296cdc2ade3002f362fdf5c
27 files changed
+95
-766
CMakeLists.txt
+5
-8
@@ -615,6 +615,8 @@ set(LIBNETDATA_FILES
615
src/libnetdata/avl/avl.h
616
src/libnetdata/buffer/buffer.c
617
src/libnetdata/buffer/buffer.h
618
+ src/libnetdata/ringbuffer/ringbuffer.c
619
+ src/libnetdata/ringbuffer/ringbuffer.h
620
src/libnetdata/circular_buffer/circular_buffer.c
621
src/libnetdata/circular_buffer/circular_buffer.h
622
src/libnetdata/clocks/clocks.c
@@ -1298,12 +1300,9 @@ set(ACLK_ALWAYS_BUILD
1300
src/aclk/aclk_util.h
1301
src/aclk/https_client.c
1302
src/aclk/https_client.h
1301
- src/aclk/mqtt_websockets/c-rbuf/cringbuffer.c
1302
- src/aclk/mqtt_websockets/c-rbuf/cringbuffer.h
1303
- src/aclk/mqtt_websockets/c-rbuf/cringbuffer_internal.h
1304
- src/aclk/mqtt_websockets/c_rhash/c_rhash.c
1305
- src/aclk/mqtt_websockets/c_rhash/c_rhash.h
1306
- src/aclk/mqtt_websockets/c_rhash/c_rhash_internal.h
1303
+ src/libnetdata/c_rhash/c_rhash.c
1304
+ src/libnetdata/c_rhash/c_rhash.h
1305
+ src/libnetdata/c_rhash/c_rhash_internal.h
1306
)
1307
1308
set(TIMEX_PLUGIN_FILES
@@ -1546,8 +1545,6 @@ set(ACLK_FILES
1545
src/aclk/schema-wrappers/schema_wrapper_utils.h
1546
src/aclk/schema-wrappers/agent_cmds.cc
1547
src/aclk/schema-wrappers/agent_cmds.h
1549
- src/aclk/helpers/mqtt_wss_pal.h
1550
- src/aclk/helpers/ringbuffer_pal.h
1548
)
1549
1550
src/aclk/aclk_otp.c
-3
@@ -1,12 +1,9 @@
1
-
1
// SPDX-License-Identifier: GPL-3.0-or-later
2
3
#include "aclk_otp.h"
4
#include "aclk_util.h"
5
#include "aclk.h"
6
8
-#include "mqtt_websockets/c-rbuf/cringbuffer.h"
9
-
7
static int aclk_https_request(https_req_t *request, https_req_response_t *response) {
8
int rc;
9
// wrapper for ACLK only which loads ACLK specific proxy settings
src/aclk/helpers/mqtt_wss_pal.h
deleted
-13
@@ -1,13 +0,0 @@
1
-// SPDX-License-Identifier: GPL-3.0-or-later
2
-
3
-#ifndef MQTT_WSS_PAL_H
4
-#define MQTT_WSS_PAL_H
5
-
6
-#include "libnetdata/libnetdata.h"
7
-
8
-#undef OPENSSL_VERSION_095
9
-#undef OPENSSL_VERSION_097
10
-#undef OPENSSL_VERSION_110
11
-#undef OPENSSL_VERSION_111
12
-
13
-#endif /* MQTT_WSS_PAL_H */
src/aclk/helpers/ringbuffer_pal.h
deleted
-11
@@ -1,11 +0,0 @@
1
-// SPDX-License-Identifier: GPL-3.0-or-later
2
-
3
-#ifndef RINGBUFFER_PAL_H
4
-#define RINGBUFFER_PAL_H
5
-
6
-#include "libnetdata/libnetdata.h"
7
-
8
-#define crbuf_malloc(...) mallocz(__VA_ARGS__)
9
-#define crbuf_free(...) freez(__VA_ARGS__)
10
-
11
-#endif /* RINGBUFFER_PAL_H */
src/aclk/https_client.h
-3
@@ -5,9 +5,6 @@
5
6
#include "libnetdata/libnetdata.h"
7
8
-#include "mqtt_websockets/c-rbuf/cringbuffer.h"
9
-#include "mqtt_websockets/c_rhash/c_rhash.h"
10
-
8
typedef enum http_req_type {
9
HTTP_REQ_GET = 0,
10
HTTP_REQ_POST,
src/aclk/mqtt_websockets/.github/workflows/run-tests.yaml
deleted
-14
@@ -1,14 +0,0 @@
1
-name: run-tests
2
-on:
3
- push:
4
- schedule:
5
- - cron: '5 3 * * 0'
6
- pull_request:
7
-jobs:
8
- run-tests:
9
- runs-on: ubuntu-latest
10
- steps:
11
- - name: Install ruby and deps
12
- run: sudo apt-get install ruby ruby-dev mosquitto
13
- - name: Checkout
14
- uses: actions/checkout@v2
src/aclk/mqtt_websockets/.gitignore
deleted
-10
@@ -1,10 +0,0 @@
1
-build/*
2
-!build/.keep
3
-test
4
-.vscode
5
-mqtt/mqtt.c
6
-mqtt/include/mqtt.h
7
-libmqttwebsockets.*
8
-*.o
9
-.dirstamp
10
-.deps
src/aclk/mqtt_websockets/c-rbuf/cringbuffer_internal.h
deleted
-37
@@ -1,37 +0,0 @@
1
-// Copyright: SPDX-License-Identifier: GPL-3.0-only
2
-
3
-#ifndef CRINGBUFFER_INTERNAL_H
4
-#define CRINGBUFFER_INTERNAL_H
5
-
6
-struct rbuf_t {
7
- char *data;
8
-
9
- // points to next byte where we can write
10
- char *head;
11
- // points to oldest (next to be poped) readable byte
12
- char *tail;
13
-
14
- // to avoid calculating data + size
15
- // all the time
16
- char *end;
17
-
18
- size_t size;
19
- size_t size_data;
20
-};
21
-
22
-/* this exists so that it can be tested by unit tests
23
- * without optimization that resets head and tail to
24
- * beginning if buffer empty
25
- */
26
-inline static int rbuf_bump_tail_noopt(rbuf_t buffer, size_t bytes)
27
-{
28
- if (bytes > buffer->size_data)
29
- return 0;
30
- int i = buffer->tail - buffer->data;
31
- buffer->tail = &buffer->data[(i + bytes) % buffer->size];
32
- buffer->size_data -= bytes;
33
-
34
- return 1;
35
-}
36
-
37
-#endif
src/aclk/mqtt_websockets/c-rbuf/ringbuffer_test.c
deleted
-485
@@ -1,485 +0,0 @@
1
-// Copyright: SPDX-License-Identifier: GPL-3.0-only
2
-
3
-#include "ringbuffer.h"
4
-
5
-// to be able to access internals
6
-// never do this from app
7
-#include "../src/ringbuffer_internal.h"
8
-
9
-#include <stdio.h>
10
-#include <string.h>
11
-
12
-#define KNRM "\x1B[0m"
13
-#define KRED "\x1B[31m"
14
-#define KGRN "\x1B[32m"
15
-#define KYEL "\x1B[33m"
16
-#define KBLU "\x1B[34m"
17
-#define KMAG "\x1B[35m"
18
-#define KCYN "\x1B[36m"
19
-#define KWHT "\x1B[37m"
20
-
21
-#define UNUSED(x) (void)(x)
22
-
23
-int total_fails = 0;
24
-int total_tests = 0;
25
-int total_checks = 0;
26
-
27
-#define CHECK_EQ_RESULT(x, y) \
28
- while (s_len--) \
29
- putchar('.'); \
30
- printf("%s%s " KNRM "\n", (((x) == (y)) ? KGRN : KRED), (((x) == (y)) ? " PASS " : " FAIL ")); \
31
- if ((x) != (y)) \
32
- total_fails++; \
33
- total_checks++;
34
-
35
-#define CHECK_EQ_PREFIX(x, y, prefix, subtest_name, ...) \
36
- { \
37
- int s_len = \
38
- 100 - \
39
- printf(("Checking: " KWHT "%s %s%2d " subtest_name " " KNRM), __func__, prefix, subtest_no, ##__VA_ARGS__); \
40
- CHECK_EQ_RESULT(x, y) \
41
- }
42
-
43
-#define CHECK_EQ(x, y, subtest_name, ...) \
44
- { \
45
- int s_len = \
46
- 100 - printf(("Checking: " KWHT "%s %2d " subtest_name " " KNRM), __func__, subtest_no, ##__VA_ARGS__); \
47
- CHECK_EQ_RESULT(x, y) \
48
- }
49
-
50
-#define TEST_DECL() \
51
- int subtest_no = 0; \
52
- printf(KYEL "TEST SUITE: %s\n" KNRM, __func__); \
53
- total_tests++;
54
-
55
-static void test_rbuf_get_linear_insert_range()
56
-{
57
- TEST_DECL();
58
-
59
- // check empty buffer behaviour
60
- rbuf_t buff = rbuf_create(5);
61
- char *to_write;
62
- size_t ret;
63
- to_write = rbuf_get_linear_insert_range(buff, &ret);
64
- CHECK_EQ(ret, 5, "empty size");
65
- CHECK_EQ(to_write, buff->head, "empty write ptr");
66
- rbuf_free(buff);
67
-
68
- // check full buffer behaviour
69
- subtest_no++;
70
- buff = rbuf_create(5);
71
- ret = rbuf_bump_head(buff, 5);
72
- CHECK_EQ(ret, 1, "ret");
73
- to_write = rbuf_get_linear_insert_range(buff, &ret);
74
- CHECK_EQ(to_write, NULL, "writable NULL");
75
- CHECK_EQ(ret, 0, "writable count = 0");
76
-
77
- // check buffer flush
78
- subtest_no++;
79
- rbuf_flush(buff);
80
- CHECK_EQ(rbuf_bytes_free(buff), 5, "size_free");
81
- CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail");
82
- CHECK_EQ(buff->head, buff->data, "head_ptr");
83
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
84
-
85
- // check behaviour head > tail
86
- subtest_no++;
87
- rbuf_flush(buff);
88
- rbuf_bump_head(buff, 3);
89
- to_write = rbuf_get_linear_insert_range(buff, &ret);
90
- CHECK_EQ(to_write, buff->head, "write location");
91
- CHECK_EQ(ret, 2, "availible to linear write");
92
-
93
- // check behaviour tail > head
94
- subtest_no++;
95
- rbuf_flush(buff);
96
- rbuf_bump_head(buff, 5);
97
- rbuf_bump_tail(buff, 3);
98
- CHECK_EQ(buff->head, buff->data, "head_ptr");
99
- CHECK_EQ(buff->tail, buff->data + 3, "tail_ptr");
100
- to_write = rbuf_get_linear_insert_range(buff, &ret);
101
- CHECK_EQ(to_write, buff->head, "write location");
102
- CHECK_EQ(ret, 3, "availible to linear write");
103
-
104
-/* // check behaviour tail and head at last element
105
- subtest_no++;
106
- rbuf_flush(buff);
107
- rbuf_bump_head(buff, 4);
108
- rbuf_bump_tail(buff, 4);
109
- CHECK_EQ(buff->head, buff->end - 1, "head_ptr");
110
- CHECK_EQ(buff->tail, buff->end - 1, "tail_ptr");
111
- to_write = rbuf_get_linear_insert_range(buff, &ret);
112
- CHECK_EQ(to_write, buff->head, "write location");
113
- CHECK_EQ(ret, 1, "availible to linear write");*/
114
-
115
- // check behaviour tail and head at last element
116
- // after rbuf_bump_tail optimisation that restarts buffer
117
- // in case tail catches up with head
118
- subtest_no++;
119
- rbuf_flush(buff);
120
- rbuf_bump_head(buff, 4);
121
- rbuf_bump_tail(buff, 4);
122
- CHECK_EQ(buff->head, buff->data, "head_ptr");
123
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
124
- to_write = rbuf_get_linear_insert_range(buff, &ret);
125
- CHECK_EQ(to_write, buff->head, "write location");
126
- CHECK_EQ(ret, 5, "availible to linear write");
127
-}
128
-
129
-#define _CHECK_EQ(x, y, subtest_name, ...) CHECK_EQ_PREFIX(x, y, prefix, subtest_name, ##__VA_ARGS__)
130
-#define _PREFX "(size = %5zu) "
131
-static void test_rbuf_bump_head_bsize(size_t size)
132
-{
133
- char prefix[16];
134
- snprintf(prefix, 16, _PREFX, size);
135
- int subtest_no = 0;
136
- rbuf_t buff = rbuf_create(size);
137
- _CHECK_EQ(rbuf_bytes_free(buff), size, "size_free");
138
-
139
- subtest_no++;
140
- int ret = rbuf_bump_head(buff, size);
141
- _CHECK_EQ(buff->data, buff->head, "loc");
142
- _CHECK_EQ(ret, 1, "ret");
143
- _CHECK_EQ(buff->size_data, buff->size, "size");
144
- _CHECK_EQ(rbuf_bytes_free(buff), 0, "size_free");
145
-
146
- subtest_no++;
147
- ret = rbuf_bump_head(buff, 1);
148
- _CHECK_EQ(buff->data, buff->head, "loc no move");
149
- _CHECK_EQ(ret, 0, "ret error");
150
- _CHECK_EQ(buff->size_data, buff->size, "size");
151
- _CHECK_EQ(rbuf_bytes_free(buff), 0, "size_free");
152
- rbuf_free(buff);
153
-
154
- subtest_no++;
155
- buff = rbuf_create(size);
156
- ret = rbuf_bump_head(buff, size - 1);
157
- _CHECK_EQ(buff->head, buff->end-1, "loc end");
158
- rbuf_free(buff);
159
-}
160
-#undef _CHECK_EQ
161
-
162
-static void test_rbuf_bump_head()
163
-{
164
- TEST_DECL();
165
- UNUSED(subtest_no);
166
-
167
- size_t test_sizes[] = { 1, 2, 3, 5, 6, 7, 8, 100, 99999, 0 };
168
- for (int i = 0; test_sizes[i]; i++)
169
- test_rbuf_bump_head_bsize(test_sizes[i]);
170
-}
171
-
172
-static void test_rbuf_bump_tail_noopt(int subtest_no)
173
-{
174
- rbuf_t buff = rbuf_create(10);
175
- CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free");
176
- CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail");
177
-
178
- subtest_no++;
179
- int ret = rbuf_bump_head(buff, 5);
180
- CHECK_EQ(ret, 1, "ret");
181
- CHECK_EQ(rbuf_bytes_free(buff), 5, "size_free");
182
- CHECK_EQ(rbuf_bytes_available(buff), 5, "size_avail");
183
- CHECK_EQ(buff->head, buff->data + 5, "head_ptr");
184
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
185
-
186
- subtest_no++;
187
- ret = rbuf_bump_tail_noopt(buff, 2);
188
- CHECK_EQ(ret, 1, "ret");
189
- CHECK_EQ(rbuf_bytes_available(buff), 3, "size_avail");
190
- CHECK_EQ(rbuf_bytes_free(buff), 7, "size_free");
191
- CHECK_EQ(buff->head, buff->data + 5, "head_ptr");
192
- CHECK_EQ(buff->tail, buff->data + 2, "tail_ptr");
193
-
194
- subtest_no++;
195
- ret = rbuf_bump_tail_noopt(buff, 3);
196
- CHECK_EQ(ret, 1, "ret");
197
- CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail");
198
- CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free");
199
- CHECK_EQ(buff->head, buff->data + 5, "head_ptr");
200
- CHECK_EQ(buff->tail, buff->data + 5, "tail_ptr");
201
-
202
- subtest_no++;
203
- ret = rbuf_bump_tail_noopt(buff, 1);
204
- CHECK_EQ(ret, 0, "ret");
205
- CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail");
206
- CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free");
207
- CHECK_EQ(buff->head, buff->data + 5, "head_ptr");
208
- CHECK_EQ(buff->tail, buff->data + 5, "tail_ptr");
209
-
210
- subtest_no++;
211
- ret = rbuf_bump_head(buff, 7);
212
- CHECK_EQ(ret, 1, "ret");
213
- CHECK_EQ(rbuf_bytes_available(buff), 7, "size_avail");
214
- CHECK_EQ(rbuf_bytes_free(buff), 3, "size_free");
215
- CHECK_EQ(buff->head, buff->data + 2, "head_ptr");
216
- CHECK_EQ(buff->tail, buff->data + 5, "tail_ptr");
217
-
218
- subtest_no++;
219
- ret = rbuf_bump_tail_noopt(buff, 5);
220
- CHECK_EQ(ret, 1, "ret");
221
- CHECK_EQ(rbuf_bytes_available(buff), 2, "size_avail");
222
- CHECK_EQ(rbuf_bytes_free(buff), 8, "size_free");
223
- CHECK_EQ(buff->head, buff->data + 2, "head_ptr");
224
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
225
-
226
- // check tail can't overrun head
227
- subtest_no++;
228
- ret = rbuf_bump_tail_noopt(buff, 3);
229
- CHECK_EQ(ret, 0, "ret");
230
- CHECK_EQ(rbuf_bytes_available(buff), 2, "size_avail");
231
- CHECK_EQ(rbuf_bytes_free(buff), 8, "size_free");
232
- CHECK_EQ(buff->head, buff->data + 2, "head_ptr");
233
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
234
-
235
- // check head can't overrun tail
236
- subtest_no++;
237
- ret = rbuf_bump_head(buff, 9);
238
- CHECK_EQ(ret, 0, "ret");
239
- CHECK_EQ(rbuf_bytes_available(buff), 2, "size_avail");
240
- CHECK_EQ(rbuf_bytes_free(buff), 8, "size_free");
241
- CHECK_EQ(buff->head, buff->data + 2, "head_ptr");
242
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
243
-
244
- // check head can fill the buffer
245
- subtest_no++;
246
- ret = rbuf_bump_head(buff, 8);
247
- CHECK_EQ(ret, 1, "ret");
248
- CHECK_EQ(rbuf_bytes_available(buff), 10, "size_avail");
249
- CHECK_EQ(rbuf_bytes_free(buff), 0, "size_free");
250
- CHECK_EQ(buff->head, buff->data, "head_ptr");
251
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
252
-
253
- // check can empty the buffer
254
- subtest_no++;
255
- ret = rbuf_bump_tail_noopt(buff, 10);
256
- CHECK_EQ(ret, 1, "ret");
257
- CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail");
258
- CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free");
259
- CHECK_EQ(buff->head, buff->data, "head_ptr");
260
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
261
-}
262
-
263
-static void test_rbuf_bump_tail_opt(int subtest_no)
264
-{
265
- subtest_no++;
266
- rbuf_t buff = rbuf_create(10);
267
- CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free");
268
- CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail");
269
-
270
- subtest_no++;
271
- int ret = rbuf_bump_head(buff, 5);
272
- CHECK_EQ(ret, 1, "ret");
273
- CHECK_EQ(rbuf_bytes_free(buff), 5, "size_free");
274
- CHECK_EQ(rbuf_bytes_available(buff), 5, "size_avail");
275
- CHECK_EQ(buff->head, buff->data + 5, "head_ptr");
276
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
277
-
278
- subtest_no++;
279
- ret = rbuf_bump_tail(buff, 2);
280
- CHECK_EQ(ret, 1, "ret");
281
- CHECK_EQ(rbuf_bytes_available(buff), 3, "size_avail");
282
- CHECK_EQ(rbuf_bytes_free(buff), 7, "size_free");
283
- CHECK_EQ(buff->head, buff->data + 5, "head_ptr");
284
- CHECK_EQ(buff->tail, buff->data + 2, "tail_ptr");
285
-
286
- subtest_no++;
287
- ret = rbuf_bump_tail(buff, 3);
288
- CHECK_EQ(ret, 1, "ret");
289
- CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail");
290
- CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free");
291
- CHECK_EQ(buff->head, buff->data, "head_ptr");
292
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
293
-
294
- subtest_no++;
295
- ret = rbuf_bump_tail_noopt(buff, 1);
296
- CHECK_EQ(ret, 0, "ret");
297
- CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail");
298
- CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free");
299
- CHECK_EQ(buff->head, buff->data, "head_ptr");
300
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
301
-
302
- subtest_no++;
303
- ret = rbuf_bump_head(buff, 6);
304
- ret = rbuf_bump_tail(buff, 5);
305
- ret = rbuf_bump_head(buff, 6);
306
- CHECK_EQ(ret, 1, "ret");
307
- CHECK_EQ(rbuf_bytes_available(buff), 7, "size_avail");
308
- CHECK_EQ(rbuf_bytes_free(buff), 3, "size_free");
309
- CHECK_EQ(buff->head, buff->data + 2, "head_ptr");
310
- CHECK_EQ(buff->tail, buff->data + 5, "tail_ptr");
311
-
312
- subtest_no++;
313
- ret = rbuf_bump_tail(buff, 5);
314
- CHECK_EQ(ret, 1, "ret");
315
- CHECK_EQ(rbuf_bytes_available(buff), 2, "size_avail");
316
- CHECK_EQ(rbuf_bytes_free(buff), 8, "size_free");
317
- CHECK_EQ(buff->head, buff->data + 2, "head_ptr");
318
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
319
-
320
- // check tail can't overrun head
321
- subtest_no++;
322
- ret = rbuf_bump_tail(buff, 3);
323
- CHECK_EQ(ret, 0, "ret");
324
- CHECK_EQ(rbuf_bytes_available(buff), 2, "size_avail");
325
- CHECK_EQ(rbuf_bytes_free(buff), 8, "size_free");
326
- CHECK_EQ(buff->head, buff->data + 2, "head_ptr");
327
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
328
-
329
- // check head can't overrun tail
330
- subtest_no++;
331
- ret = rbuf_bump_head(buff, 9);
332
- CHECK_EQ(ret, 0, "ret");
333
- CHECK_EQ(rbuf_bytes_available(buff), 2, "size_avail");
334
- CHECK_EQ(rbuf_bytes_free(buff), 8, "size_free");
335
- CHECK_EQ(buff->head, buff->data + 2, "head_ptr");
336
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
337
-
338
- // check head can fill the buffer
339
- subtest_no++;
340
- ret = rbuf_bump_head(buff, 8);
341
- CHECK_EQ(ret, 1, "ret");
342
- CHECK_EQ(rbuf_bytes_available(buff), 10, "size_avail");
343
- CHECK_EQ(rbuf_bytes_free(buff), 0, "size_free");
344
- CHECK_EQ(buff->head, buff->data, "head_ptr");
345
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
346
-
347
- // check can empty the buffer
348
- subtest_no++;
349
- ret = rbuf_bump_tail(buff, 10);
350
- CHECK_EQ(ret, 1, "ret");
351
- CHECK_EQ(rbuf_bytes_available(buff), 0, "size_avail");
352
- CHECK_EQ(rbuf_bytes_free(buff), 10, "size_free");
353
- CHECK_EQ(buff->head, buff->data, "head_ptr");
354
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
355
-}
356
-
357
-static void test_rbuf_bump_tail()
358
-{
359
- TEST_DECL();
360
- test_rbuf_bump_tail_noopt(subtest_no);
361
- test_rbuf_bump_tail_opt(subtest_no);
362
-}
363
-
364
-#define ASCII_A 0x61
365
-#define ASCII_Z 0x7A
366
-#define TEST_DATA_SIZE ASCII_Z-ASCII_A+1
367
-static void test_rbuf_push()
368
-{
369
- TEST_DECL();
370
- rbuf_t buff = rbuf_create(10);
371
- int i;
372
- char test_data[TEST_DATA_SIZE];
373
-
374
- for (int i = 0; i <= TEST_DATA_SIZE; i++)
375
- test_data[i] = i + ASCII_A;
376
-
377
- int ret = rbuf_push(buff, test_data, 10);
378
- CHECK_EQ(ret, 10, "written 10 bytes");
379
- CHECK_EQ(rbuf_bytes_free(buff), 0, "empty size == 0");
380
- for (i = 0; i < 10; i++)
381
- CHECK_EQ(buff->data[i], i + ASCII_A, "Check data");
382
-
383
- subtest_no++;
384
- rbuf_flush(buff);
385
- rbuf_bump_head(buff, 5);
386
- rbuf_bump_tail_noopt(buff, 5); //to not reset both pointers to beginning
387
- ret = rbuf_push(buff, test_data, 10);
388
- CHECK_EQ(ret, 10, "written 10 bytes");
389
- for (i = 0; i < 10; i++)
390
- CHECK_EQ(buff->data[i], ((i+5)%10) + ASCII_A, "Check Data");
391
-
392
- subtest_no++;
393
- rbuf_flush(buff);
394
- rbuf_bump_head(buff, 9);
395
- rbuf_bump_tail_noopt(buff, 9);
396
- ret = rbuf_push(buff, test_data, 10);
397
- CHECK_EQ(ret, 10, "written 10 bytes");
398
- for (i = 0; i < 10; i++)
399
- CHECK_EQ(buff->data[i], ((i + 1) % 10) + ASCII_A, "Check data");
400
-
401
- // let tail > head
402
- subtest_no++;
403
- rbuf_flush(buff);
404
- rbuf_bump_head(buff, 9);
405
- rbuf_bump_tail_noopt(buff, 9);
406
- rbuf_bump_head(buff, 1);
407
- ret = rbuf_push(buff, test_data, 9);
408
- CHECK_EQ(ret, 9, "written 9 bytes");
409
- CHECK_EQ(buff->head, buff->end - 1, "head_ptr");
410
- CHECK_EQ(buff->tail, buff->head, "tail_ptr");
411
- rbuf_bump_tail(buff, 1);
412
- //TODO push byte can be usefull optimisation
413
- ret = rbuf_push(buff, &test_data[9], 1);
414
- CHECK_EQ(ret, 1, "written 1 byte");
415
- CHECK_EQ(rbuf_bytes_free(buff), 0, "empty size == 0");
416
- for (i = 0; i < 10; i++)
417
- CHECK_EQ(buff->data[i], i + ASCII_A, "Check data");
418
-
419
- subtest_no++;
420
- rbuf_flush(buff);
421
- rbuf_bump_head(buff, 9);
422
- rbuf_bump_tail_noopt(buff, 7);
423
- rbuf_bump_head(buff, 1);
424
- ret = rbuf_push(buff, test_data, 7);
425
- CHECK_EQ(ret, 7, "written 7 bytes");
426
- CHECK_EQ(buff->head, buff->data + 7, "head_ptr");
427
- CHECK_EQ(buff->tail, buff->head, "tail_ptr");
428
- rbuf_bump_tail(buff, 3);
429
- CHECK_EQ(buff->tail, buff->data, "tail_ptr");
430
- //TODO push byte can be usefull optimisation
431
- ret = rbuf_push(buff, &test_data[7], 3);
432
- CHECK_EQ(ret, 3, "written 3 bytes");
433
- CHECK_EQ(rbuf_bytes_free(buff), 0, "empty size == 0");
434
- for (i = 0; i < 10; i++)
435
- CHECK_EQ(buff->data[i], i + ASCII_A, "Check data");
436
-
437
- // test can't overfill the buffer
438
- subtest_no++;
439
- rbuf_flush(buff);
440
- rbuf_push(buff, test_data, TEST_DATA_SIZE);
441
- CHECK_EQ(ret, 3, "written 10 bytes");
442
- for (i = 0; i < 10; i++)
443
- CHECK_EQ(buff->data[i], i + ASCII_A, "Check data");
444
-}
445
-
446
-#define TEST_RBUF_FIND_BYTES_SIZE 10
447
-void test_rbuf_find_bytes()
448
-{
449
- TEST_DECL();
450
- rbuf_t buff = rbuf_create(TEST_RBUF_FIND_BYTES_SIZE);
451
- char *filler_3 = " ";
452
- char *needle = "needle";
453
- int idx;
454
- char *ptr;
455
-
456
- // make sure needle is wrapped aroung in the buffer
457
- // to test we still can find it
458
- // target "edle ne"
459
- rbuf_bump_head(buff, TEST_RBUF_FIND_BYTES_SIZE / 2);
460
- rbuf_push(buff, filler_3, strlen(filler_3));
461
- rbuf_bump_tail(buff, TEST_RBUF_FIND_BYTES_SIZE / 2);
462
- rbuf_push(buff, needle, strlen(needle));
463
- ptr = rbuf_find_bytes(buff, needle, strlen(needle), &idx);
464
- CHECK_EQ(ptr, buff->data + (TEST_RBUF_FIND_BYTES_SIZE / 2) + strlen(filler_3), "Pointer to needle correct");
465
- CHECK_EQ(idx, ptr - buff->tail, "Check needle index");
466
-}
467
-
468
-int main()
469
-{
470
- test_rbuf_bump_head();
471
- test_rbuf_bump_tail();
472
- test_rbuf_get_linear_insert_range();
473
- test_rbuf_push();
474
- test_rbuf_find_bytes();
475
-
476
- printf(
477
- KNRM "Total Tests %d, Total Checks %d, Successful Checks %d, Failed Checks %d\n",
478
- total_tests, total_checks, total_checks - total_fails, total_fails);
479
- if (total_fails)
480
- printf(KRED "!!!Some test(s) Failed!!!\n");
481
- else
482
- printf(KGRN "ALL TESTS PASSED\n");
483
-
484
- return total_fails;
485
-}
src/aclk/mqtt_websockets/common_internal.h
+1
-16
@@ -1,27 +1,12 @@
1
-// SPDX-License-Identifier: GPL-3.0-only
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
3
#ifndef COMMON_INTERNAL_H
4
#define COMMON_INTERNAL_H
5
6
#include "endian_compat.h"
7
8
-#ifdef MQTT_WSS_CUSTOM_ALLOC
9
-#include "../helpers/mqtt_wss_pal.h"
10
-#else
11
-#define mw_malloc(...) malloc(__VA_ARGS__)
12
-#define mw_calloc(...) calloc(__VA_ARGS__)
13
-#define mw_free(...) free(__VA_ARGS__)
14
-#define mw_strdup(...) strdup(__VA_ARGS__)
15
-#define mw_realloc(...) realloc(__VA_ARGS__)
16
-#endif
17
-
8
#ifndef MQTT_WSS_FRAG_MEMALIGN
9
#define MQTT_WSS_FRAG_MEMALIGN (8)
10
#endif
11
22
-#define OPENSSL_VERSION_095 0x00905100L
23
-#define OPENSSL_VERSION_097 0x00907000L
24
-#define OPENSSL_VERSION_110 0x10100000L
25
-#define OPENSSL_VERSION_111 0x10101000L
26
-
12
#endif /* COMMON_INTERNAL_H */
src/aclk/mqtt_websockets/common_public.h
+2
@@ -1,3 +1,5 @@
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
+
3
#ifndef MQTT_WEBSOCKETS_COMMON_PUBLIC_H
4
#define MQTT_WEBSOCKETS_COMMON_PUBLIC_H
5
src/aclk/mqtt_websockets/mqtt_ng.c
+6
-15
@@ -1,16 +1,10 @@
1
-// Copyright: SPDX-License-Identifier: GPL-3.0-only
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
3
#ifndef _GNU_SOURCE
4
#define _GNU_SOURCE
5
#endif
6
7
-#include <stdint.h>
8
-#include <stdlib.h>
9
-#include <string.h>
10
-#include <pthread.h>
11
-#include <inttypes.h>
12
-
13
-#include "c_rhash/c_rhash.h"
7
+#include "libnetdata/libnetdata.h"
8
9
#include "common_internal.h"
10
#include "mqtt_constants.h"
@@ -26,10 +20,8 @@
20
21
#define SMALL_STRING_DONT_FRAGMENT_LIMIT 128
22
29
-#define MIN(a,b) (((a)<(b))?(a):(b))
30
-
31
-#define LOCK_HDR_BUFFER(buffer) pthread_mutex_lock(&((buffer)->mutex))
32
-#define UNLOCK_HDR_BUFFER(buffer) pthread_mutex_unlock(&((buffer)->mutex))
23
+#define LOCK_HDR_BUFFER(buffer) spinlock_lock(&((buffer)->spinlock))
24
+#define UNLOCK_HDR_BUFFER(buffer) spinlock_unlock(&((buffer)->spinlock))
25
26
#define BUFFER_FRAG_GARBAGE_COLLECT 0x01
27
// some packets can be marked for garbage collection
@@ -75,7 +67,7 @@ struct transaction_buffer {
67
// to be able to revert state easily
68
// in case of error mid processing
69
struct header_buffer state_backup;
78
- pthread_mutex_t mutex;
70
+ SPINLOCK spinlock;
71
struct buffer_fragment *sending_frag;
72
};
73
@@ -578,7 +570,7 @@ static int transaction_buffer_grow(struct transaction_buffer *buf, mqtt_wss_log_
570
571
inline static int transaction_buffer_init(struct transaction_buffer *to_init, size_t size)
572
{
581
- pthread_mutex_init(&to_init->mutex, NULL);
573
+ spinlock_init(&to_init->spinlock);
574
575
to_init->hdr_buffer.size = size;
576
to_init->hdr_buffer.data = mallocz(size);
@@ -593,7 +585,6 @@ inline static int transaction_buffer_init(struct transaction_buffer *to_init, si
585
static void transaction_buffer_destroy(struct transaction_buffer *to_init)
586
{
587
buffer_purge(&to_init->hdr_buffer);
596
- pthread_mutex_destroy(&to_init->mutex);
588
freez(to_init->hdr_buffer.data);
589
}
590
src/aclk/mqtt_websockets/mqtt_ng.h
+1
-6
@@ -1,10 +1,5 @@
1
-// Copyright: SPDX-License-Identifier: GPL-3.0-only
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
3
-#include <stdint.h>
4
-#include <sys/types.h>
5
-#include <time.h>
6
-
7
-#include "c-rbuf/cringbuffer.h"
3
#include "common_public.h"
4
5
#define MQTT_NG_MSGGEN_OK 0
src/aclk/mqtt_websockets/mqtt_wss_client.c
+2
-14
@@ -1,27 +1,15 @@
1
-// SPDX-License-Identifier: GPL-3.0-only
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
3
#ifndef _GNU_SOURCE
4
#define _GNU_SOURCE
5
#endif
6
7
+#include "libnetdata/libnetdata.h"
8
#include "mqtt_wss_client.h"
9
#include "mqtt_ng.h"
10
#include "ws_client.h"
11
#include "common_internal.h"
12
12
-#include <stdlib.h>
13
-#include <fcntl.h>
14
-#include <unistd.h>
15
-#include <poll.h>
16
-#include <string.h>
17
-#include <time.h>
18
-
19
-#include <sys/socket.h>
20
-#include <netinet/in.h>
21
-
22
-#include <openssl/err.h>
23
-#include <openssl/ssl.h>
24
-
13
#define PIPE_READ_END 0
14
#define PIPE_WRITE_END 1
15
#define POLLFD_SOCKET 0
src/aclk/mqtt_websockets/mqtt_wss_client.h
+1
-5
@@ -1,12 +1,8 @@
1
-// SPDX-License-Identifier: GPL-3.0-only
2
-// Copyright (C) 2020 Timotej Šiškovič
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
3
#ifndef MQTT_WSS_CLIENT_H
4
#define MQTT_WSS_CLIENT_H
5
7
-#include <stdint.h>
8
-#include <stddef.h> //size_t
9
-
6
#include "mqtt_wss_log.h"
7
#include "common_public.h"
8
src/aclk/mqtt_websockets/mqtt_wss_log.c
+2
-6
@@ -1,12 +1,8 @@
1
-// Copyright: SPDX-License-Identifier: GPL-3.0-only
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
3
-#include <stdlib.h>
4
-#include <stdarg.h>
5
-#include <string.h>
6
-#include <stdio.h>
3
+#include "libnetdata/libnetdata.h"
4
5
#include "mqtt_wss_log.h"
9
-#include "common_internal.h"
6
7
struct mqtt_wss_log_ctx {
8
mqtt_wss_log_callback_t extern_log_fnc;
src/aclk/mqtt_websockets/ws_client.c
+3
-24
@@ -1,31 +1,10 @@
1
-// Copyright (C) 2020 Timotej Šiškovič
2
-// SPDX-License-Identifier: GPL-3.0-only
3
-//
4
-// This program is free software: you can redistribute it and/or modify it
5
-// under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.
6
-//
7
-// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8
-// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9
-// See the GNU General Public License for more details.
10
-//
11
-// You should have received a copy of the GNU General Public License along with this program.
12
-// If not, see <https://www.gnu.org/licenses/>.
13
-
14
-#include <fcntl.h>
15
-#include <unistd.h>
16
-#include <string.h>
17
-#include <errno.h>
18
-#include <ctype.h>
19
-
20
-#include <openssl/evp.h>
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
+
3
+#include "libnetdata/libnetdata.h"
4
5
#include "ws_client.h"
6
#include "common_internal.h"
7
25
-#ifdef MQTT_WEBSOCKETS_DEBUG
26
-#include "../c-rbuf/src/ringbuffer_internal.h"
27
-#endif
28
-
8
#define UNIT_LOG_PREFIX "ws_client: "
9
#define FATAL(fmt, ...) mws_fatal(client->log, UNIT_LOG_PREFIX fmt, ##__VA_ARGS__)
10
#define ERROR(fmt, ...) mws_error(client->log, UNIT_LOG_PREFIX fmt, ##__VA_ARGS__)
src/aclk/mqtt_websockets/ws_client.h
+1
-5
@@ -1,14 +1,10 @@
1
-// SPDX-License-Identifier: GPL-3.0-only
2
-// Copyright (C) 2020 Timotej Šiškovič
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
3
#ifndef WS_CLIENT_H
4
#define WS_CLIENT_H
5
7
-#include "c-rbuf/cringbuffer.h"
6
#include "mqtt_wss_log.h"
7
10
-#include <stdint.h>
11
-
8
#define WS_CLIENT_NEED_MORE_BYTES 0x10
9
#define WS_CLIENT_PARSING_DONE 0x11
10
#define WS_CLIENT_CONNECTION_CLOSED 0x12
src/libnetdata/c_rhash/c_rhash.c
renamed
+12
-54
@@ -1,33 +1,13 @@
1
-// Copyright: SPDX-License-Identifier: GPL-3.0-only
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
3
+#include "../libnetdata.h"
4
#include "c_rhash_internal.h"
5
5
-#include <stdlib.h>
6
-#include <string.h>
7
-
8
-#ifdef DEBUG_VERBOSE
9
-#include <stdio.h>
10
-#endif
11
-
12
-#define c_rmalloc(...) malloc(__VA_ARGS__)
13
-#define c_rcalloc(...) calloc(__VA_ARGS__)
14
-#define c_rfree(...) free(__VA_ARGS__)
15
-
16
-static inline uint32_t simple_hash(const char *name) {
17
- unsigned char *s = (unsigned char *) name;
18
- uint32_t hval = 0x811c9dc5;
19
- while (*s) {
20
- hval *= 16777619;
21
- hval ^= (uint32_t) *s++;
22
- }
23
- return hval;
24
-}
25
-
6
c_rhash c_rhash_new(size_t bin_count) {
7
if (!bin_count)
8
bin_count = 1000;
9
30
- c_rhash hash = c_rcalloc(1, sizeof(struct c_rhash_s) + (bin_count * sizeof(struct bin_ll*)) );
10
+ c_rhash hash = callocz(1, sizeof(struct c_rhash_s) + (bin_count * sizeof(struct bin_ll*)) );
11
if (hash == NULL)
12
return NULL;
13
@@ -75,16 +55,9 @@ static int insert_into_bin(c_rhash_bin *bin, uint8_t key_type, const void *key,
55
struct bin_item *prev = NULL;
56
while (*bin != NULL) {
57
if (!compare_bin_item(*bin, key_type, key)) {
78
-#ifdef DEBUG_VERBOSE
79
- printf("Key already present! Updating value!\n");
80
-#endif
81
-// TODO: optimize here if the new value is of different kind compared to the old one
82
-// in case it is not crazily bigger we can reuse the memory and avoid malloc and free
83
- c_rfree((*bin)->value);
58
+ freez((*bin)->value);
59
(*bin)->value_type = value_type;
85
- (*bin)->value = c_rmalloc(get_itemtype_len(value_type, value));
86
- if ((*bin)->value == NULL)
87
- return 1;
60
+ (*bin)->value = mallocz(get_itemtype_len(value_type, value));
61
memcpy((*bin)->value, value, get_itemtype_len(value_type, value));
62
return 0;
63
}
@@ -93,18 +66,18 @@ static int insert_into_bin(c_rhash_bin *bin, uint8_t key_type, const void *key,
66
}
67
68
if (*bin == NULL)
96
- *bin = c_rcalloc(1, sizeof(struct bin_item));
69
+ *bin = callocz(1, sizeof(struct bin_item));
70
if (prev != NULL)
71
prev->next = *bin;
72
73
(*bin)->key_type = key_type;
74
size_t len = get_itemtype_len(key_type, key);
102
- (*bin)->key = c_rmalloc(len);
75
+ (*bin)->key = mallocz(len);
76
memcpy((*bin)->key, key, len);
77
78
(*bin)->value_type = value_type;
79
len = get_itemtype_len(value_type, value);
107
- (*bin)->value = c_rmalloc(len);
80
+ (*bin)->value = mallocz(len);
81
memcpy((*bin)->value, value, len);
82
return 0;
83
}
@@ -121,33 +94,18 @@ static inline c_rhash_bin *get_binptr_by_str(c_rhash hash, const char *key) {
94
int c_rhash_insert_str_ptr(c_rhash hash, const char *key, void *value) {
95
c_rhash_bin *bin = get_binptr_by_str(hash, key);
96
124
-#ifdef DEBUG_VERBOSE
125
- if (bin != NULL)
126
- printf("COLLISION. There will be more than one item in bin idx=%d\n", nhash);
127
-#endif
128
-
97
return insert_into_bin(bin, ITEMTYPE_STRING, key, ITEMTYPE_OPAQUE_PTR, &value);
98
}
99
100
int c_rhash_insert_str_uint8(c_rhash hash, const char *key, uint8_t value) {
101
c_rhash_bin *bin = get_binptr_by_str(hash, key);
102
135
-#ifdef DEBUG_VERBOSE
136
- if (bin != NULL)
137
- printf("COLLISION. There will be more than one item in bin idx=%d\n", nhash);
138
-#endif
139
-
103
return insert_into_bin(bin, ITEMTYPE_STRING, key, ITEMTYPE_UINT8, &value);
104
}
105
106
int c_rhash_insert_uint64_ptr(c_rhash hash, uint64_t key, void *value) {
107
c_rhash_bin *bin = &hash->bins[key % hash->bin_count];
108
146
-#ifdef DEBUG_VERBOSE
147
- if (bin != NULL)
148
- printf("COLLISION. There will be more than one item in bin idx=%d\n", nhash);
149
-#endif
150
-
109
return insert_into_bin(bin, ITEMTYPE_UINT64, &key, ITEMTYPE_OPAQUE_PTR, &value);
110
}
111
@@ -208,9 +166,9 @@ static void c_rhash_destroy_bin(c_rhash_bin bin) {
166
struct bin_item *next;
167
do {
168
next = bin->next;
211
- c_rfree(bin->key);
212
- c_rfree(bin->value);
213
- c_rfree(bin);
169
+ freez(bin->key);
170
+ freez(bin->value);
171
+ freez(bin);
172
bin = next;
173
} while (bin != NULL);
174
}
@@ -260,5 +218,5 @@ void c_rhash_destroy(c_rhash hash) {
218
if (hash->bins[i] != NULL)
219
c_rhash_destroy_bin(hash->bins[i]);
220
}
263
- c_rfree(hash);
221
+ freez(hash);
222
}
src/libnetdata/c_rhash/c_rhash.h
renamed
+6
-4
@@ -1,8 +1,8 @@
1
-// Copyright: SPDX-License-Identifier: GPL-3.0-only
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
3
-#include <sys/types.h>
4
-#include <stdint.h>
5
-#include <stddef.h>
3
+#ifndef C_RHASH_H
4
+#define C_RHASH_H
5
+#include "../libnetdata.h"
6
7
#ifndef DEFAULT_BIN_COUNT
8
#define DEFAULT_BIN_COUNT 1000
@@ -59,3 +59,5 @@ typedef struct {
59
int c_rhash_iter_uint64_keys(c_rhash hash, c_rhash_iter_t *iter, uint64_t *key);
60
61
int c_rhash_iter_str_keys(c_rhash hash, c_rhash_iter_t *iter, const char **key);
62
+
63
+#endif
src/libnetdata/c_rhash/c_rhash_internal.h
renamed
+1
-1
@@ -1,4 +1,4 @@
1
-// Copyright: SPDX-License-Identifier: GPL-3.0-only
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
3
#include "c_rhash.h"
4
src/libnetdata/c_rhash/tests.c
renamed
+1
-1
@@ -1,4 +1,4 @@
1
-// Copyright: SPDX-License-Identifier: GPL-3.0-only
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
3
#include <stdio.h>
4
#include <string.h>
src/libnetdata/libnetdata.h
+2
@@ -451,6 +451,8 @@ extern char *netdata_configured_host_prefix;
451
#include "july/july.h"
452
#include "threads/threads.h"
453
#include "buffer/buffer.h"
454
+#include "ringbuffer/ringbuffer.h"
455
+#include "c_rhash/c_rhash.h"
456
#include "locks/locks.h"
457
#include "circular_buffer/circular_buffer.h"
458
#include "avl/avl.h"
src/libnetdata/ringbuffer/ringbuffer.c
renamed
+18
-23
@@ -1,33 +1,17 @@
1
-// Copyright: SPDX-License-Identifier: GPL-3.0-only
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
3
-#include "cringbuffer.h"
4
-#include "cringbuffer_internal.h"
5
-
6
-#include <stdlib.h>
7
-#include <assert.h>
8
-#include <string.h>
9
-
10
-#define MIN(a,b) (((a)<(b))?(a):(b))
11
-#define MAX(a,b) (((a)>(b))?(a):(b))
12
-
13
-// this allows user to use their own
14
-// custom memory allocation functions
15
-#ifdef RBUF_CUSTOM_MALLOC
16
-#include "../../helpers/ringbuffer_pal.h"
17
-#else
18
-#define crbuf_malloc(...) malloc(__VA_ARGS__)
19
-#define crbuf_free(...) free(__VA_ARGS__)
20
-#endif
3
+#include "../libnetdata.h"
4
+#include "ringbuffer_internal.h"
5
6
rbuf_t rbuf_create(size_t size)
7
{
24
- rbuf_t buffer = crbuf_malloc(sizeof(struct rbuf_t) + size);
8
+ rbuf_t buffer = mallocz(sizeof(struct rbuf) + size);
9
if (!buffer)
10
return NULL;
11
28
- memset(buffer, 0, sizeof(struct rbuf_t));
12
+ memset(buffer, 0, sizeof(struct rbuf));
13
30
- buffer->data = ((char*)buffer) + sizeof(struct rbuf_t);
14
+ buffer->data = ((char*)buffer) + sizeof(struct rbuf);
15
16
buffer->head = buffer->data;
17
buffer->tail = buffer->data;
@@ -39,7 +23,7 @@ rbuf_t rbuf_create(size_t size)
23
24
void rbuf_free(rbuf_t buffer)
25
{
42
- crbuf_free(buffer);
26
+ freez(buffer);
27
}
28
29
void rbuf_flush(rbuf_t buffer)
@@ -81,6 +65,17 @@ int rbuf_bump_head(rbuf_t buffer, size_t bytes)
65
return 1;
66
}
67
68
+int rbuf_bump_tail_noopt(rbuf_t buffer, size_t bytes)
69
+{
70
+ if (bytes > buffer->size_data)
71
+ return 0;
72
+ int i = buffer->tail - buffer->data;
73
+ buffer->tail = &buffer->data[(i + bytes) % buffer->size];
74
+ buffer->size_data -= bytes;
75
+
76
+ return 1;
77
+}
78
+
79
int rbuf_bump_tail(rbuf_t buffer, size_t bytes)
80
{
81
if(!rbuf_bump_tail_noopt(buffer, bytes))
src/libnetdata/ringbuffer/ringbuffer.h
renamed
+5
-6
@@ -1,11 +1,10 @@
1
-// Copyright: SPDX-License-Identifier: GPL-3.0-only
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
3
-#ifndef CRINGBUFFER_H
4
-#define CRINGBUFFER_H
3
+#ifndef RINGBUFFER_H
4
+#define RINGBUFFER_H
5
+#include "../libnetdata.h"
6
6
-#include <stddef.h>
7
-
8
-typedef struct rbuf_t *rbuf_t;
7
+typedef struct rbuf *rbuf_t;
8
9
rbuf_t rbuf_create(size_t size);
10
void rbuf_free(rbuf_t buffer);
src/libnetdata/ringbuffer/ringbuffer_internal.h
new
+26
@@ -0,0 +1,26 @@
1
+// SPDX-License-Identifier: GPL-3.0-or-later
2
+
3
+#ifndef RINGBUFFER_INTERNAL_H
4
+#define RINGBUFFER_INTERNAL_H
5
+
6
+#include "ringbuffer.h"
7
+
8
+struct rbuf {
9
+ char *data;
10
+
11
+ // points to next byte where we can write
12
+ char *head;
13
+ // points to oldest (next to be poped) readable byte
14
+ char *tail;
15
+
16
+ // to avoid calculating data + size
17
+ // all the time
18
+ char *end;
19
+
20
+ size_t size;
21
+ size_t size_data;
22
+};
23
+
24
+typedef struct rbuf *rbuf_t;
25
+
26
+#endif
src/web/server/h2o/streaming.h
-2
@@ -3,8 +3,6 @@
3
#ifndef HTTPD_STREAMING_H
4
#define HTTPD_STREAMING_H
5
6
-#include "aclk/mqtt_websockets/c-rbuf/cringbuffer.h"
7
-
6
#pragma GCC diagnostic push
7
#pragma GCC diagnostic ignored "-Wunused-parameter"
8
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"