blob: 93e78c36a7e7c6ffdea380a8d52ade5d7213329d [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;
162 int i, rc;
163
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930164 if (len >= rb->size) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800165 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930166 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800167
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930168 if (len == 0) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800169 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930170 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800171
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800172 /* Ensure there is at least len bytes of space available.
173 *
174 * If a client doesn't have sufficient space, perform a blocking write
175 * (by calling ->poll_fn with force_len) to create it.
176 */
177 for (i = 0; i < rb->n_consumers; i++) {
178 rbc = rb->consumers[i];
179
180 rc = ringbuffer_consumer_ensure_space(rbc, len);
181 if (rc) {
182 ringbuffer_consumer_unregister(rbc);
183 i--;
184 continue;
185 }
186
187 assert(ringbuffer_space(rbc) >= len);
188 }
189
190 /* Now that we know we have enough space, add new data to tail */
191 wlen = min(len, rb->size - rb->tail);
192 memcpy(rb->buf + rb->tail, data, wlen);
193 rb->tail = (rb->tail + wlen) % rb->size;
194 len -= wlen;
195 data += wlen;
196
197 memcpy(rb->buf, data, len);
198 rb->tail += len;
199
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800200 /* Inform consumers of new data in non-blocking mode, by calling
201 * ->poll_fn with 0 force_len */
202 for (i = 0; i < rb->n_consumers; i++) {
203 enum ringbuffer_poll_ret prc;
204
205 rbc = rb->consumers[i];
206 prc = rbc->poll_fn(rbc->poll_data, 0);
207 if (prc == RINGBUFFER_POLL_REMOVE) {
208 ringbuffer_consumer_unregister(rbc);
209 i--;
210 }
211 }
212
213 return 0;
214}
215
216size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930217 uint8_t **data)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800218{
219 struct ringbuffer *rb = rbc->rb;
220 size_t pos;
221 size_t len;
222
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930223 if (offset >= ringbuffer_len(rbc)) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800224 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930225 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800226
227 pos = (rbc->pos + offset) % rb->size;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930228 if (pos <= rb->tail) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800229 len = rb->tail - pos;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930230 } else {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800231 len = rb->size - pos;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930232 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800233
234 *data = rb->buf + pos;
235 return len;
236}
237
238int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len)
239{
240 assert(len <= ringbuffer_len(rbc));
241 rbc->pos = (rbc->pos + len) % rbc->rb->size;
242 return 0;
243}