blob: 996dc49e6ee68e76e6935f1d0e3799cf2eff5e58 [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
17#define _GNU_SOURCE
18
19#include <assert.h>
20#include <stdint.h>
21#include <stdlib.h>
22#include <string.h>
23
24#include "console-server.h"
25
Andrew Jeffery15ceb3e2023-04-18 11:52:36 +093026static inline size_t min(size_t a, size_t b)
27{
28 return a < b ? a : b;
29}
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080030
31struct ringbuffer {
Andrew Jefferya72711a2023-04-18 18:19:41 +093032 uint8_t *buf;
33 size_t size;
34 size_t tail;
35 struct ringbuffer_consumer **consumers;
36 int n_consumers;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080037};
38
39struct ringbuffer_consumer {
Andrew Jefferya72711a2023-04-18 18:19:41 +093040 struct ringbuffer *rb;
41 ringbuffer_poll_fn_t poll_fn;
42 void *poll_data;
43 size_t pos;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080044};
45
46struct ringbuffer *ringbuffer_init(size_t size)
47{
48 struct ringbuffer *rb;
49
50 rb = malloc(sizeof(*rb) + size);
51 if (!rb)
52 return NULL;
53
54 memset(rb, 0, sizeof(*rb));
55 rb->size = size;
56 rb->buf = (void *)(rb + 1);
57
58 return rb;
59}
60
61void ringbuffer_fini(struct ringbuffer *rb)
62{
63 while (rb->n_consumers)
64 ringbuffer_consumer_unregister(rb->consumers[0]);
65 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++;
82 rb->consumers = realloc(rb->consumers,
Andrew Jefferya72711a2023-04-18 18:19:41 +093083 sizeof(*rb->consumers) * rb->n_consumers);
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080084 rb->consumers[n] = rbc;
85
86 return rbc;
87}
88
89void ringbuffer_consumer_unregister(struct ringbuffer_consumer *rbc)
90{
91 struct ringbuffer *rb = rbc->rb;
92 int i;
93
94 for (i = 0; i < rb->n_consumers; i++)
95 if (rb->consumers[i] == rbc)
96 break;
97
98 assert(i < rb->n_consumers);
99
100 rb->n_consumers--;
101
Andrew Jefferya72711a2023-04-18 18:19:41 +0930102 memmove(&rb->consumers[i], &rb->consumers[i + 1],
103 sizeof(*rb->consumers) * (rb->n_consumers - i));
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800104
105 rb->consumers = realloc(rb->consumers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930106 sizeof(*rb->consumers) * rb->n_consumers);
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800107
108 free(rbc);
109}
110
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800111size_t ringbuffer_len(struct ringbuffer_consumer *rbc)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800112{
113 if (rbc->pos <= rbc->rb->tail)
114 return rbc->rb->tail - rbc->pos;
115 else
116 return rbc->rb->tail + rbc->rb->size - rbc->pos;
117}
118
119static size_t ringbuffer_space(struct ringbuffer_consumer *rbc)
120{
121 return rbc->rb->size - ringbuffer_len(rbc) - 1;
122}
123
Andrew Jefferya72711a2023-04-18 18:19:41 +0930124static int ringbuffer_consumer_ensure_space(struct ringbuffer_consumer *rbc,
125 size_t len)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800126{
127 enum ringbuffer_poll_ret prc;
128 int force_len;
129
130 if (ringbuffer_space(rbc) >= len)
131 return 0;
132
133 force_len = len - ringbuffer_space(rbc);
134
135 prc = rbc->poll_fn(rbc->poll_data, force_len);
136 if (prc != RINGBUFFER_POLL_OK)
137 return -1;
138
139 return 0;
140}
141
142int ringbuffer_queue(struct ringbuffer *rb, uint8_t *data, size_t len)
143{
144 struct ringbuffer_consumer *rbc;
145 size_t wlen;
146 int i, rc;
147
148 if (len >= rb->size)
149 return -1;
150
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800151 if (len == 0)
152 return 0;
153
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800154 /* Ensure there is at least len bytes of space available.
155 *
156 * If a client doesn't have sufficient space, perform a blocking write
157 * (by calling ->poll_fn with force_len) to create it.
158 */
159 for (i = 0; i < rb->n_consumers; i++) {
160 rbc = rb->consumers[i];
161
162 rc = ringbuffer_consumer_ensure_space(rbc, len);
163 if (rc) {
164 ringbuffer_consumer_unregister(rbc);
165 i--;
166 continue;
167 }
168
169 assert(ringbuffer_space(rbc) >= len);
170 }
171
172 /* Now that we know we have enough space, add new data to tail */
173 wlen = min(len, rb->size - rb->tail);
174 memcpy(rb->buf + rb->tail, data, wlen);
175 rb->tail = (rb->tail + wlen) % rb->size;
176 len -= wlen;
177 data += wlen;
178
179 memcpy(rb->buf, data, len);
180 rb->tail += len;
181
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800182 /* Inform consumers of new data in non-blocking mode, by calling
183 * ->poll_fn with 0 force_len */
184 for (i = 0; i < rb->n_consumers; i++) {
185 enum ringbuffer_poll_ret prc;
186
187 rbc = rb->consumers[i];
188 prc = rbc->poll_fn(rbc->poll_data, 0);
189 if (prc == RINGBUFFER_POLL_REMOVE) {
190 ringbuffer_consumer_unregister(rbc);
191 i--;
192 }
193 }
194
195 return 0;
196}
197
198size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930199 uint8_t **data)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800200{
201 struct ringbuffer *rb = rbc->rb;
202 size_t pos;
203 size_t len;
204
205 if (offset >= ringbuffer_len(rbc))
206 return 0;
207
208 pos = (rbc->pos + offset) % rb->size;
209 if (pos <= rb->tail)
210 len = rb->tail - pos;
211 else
212 len = rb->size - pos;
213
214 *data = rb->buf + pos;
215 return len;
216}
217
218int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len)
219{
220 assert(len <= ringbuffer_len(rbc));
221 rbc->pos = (rbc->pos + len) % rbc->rb->size;
222 return 0;
223}