/work/toxav/ring_buffer.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2016-2026 The TokTok team. |
3 | | * Copyright © 2013 Tox project. |
4 | | * Copyright © 2013 plutooo |
5 | | */ |
6 | | #include "ring_buffer.h" |
7 | | |
8 | | #include <stdlib.h> |
9 | | |
10 | | #include "../toxcore/ccompat.h" |
11 | | |
12 | | struct RingBuffer { |
13 | | uint16_t size; /* Max size */ |
14 | | uint16_t start; |
15 | | uint16_t end; |
16 | | void *_Nullable *_Nonnull data; |
17 | | }; |
18 | | |
19 | | bool rb_full(const RingBuffer *b) |
20 | 6 | { |
21 | 6 | return (b->end + 1) % b->size == b->start; |
22 | 6 | } |
23 | | |
24 | | bool rb_empty(const RingBuffer *b) |
25 | 148 | { |
26 | 148 | return b->end == b->start; |
27 | 148 | } |
28 | | |
29 | | /** |
30 | | * @retval NULL on success |
31 | | * @return input value "p" on failure, so caller can free on failed rb_write |
32 | | */ |
33 | | void *rb_write(RingBuffer *b, void *p) |
34 | 561 | { |
35 | 561 | if (b == nullptr) { |
36 | 0 | return p; |
37 | 0 | } |
38 | | |
39 | 561 | void *rc = nullptr; |
40 | | |
41 | 561 | if ((b->end + 1) % b->size == b->start) { /* full */ |
42 | 5 | rc = b->data[b->start]; |
43 | 5 | } |
44 | | |
45 | 561 | b->data[b->end] = p; |
46 | 561 | b->end = (b->end + 1) % b->size; |
47 | | |
48 | 561 | if (b->end == b->start) { |
49 | 5 | b->start = (b->start + 1) % b->size; |
50 | 5 | } |
51 | | |
52 | 561 | return rc; |
53 | 561 | } |
54 | | |
55 | | bool rb_read(RingBuffer *b, void **p) |
56 | 228 | { |
57 | 228 | if (b->end == b->start) { /* Empty */ |
58 | 107 | *p = nullptr; |
59 | 107 | return false; |
60 | 107 | } |
61 | | |
62 | 121 | *p = b->data[b->start]; |
63 | 121 | b->start = (b->start + 1) % b->size; |
64 | 121 | return true; |
65 | 228 | } |
66 | | |
67 | | RingBuffer *rb_new(int size) |
68 | 59 | { |
69 | 59 | if (size < 0 || size >= 65535) { |
70 | 2 | return nullptr; |
71 | 2 | } |
72 | | |
73 | 57 | RingBuffer *buf = (RingBuffer *)calloc(1, sizeof(RingBuffer)); |
74 | | |
75 | 57 | if (buf == nullptr) { |
76 | 0 | return nullptr; |
77 | 0 | } |
78 | | |
79 | 57 | buf->size = size + 1; /* include empty elem */ |
80 | 57 | buf->data = (void **)calloc(buf->size, sizeof(void *)); |
81 | | |
82 | 57 | if (buf->data == nullptr) { |
83 | 0 | free(buf); |
84 | 0 | return nullptr; |
85 | 0 | } |
86 | | |
87 | 57 | return buf; |
88 | 57 | } |
89 | | |
90 | | void rb_kill(RingBuffer *b) |
91 | 57 | { |
92 | 57 | if (b != nullptr) { |
93 | 57 | free(b->data); |
94 | 57 | free(b); |
95 | 57 | } |
96 | 57 | } |
97 | | |
98 | | uint16_t rb_size(const RingBuffer *b) |
99 | 145 | { |
100 | 145 | if (rb_empty(b)) { |
101 | 116 | return 0; |
102 | 116 | } |
103 | | |
104 | 29 | return |
105 | 29 | b->end > b->start ? |
106 | 16 | b->end - b->start : |
107 | 29 | (b->size - b->start) + b->end; |
108 | 145 | } |
109 | | |
110 | | uint16_t rb_data(const RingBuffer *b, void **dest, uint16_t dest_size) |
111 | 7 | { |
112 | 7 | uint16_t i; |
113 | 7 | uint16_t size = rb_size(b); |
114 | | |
115 | 7 | if (size > dest_size) { |
116 | 1 | size = dest_size; |
117 | 1 | } |
118 | | |
119 | 21 | for (i = 0; i < size; ++i) { |
120 | 14 | dest[i] = b->data[(b->start + i) % b->size]; |
121 | 14 | } |
122 | | |
123 | 7 | return i; |
124 | 7 | } |