blob: 4c4397be73fcfc48b734d54f10c2b0d2e8e92742 [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
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080029struct ringbuffer *ringbuffer_init(size_t size)
30{
31 struct ringbuffer *rb;
32
33 rb = malloc(sizeof(*rb) + size);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093034 if (!rb) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080035 return NULL;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093036 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080037
38 memset(rb, 0, sizeof(*rb));
39 rb->size = size;
40 rb->buf = (void *)(rb + 1);
41
42 return rb;
43}
44
45void ringbuffer_fini(struct ringbuffer *rb)
46{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093047 while (rb->n_consumers) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080048 ringbuffer_consumer_unregister(rb->consumers[0]);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093049 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080050 free(rb);
51}
52
Andrew Jefferya72711a2023-04-18 18:19:41 +093053struct ringbuffer_consumer *
54ringbuffer_consumer_register(struct ringbuffer *rb, ringbuffer_poll_fn_t fn,
55 void *data)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080056{
57 struct ringbuffer_consumer *rbc;
58 int n;
59
60 rbc = malloc(sizeof(*rbc));
61 rbc->rb = rb;
62 rbc->poll_fn = fn;
63 rbc->poll_data = data;
64 rbc->pos = rb->tail;
65
66 n = rb->n_consumers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +093067 /*
68 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
69 * pointer type.
70 */
71 /* NOLINTBEGIN(bugprone-sizeof-expression) */
72 rb->consumers = reallocarray(rb->consumers, rb->n_consumers,
73 sizeof(*rb->consumers));
74 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080075 rb->consumers[n] = rbc;
76
77 return rbc;
78}
79
80void ringbuffer_consumer_unregister(struct ringbuffer_consumer *rbc)
81{
82 struct ringbuffer *rb = rbc->rb;
83 int i;
84
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093085 for (i = 0; i < rb->n_consumers; i++) {
86 if (rb->consumers[i] == rbc) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080087 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093088 }
89 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080090
91 assert(i < rb->n_consumers);
92
93 rb->n_consumers--;
94
Andrew Jeffery91b52172023-04-19 12:42:14 +093095 /*
96 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
97 * pointer type.
98 */
Andrew Jefferya72711a2023-04-18 18:19:41 +093099 memmove(&rb->consumers[i], &rb->consumers[i + 1],
John Wang2f1abc32024-06-04 20:58:33 +0800100 /* NOLINTNEXTLINE(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930101 sizeof(*rb->consumers) * (rb->n_consumers - i));
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800102
John Wang2f1abc32024-06-04 20:58:33 +0800103 if (rb->n_consumers == 0) {
104 free(rb->consumers);
105 rb->consumers = NULL;
106 } else {
107 rb->consumers = reallocarray(
108 rb->consumers, rb->n_consumers,
109 /* NOLINTNEXTLINE(bugprone-sizeof-expression) */
110 sizeof(*rb->consumers));
111 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800112
113 free(rbc);
114}
115
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800116size_t ringbuffer_len(struct ringbuffer_consumer *rbc)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800117{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930118 if (rbc->pos <= rbc->rb->tail) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800119 return rbc->rb->tail - rbc->pos;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930120 }
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930121 return rbc->rb->tail + rbc->rb->size - rbc->pos;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800122}
123
124static size_t ringbuffer_space(struct ringbuffer_consumer *rbc)
125{
126 return rbc->rb->size - ringbuffer_len(rbc) - 1;
127}
128
Andrew Jefferya72711a2023-04-18 18:19:41 +0930129static int ringbuffer_consumer_ensure_space(struct ringbuffer_consumer *rbc,
130 size_t len)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800131{
132 enum ringbuffer_poll_ret prc;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930133 size_t force_len;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800134
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930135 if (ringbuffer_space(rbc) >= len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800136 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930137 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800138
139 force_len = len - ringbuffer_space(rbc);
140
141 prc = rbc->poll_fn(rbc->poll_data, force_len);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930142 if (prc != RINGBUFFER_POLL_OK) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800143 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930144 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800145
146 return 0;
147}
148
149int ringbuffer_queue(struct ringbuffer *rb, uint8_t *data, size_t len)
150{
151 struct ringbuffer_consumer *rbc;
152 size_t wlen;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930153 int i;
154 int rc;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800155
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930156 if (len >= rb->size) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800157 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930158 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800159
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930160 if (len == 0) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800161 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930162 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800163
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800164 /* Ensure there is at least len bytes of space available.
165 *
166 * If a client doesn't have sufficient space, perform a blocking write
167 * (by calling ->poll_fn with force_len) to create it.
168 */
169 for (i = 0; i < rb->n_consumers; i++) {
170 rbc = rb->consumers[i];
171
172 rc = ringbuffer_consumer_ensure_space(rbc, len);
173 if (rc) {
174 ringbuffer_consumer_unregister(rbc);
175 i--;
176 continue;
177 }
178
179 assert(ringbuffer_space(rbc) >= len);
180 }
181
182 /* Now that we know we have enough space, add new data to tail */
183 wlen = min(len, rb->size - rb->tail);
184 memcpy(rb->buf + rb->tail, data, wlen);
185 rb->tail = (rb->tail + wlen) % rb->size;
186 len -= wlen;
187 data += wlen;
188
189 memcpy(rb->buf, data, len);
190 rb->tail += len;
191
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800192 /* Inform consumers of new data in non-blocking mode, by calling
193 * ->poll_fn with 0 force_len */
194 for (i = 0; i < rb->n_consumers; i++) {
195 enum ringbuffer_poll_ret prc;
196
197 rbc = rb->consumers[i];
198 prc = rbc->poll_fn(rbc->poll_data, 0);
199 if (prc == RINGBUFFER_POLL_REMOVE) {
200 ringbuffer_consumer_unregister(rbc);
201 i--;
202 }
203 }
204
205 return 0;
206}
207
208size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930209 uint8_t **data)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800210{
211 struct ringbuffer *rb = rbc->rb;
212 size_t pos;
213 size_t len;
214
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930215 if (offset >= ringbuffer_len(rbc)) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800216 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930217 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800218
219 pos = (rbc->pos + offset) % rb->size;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930220 if (pos <= rb->tail) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800221 len = rb->tail - pos;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930222 } else {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800223 len = rb->size - pos;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930224 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800225
226 *data = rb->buf + pos;
227 return len;
228}
229
230int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len)
231{
232 assert(len <= ringbuffer_len(rbc));
233 rbc->pos = (rbc->pos + len) % rbc->rb->size;
234 return 0;
235}