blob: efa52042f8b369f8304d6166d023d30f91d38440 [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);
49 if (!rb)
50 return NULL;
51
52 memset(rb, 0, sizeof(*rb));
53 rb->size = size;
54 rb->buf = (void *)(rb + 1);
55
56 return rb;
57}
58
59void ringbuffer_fini(struct ringbuffer *rb)
60{
61 while (rb->n_consumers)
62 ringbuffer_consumer_unregister(rb->consumers[0]);
63 free(rb);
64}
65
Andrew Jefferya72711a2023-04-18 18:19:41 +093066struct ringbuffer_consumer *
67ringbuffer_consumer_register(struct ringbuffer *rb, ringbuffer_poll_fn_t fn,
68 void *data)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080069{
70 struct ringbuffer_consumer *rbc;
71 int n;
72
73 rbc = malloc(sizeof(*rbc));
74 rbc->rb = rb;
75 rbc->poll_fn = fn;
76 rbc->poll_data = data;
77 rbc->pos = rb->tail;
78
79 n = rb->n_consumers++;
Andrew Jeffery91b52172023-04-19 12:42:14 +093080 /*
81 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
82 * pointer type.
83 */
84 /* NOLINTBEGIN(bugprone-sizeof-expression) */
85 rb->consumers = reallocarray(rb->consumers, rb->n_consumers,
86 sizeof(*rb->consumers));
87 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080088 rb->consumers[n] = rbc;
89
90 return rbc;
91}
92
93void ringbuffer_consumer_unregister(struct ringbuffer_consumer *rbc)
94{
95 struct ringbuffer *rb = rbc->rb;
96 int i;
97
98 for (i = 0; i < rb->n_consumers; i++)
99 if (rb->consumers[i] == rbc)
100 break;
101
102 assert(i < rb->n_consumers);
103
104 rb->n_consumers--;
105
Andrew Jeffery91b52172023-04-19 12:42:14 +0930106 /*
107 * We're managing an array of pointers to aggregates, so don't warn about sizeof() on a
108 * pointer type.
109 */
110 /* NOLINTBEGIN(bugprone-sizeof-expression) */
Andrew Jefferya72711a2023-04-18 18:19:41 +0930111 memmove(&rb->consumers[i], &rb->consumers[i + 1],
112 sizeof(*rb->consumers) * (rb->n_consumers - i));
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800113
Andrew Jeffery91b52172023-04-19 12:42:14 +0930114 rb->consumers = reallocarray(rb->consumers, rb->n_consumers,
115 sizeof(*rb->consumers));
116 /* NOLINTEND(bugprone-sizeof-expression) */
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800117
118 free(rbc);
119}
120
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800121size_t ringbuffer_len(struct ringbuffer_consumer *rbc)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800122{
123 if (rbc->pos <= rbc->rb->tail)
124 return rbc->rb->tail - rbc->pos;
125 else
126 return rbc->rb->tail + rbc->rb->size - rbc->pos;
127}
128
129static size_t ringbuffer_space(struct ringbuffer_consumer *rbc)
130{
131 return rbc->rb->size - ringbuffer_len(rbc) - 1;
132}
133
Andrew Jefferya72711a2023-04-18 18:19:41 +0930134static int ringbuffer_consumer_ensure_space(struct ringbuffer_consumer *rbc,
135 size_t len)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800136{
137 enum ringbuffer_poll_ret prc;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930138 size_t force_len;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800139
140 if (ringbuffer_space(rbc) >= len)
141 return 0;
142
143 force_len = len - ringbuffer_space(rbc);
144
145 prc = rbc->poll_fn(rbc->poll_data, force_len);
146 if (prc != RINGBUFFER_POLL_OK)
147 return -1;
148
149 return 0;
150}
151
152int ringbuffer_queue(struct ringbuffer *rb, uint8_t *data, size_t len)
153{
154 struct ringbuffer_consumer *rbc;
155 size_t wlen;
156 int i, rc;
157
158 if (len >= rb->size)
159 return -1;
160
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800161 if (len == 0)
162 return 0;
163
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
215 if (offset >= ringbuffer_len(rbc))
216 return 0;
217
218 pos = (rbc->pos + offset) % rb->size;
219 if (pos <= rb->tail)
220 len = rb->tail - pos;
221 else
222 len = rb->size - pos;
223
224 *data = rb->buf + pos;
225 return len;
226}
227
228int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len)
229{
230 assert(len <= ringbuffer_len(rbc));
231 rbc->pos = (rbc->pos + len) % rbc->rb->size;
232 return 0;
233}