blob: 13104e9cc1249a00932082cfef64615ec890490b [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 */
99 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930100 memmove(&rb->consumers[i], &rb->consumers[i + 1],
101 sizeof(*rb->consumers) * (rb->n_consumers - i));
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800102
Andrew Jeffery91b52172023-04-19 12:42:14 +0930103 rb->consumers = reallocarray(rb->consumers, rb->n_consumers,
104 sizeof(*rb->consumers));
105 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800106
107 free(rbc);
108}
109
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800110size_t ringbuffer_len(struct ringbuffer_consumer *rbc)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800111{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930112 if (rbc->pos <= rbc->rb->tail) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800113 return rbc->rb->tail - rbc->pos;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930114 }
Andrew Jeffery0b7b0472023-04-19 12:48:51 +0930115 return rbc->rb->tail + rbc->rb->size - rbc->pos;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800116}
117
118static size_t ringbuffer_space(struct ringbuffer_consumer *rbc)
119{
120 return rbc->rb->size - ringbuffer_len(rbc) - 1;
121}
122
Andrew Jefferya72711a2023-04-18 18:19:41 +0930123static int ringbuffer_consumer_ensure_space(struct ringbuffer_consumer *rbc,
124 size_t len)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800125{
126 enum ringbuffer_poll_ret prc;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930127 size_t force_len;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800128
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930129 if (ringbuffer_space(rbc) >= len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800130 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930131 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800132
133 force_len = len - ringbuffer_space(rbc);
134
135 prc = rbc->poll_fn(rbc->poll_data, force_len);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930136 if (prc != RINGBUFFER_POLL_OK) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800137 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930138 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800139
140 return 0;
141}
142
143int ringbuffer_queue(struct ringbuffer *rb, uint8_t *data, size_t len)
144{
145 struct ringbuffer_consumer *rbc;
146 size_t wlen;
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930147 int i;
148 int rc;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800149
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930150 if (len >= rb->size) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800151 return -1;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930152 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800153
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930154 if (len == 0) {
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800155 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930156 }
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800157
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800158 /* Ensure there is at least len bytes of space available.
159 *
160 * If a client doesn't have sufficient space, perform a blocking write
161 * (by calling ->poll_fn with force_len) to create it.
162 */
163 for (i = 0; i < rb->n_consumers; i++) {
164 rbc = rb->consumers[i];
165
166 rc = ringbuffer_consumer_ensure_space(rbc, len);
167 if (rc) {
168 ringbuffer_consumer_unregister(rbc);
169 i--;
170 continue;
171 }
172
173 assert(ringbuffer_space(rbc) >= len);
174 }
175
176 /* Now that we know we have enough space, add new data to tail */
177 wlen = min(len, rb->size - rb->tail);
178 memcpy(rb->buf + rb->tail, data, wlen);
179 rb->tail = (rb->tail + wlen) % rb->size;
180 len -= wlen;
181 data += wlen;
182
183 memcpy(rb->buf, data, len);
184 rb->tail += len;
185
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800186 /* Inform consumers of new data in non-blocking mode, by calling
187 * ->poll_fn with 0 force_len */
188 for (i = 0; i < rb->n_consumers; i++) {
189 enum ringbuffer_poll_ret prc;
190
191 rbc = rb->consumers[i];
192 prc = rbc->poll_fn(rbc->poll_data, 0);
193 if (prc == RINGBUFFER_POLL_REMOVE) {
194 ringbuffer_consumer_unregister(rbc);
195 i--;
196 }
197 }
198
199 return 0;
200}
201
202size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930203 uint8_t **data)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800204{
205 struct ringbuffer *rb = rbc->rb;
206 size_t pos;
207 size_t len;
208
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930209 if (offset >= ringbuffer_len(rbc)) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800210 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930211 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800212
213 pos = (rbc->pos + offset) % rb->size;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930214 if (pos <= rb->tail) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800215 len = rb->tail - pos;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930216 } else {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800217 len = rb->size - pos;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930218 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800219
220 *data = rb->buf + pos;
221 return len;
222}
223
224int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len)
225{
226 assert(len <= ringbuffer_len(rbc));
227 rbc->pos = (rbc->pos + len) % rbc->rb->size;
228 return 0;
229}