blob: 48c163a6dda8dee2c381f37c389803960edb960c [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++;
80 rb->consumers = realloc(rb->consumers,
Andrew Jefferya72711a2023-04-18 18:19:41 +093081 sizeof(*rb->consumers) * rb->n_consumers);
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080082 rb->consumers[n] = rbc;
83
84 return rbc;
85}
86
87void ringbuffer_consumer_unregister(struct ringbuffer_consumer *rbc)
88{
89 struct ringbuffer *rb = rbc->rb;
90 int i;
91
92 for (i = 0; i < rb->n_consumers; i++)
93 if (rb->consumers[i] == rbc)
94 break;
95
96 assert(i < rb->n_consumers);
97
98 rb->n_consumers--;
99
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
103 rb->consumers = realloc(rb->consumers,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930104 sizeof(*rb->consumers) * rb->n_consumers);
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800105
106 free(rbc);
107}
108
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800109size_t ringbuffer_len(struct ringbuffer_consumer *rbc)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800110{
111 if (rbc->pos <= rbc->rb->tail)
112 return rbc->rb->tail - rbc->pos;
113 else
114 return rbc->rb->tail + rbc->rb->size - rbc->pos;
115}
116
117static size_t ringbuffer_space(struct ringbuffer_consumer *rbc)
118{
119 return rbc->rb->size - ringbuffer_len(rbc) - 1;
120}
121
Andrew Jefferya72711a2023-04-18 18:19:41 +0930122static int ringbuffer_consumer_ensure_space(struct ringbuffer_consumer *rbc,
123 size_t len)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800124{
125 enum ringbuffer_poll_ret prc;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930126 size_t force_len;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800127
128 if (ringbuffer_space(rbc) >= len)
129 return 0;
130
131 force_len = len - ringbuffer_space(rbc);
132
133 prc = rbc->poll_fn(rbc->poll_data, force_len);
134 if (prc != RINGBUFFER_POLL_OK)
135 return -1;
136
137 return 0;
138}
139
140int ringbuffer_queue(struct ringbuffer *rb, uint8_t *data, size_t len)
141{
142 struct ringbuffer_consumer *rbc;
143 size_t wlen;
144 int i, rc;
145
146 if (len >= rb->size)
147 return -1;
148
Johnathan Mantey1cecc5d2019-02-28 15:01:46 -0800149 if (len == 0)
150 return 0;
151
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800152 /* Ensure there is at least len bytes of space available.
153 *
154 * If a client doesn't have sufficient space, perform a blocking write
155 * (by calling ->poll_fn with force_len) to create it.
156 */
157 for (i = 0; i < rb->n_consumers; i++) {
158 rbc = rb->consumers[i];
159
160 rc = ringbuffer_consumer_ensure_space(rbc, len);
161 if (rc) {
162 ringbuffer_consumer_unregister(rbc);
163 i--;
164 continue;
165 }
166
167 assert(ringbuffer_space(rbc) >= len);
168 }
169
170 /* Now that we know we have enough space, add new data to tail */
171 wlen = min(len, rb->size - rb->tail);
172 memcpy(rb->buf + rb->tail, data, wlen);
173 rb->tail = (rb->tail + wlen) % rb->size;
174 len -= wlen;
175 data += wlen;
176
177 memcpy(rb->buf, data, len);
178 rb->tail += len;
179
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800180 /* Inform consumers of new data in non-blocking mode, by calling
181 * ->poll_fn with 0 force_len */
182 for (i = 0; i < rb->n_consumers; i++) {
183 enum ringbuffer_poll_ret prc;
184
185 rbc = rb->consumers[i];
186 prc = rbc->poll_fn(rbc->poll_data, 0);
187 if (prc == RINGBUFFER_POLL_REMOVE) {
188 ringbuffer_consumer_unregister(rbc);
189 i--;
190 }
191 }
192
193 return 0;
194}
195
196size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930197 uint8_t **data)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800198{
199 struct ringbuffer *rb = rbc->rb;
200 size_t pos;
201 size_t len;
202
203 if (offset >= ringbuffer_len(rbc))
204 return 0;
205
206 pos = (rbc->pos + offset) % rb->size;
207 if (pos <= rb->tail)
208 len = rb->tail - pos;
209 else
210 len = rb->size - pos;
211
212 *data = rb->buf + pos;
213 return len;
214}
215
216int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len)
217{
218 assert(len <= ringbuffer_len(rbc));
219 rbc->pos = (rbc->pos + len) % rbc->rb->size;
220 return 0;
221}