blob: 5ec1fbc96eb8b4daf5ce84b064060513c815af8a [file] [log] [blame]
Jeremy Kerrc9775ce2017-02-07 16:25:34 +08001/**
2 * Copyright © 2017 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080017#include <assert.h>
18#include <stdint.h>
19#include <stdlib.h>
20#include <string.h>
21
22#include "console-server.h"
23
Andrew Jeffery15ceb3e2023-04-18 11:52:36 +093024static inline size_t min(size_t a, size_t b)
25{
26 return a < b ? a : b;
27}
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080028
29struct ringbuffer {
Andrew Jefferya72711a2023-04-18 18:19:41 +093030 uint8_t *buf;
31 size_t size;
32 size_t tail;
33 struct ringbuffer_consumer **consumers;
34 int n_consumers;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080035};
36
37struct ringbuffer_consumer {
Andrew Jefferya72711a2023-04-18 18:19:41 +093038 struct ringbuffer *rb;
39 ringbuffer_poll_fn_t poll_fn;
40 void *poll_data;
41 size_t pos;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080042};
43
44struct ringbuffer *ringbuffer_init(size_t size)
45{
46 struct ringbuffer *rb;
47
48 rb = malloc(sizeof(*rb) + size);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093049 if (!rb) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080050 return NULL;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093051 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080052
53 memset(rb, 0, sizeof(*rb));
54 rb->size = size;
55 rb->buf = (void *)(rb + 1);
56
57 return rb;
58}
59
60void ringbuffer_fini(struct ringbuffer *rb)
61{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093062 while (rb->n_consumers) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080063 ringbuffer_consumer_unregister(rb->consumers[0]);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093064 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080065 free(rb);
66}
67
Andrew Jefferya72711a2023-04-18 18:19:41 +093068struct ringbuffer_consumer *
69ringbuffer_consumer_register(struct ringbuffer *rb, ringbuffer_poll_fn_t fn,
70 void *data)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080071{
72 struct ringbuffer_consumer *rbc;
73 int n;
74
75 rbc = malloc(sizeof(*rbc));
76 rbc->rb = rb;
77 rbc->poll_fn = fn;
78 rbc->poll_data = data;
79 rbc->pos = rb->tail;
80
81 n = rb->n_consumers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +093082 /*
83 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
84 * pointer type.
85 */
86 /* NOLINTBEGIN(bugprone-sizeof-expression) */
87 rb->consumers = reallocarray(rb->consumers, rb->n_consumers,
88 sizeof(*rb->consumers));
89 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080090 rb->consumers[n] = rbc;
91
92 return rbc;
93}
94
95void ringbuffer_consumer_unregister(struct ringbuffer_consumer *rbc)
96{
97 struct ringbuffer *rb = rbc->rb;
98 int i;
99
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930100 for (i = 0; i < rb->n_consumers; i++) {
101 if (rb->consumers[i] == rbc) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800102 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930103 }
104 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800105
106 assert(i < rb->n_consumers);
107
108 rb->n_consumers--;
109
Andrew Jeffery91b52172023-04-19 12:42:14 +0930110 /*
111 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
112 * pointer type.
113 */
114 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930115 memmove(&rb->consumers[i], &rb->consumers[i + 1],
116 sizeof(*rb->consumers) * (rb->n_consumers - i));
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800117
Andrew Jeffery91b52172023-04-19 12:42:14 +0930118 rb->consumers = reallocarray(rb->consumers, rb->n_consumers,
119 sizeof(*rb->consumers));
120 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800121
122 free(rbc);
123}
124
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800125size_t ringbuffer_len(struct ringbuffer_consumer *rbc)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800126{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930127 if (rbc->pos <= rbc->rb->tail) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800128 return rbc->rb->tail - rbc->pos;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930129 }
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930130 return rbc->rb->tail + rbc->rb->size - rbc->pos;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800131}
132
133static size_t ringbuffer_space(struct ringbuffer_consumer *rbc)
134{
135 return rbc->rb->size - ringbuffer_len(rbc) - 1;
136}
137
Andrew Jefferya72711a2023-04-18 18:19:41 +0930138static int ringbuffer_consumer_ensure_space(struct ringbuffer_consumer *rbc,
139 size_t len)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800140{
141 enum ringbuffer_poll_ret prc;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930142 size_t force_len;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800143
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930144 if (ringbuffer_space(rbc) >= len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800145 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930146 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800147
148 force_len = len - ringbuffer_space(rbc);
149
150 prc = rbc->poll_fn(rbc->poll_data, force_len);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930151 if (prc != RINGBUFFER_POLL_OK) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800152 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930153 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800154
155 return 0;
156}
157
158int ringbuffer_queue(struct ringbuffer *rb, uint8_t *data, size_t len)
159{
160 struct ringbuffer_consumer *rbc;
161 size_t wlen;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930162 int i;
163 int rc;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800164
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930165 if (len >= rb->size) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800166 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930167 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800168
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930169 if (len == 0) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800170 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930171 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800172
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800173 /* Ensure there is at least len bytes of space available.
174 *
175 * If a client doesn't have sufficient space, perform a blocking write
176 * (by calling ->poll_fn with force_len) to create it.
177 */
178 for (i = 0; i < rb->n_consumers; i++) {
179 rbc = rb->consumers[i];
180
181 rc = ringbuffer_consumer_ensure_space(rbc, len);
182 if (rc) {
183 ringbuffer_consumer_unregister(rbc);
184 i--;
185 continue;
186 }
187
188 assert(ringbuffer_space(rbc) >= len);
189 }
190
191 /* Now that we know we have enough space, add new data to tail */
192 wlen = min(len, rb->size - rb->tail);
193 memcpy(rb->buf + rb->tail, data, wlen);
194 rb->tail = (rb->tail + wlen) % rb->size;
195 len -= wlen;
196 data += wlen;
197
198 memcpy(rb->buf, data, len);
199 rb->tail += len;
200
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800201 /* Inform consumers of new data in non-blocking mode, by calling
202 * ->poll_fn with 0 force_len */
203 for (i = 0; i < rb->n_consumers; i++) {
204 enum ringbuffer_poll_ret prc;
205
206 rbc = rb->consumers[i];
207 prc = rbc->poll_fn(rbc->poll_data, 0);
208 if (prc == RINGBUFFER_POLL_REMOVE) {
209 ringbuffer_consumer_unregister(rbc);
210 i--;
211 }
212 }
213
214 return 0;
215}
216
217size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930218 uint8_t **data)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800219{
220 struct ringbuffer *rb = rbc->rb;
221 size_t pos;
222 size_t len;
223
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930224 if (offset >= ringbuffer_len(rbc)) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800225 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930226 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800227
228 pos = (rbc->pos + offset) % rb->size;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930229 if (pos <= rb->tail) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800230 len = rb->tail - pos;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930231 } else {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800232 len = rb->size - pos;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930233 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800234
235 *data = rb->buf + pos;
236 return len;
237}
238
239int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len)
240{
241 assert(len <= ringbuffer_len(rbc));
242 rbc->pos = (rbc->pos + len) % rbc->rb->size;
243 return 0;
244}