blob: 3edab0d87148d26940127d546da0b31fe6bb898e [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
26#define min(a,b) ({ \
27 const typeof(a) _a = (a); \
28 const typeof(b) _b = (b); \
29 _a < _b ? _a : _b; \
30 })
31
32struct ringbuffer {
33 uint8_t *buf;
34 size_t size;
35 size_t tail;
36 struct ringbuffer_consumer **consumers;
37 int n_consumers;
38};
39
40struct ringbuffer_consumer {
41 struct ringbuffer *rb;
42 ringbuffer_poll_fn_t poll_fn;
43 void *poll_data;
44 size_t pos;
45};
46
47struct ringbuffer *ringbuffer_init(size_t size)
48{
49 struct ringbuffer *rb;
50
51 rb = malloc(sizeof(*rb) + size);
52 if (!rb)
53 return NULL;
54
55 memset(rb, 0, sizeof(*rb));
56 rb->size = size;
57 rb->buf = (void *)(rb + 1);
58
59 return rb;
60}
61
62void ringbuffer_fini(struct ringbuffer *rb)
63{
64 while (rb->n_consumers)
65 ringbuffer_consumer_unregister(rb->consumers[0]);
66 free(rb);
67}
68
69struct ringbuffer_consumer *ringbuffer_consumer_register(struct ringbuffer *rb,
70 ringbuffer_poll_fn_t fn, void *data)
71{
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,
83 sizeof(*rb->consumers) * rb->n_consumers);
84 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
102 memmove(&rb->consumers[i], &rb->consumers[i+1],
103 sizeof(*rb->consumers) * (rb->n_consumers - i));
104
105 rb->consumers = realloc(rb->consumers,
106 sizeof(*rb->consumers) * rb->n_consumers);
107
108 free(rbc);
109}
110
111static size_t ringbuffer_len(struct ringbuffer_consumer *rbc)
112{
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
124static int ringbuffer_consumer_ensure_space(
125 struct ringbuffer_consumer *rbc, size_t len)
126{
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
151 /* Ensure there is at least len bytes of space available.
152 *
153 * If a client doesn't have sufficient space, perform a blocking write
154 * (by calling ->poll_fn with force_len) to create it.
155 */
156 for (i = 0; i < rb->n_consumers; i++) {
157 rbc = rb->consumers[i];
158
159 rc = ringbuffer_consumer_ensure_space(rbc, len);
160 if (rc) {
161 ringbuffer_consumer_unregister(rbc);
162 i--;
163 continue;
164 }
165
166 assert(ringbuffer_space(rbc) >= len);
167 }
168
169 /* Now that we know we have enough space, add new data to tail */
170 wlen = min(len, rb->size - rb->tail);
171 memcpy(rb->buf + rb->tail, data, wlen);
172 rb->tail = (rb->tail + wlen) % rb->size;
173 len -= wlen;
174 data += wlen;
175
176 memcpy(rb->buf, data, len);
177 rb->tail += len;
178
179 /* Inform consumers of new data in non-blocking mode, by calling
180 * ->poll_fn with 0 force_len */
181 for (i = 0; i < rb->n_consumers; i++) {
182 enum ringbuffer_poll_ret prc;
183
184 rbc = rb->consumers[i];
185 prc = rbc->poll_fn(rbc->poll_data, 0);
186 if (prc == RINGBUFFER_POLL_REMOVE) {
187 ringbuffer_consumer_unregister(rbc);
188 i--;
189 }
190 }
191
192 return 0;
193}
194
195size_t ringbuffer_dequeue_peek(struct ringbuffer_consumer *rbc, size_t offset,
196 uint8_t **data)
197{
198 struct ringbuffer *rb = rbc->rb;
199 size_t pos;
200 size_t len;
201
202 if (offset >= ringbuffer_len(rbc))
203 return 0;
204
205 pos = (rbc->pos + offset) % rb->size;
206 if (pos <= rb->tail)
207 len = rb->tail - pos;
208 else
209 len = rb->size - pos;
210
211 *data = rb->buf + pos;
212 return len;
213}
214
215int ringbuffer_dequeue_commit(struct ringbuffer_consumer *rbc, size_t len)
216{
217 assert(len <= ringbuffer_len(rbc));
218 rbc->pos = (rbc->pos + len) % rbc->rb->size;
219 return 0;
220}