ustream: fix polling for write buffer availability after buffering writes
[project/libubox.git] / ustream.c
1 /*
2 * ustream - library for stream buffer management
3 *
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <stdarg.h>
24
25 #include "ustream.h"
26
27 static void ustream_init_buf(struct ustream_buf *buf, int len)
28 {
29 if (!len)
30 abort();
31
32 memset(buf, 0, sizeof(*buf));
33 buf->data = buf->tail = buf->head;
34 buf->end = buf->head + len;
35 *buf->head = 0;
36 }
37
38 static void ustream_add_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
39 {
40 if (!l->tail)
41 l->head = buf;
42 else
43 l->tail->next = buf;
44
45 buf->next = NULL;
46 l->tail = buf;
47 if (!l->data_tail)
48 l->data_tail = l->head;
49 }
50
51 static bool ustream_can_alloc(struct ustream_buf_list *l)
52 {
53 if (l->max_buffers <= 0)
54 return true;
55
56 return (l->buffers < l->max_buffers);
57 }
58
59 static int ustream_alloc_default(struct ustream *s, struct ustream_buf_list *l)
60 {
61 struct ustream_buf *buf;
62
63 if (!ustream_can_alloc(l))
64 return -1;
65
66 buf = malloc(sizeof(*buf) + l->buffer_len + s->string_data);
67 ustream_init_buf(buf, l->buffer_len);
68 ustream_add_buf(l, buf);
69
70 return 0;
71 }
72
73 static void ustream_free_buffers(struct ustream_buf_list *l)
74 {
75 struct ustream_buf *buf = l->head;
76
77 while (buf) {
78 struct ustream_buf *next = buf->next;
79
80 free(buf);
81 buf = next;
82 }
83 l->head = NULL;
84 l->tail = NULL;
85 l->data_tail = NULL;
86 }
87
88 void ustream_free(struct ustream *s)
89 {
90 if (s->free)
91 s->free(s);
92
93 uloop_timeout_cancel(&s->state_change);
94 ustream_free_buffers(&s->r);
95 ustream_free_buffers(&s->w);
96 s->write_error = false;
97 s->eof = false;
98 s->read_blocked = 0;
99 }
100
101 static void ustream_state_change_cb(struct uloop_timeout *t)
102 {
103 struct ustream *s = container_of(t, struct ustream, state_change);
104
105 if (s->write_error)
106 ustream_free_buffers(&s->w);
107 if (s->notify_state)
108 s->notify_state(s);
109 }
110
111 void ustream_init_defaults(struct ustream *s)
112 {
113 #define DEFAULT_SET(_f, _default) \
114 do { \
115 if (!_f) \
116 _f = _default; \
117 } while(0)
118
119 DEFAULT_SET(s->r.alloc, ustream_alloc_default);
120 DEFAULT_SET(s->w.alloc, ustream_alloc_default);
121
122 DEFAULT_SET(s->r.min_buffers, 1);
123 DEFAULT_SET(s->r.max_buffers, 1);
124 DEFAULT_SET(s->r.buffer_len, 4096);
125
126 DEFAULT_SET(s->w.min_buffers, 2);
127 DEFAULT_SET(s->w.max_buffers, -1);
128 DEFAULT_SET(s->w.buffer_len, 256);
129
130 #undef DEFAULT_SET
131
132 s->state_change.cb = ustream_state_change_cb;
133 }
134
135 static bool ustream_should_move(struct ustream_buf_list *l, struct ustream_buf *buf, int len)
136 {
137 int maxlen;
138 int offset;
139
140 if (buf->data == buf->head)
141 return false;
142
143 maxlen = buf->end - buf->head;
144 offset = buf->data - buf->head;
145
146 if (offset > maxlen / 2)
147 return true;
148
149 if (buf->tail - buf->data < 32 && offset > maxlen / 4)
150 return true;
151
152 if (buf != l->tail || ustream_can_alloc(l))
153 return false;
154
155 return (buf->end - buf->tail < len);
156 }
157
158 static void ustream_free_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
159 {
160 if (buf == l->head)
161 l->head = buf->next;
162
163 if (buf == l->data_tail)
164 l->data_tail = buf->next;
165
166 if (buf == l->tail)
167 l->tail = NULL;
168
169 if (--l->buffers >= l->min_buffers) {
170 free(buf);
171 return;
172 }
173
174 /* recycle */
175 ustream_init_buf(buf, buf->end - buf->head);
176 ustream_add_buf(l, buf);
177 }
178
179 static void __ustream_set_read_blocked(struct ustream *s, unsigned char val)
180 {
181 bool changed = !!s->read_blocked != !!val;
182
183 s->read_blocked = val;
184 if (changed)
185 s->set_read_blocked(s);
186 }
187
188 void ustream_set_read_blocked(struct ustream *s, bool set)
189 {
190 unsigned char val = s->read_blocked & ~READ_BLOCKED_USER;
191
192 if (set)
193 val |= READ_BLOCKED_USER;
194
195 __ustream_set_read_blocked(s, val);
196 }
197
198 void ustream_consume(struct ustream *s, int len)
199 {
200 struct ustream_buf *buf = s->r.head;
201
202 if (!len)
203 return;
204
205 s->r.data_bytes -= len;
206 if (s->r.data_bytes < 0)
207 abort();
208
209 do {
210 struct ustream_buf *next = buf->next;
211 int buf_len = buf->tail - buf->data;
212
213 if (len < buf_len) {
214 buf->data += len;
215 break;
216 }
217
218 len -= buf_len;
219 ustream_free_buf(&s->r, buf);
220 buf = next;
221 } while(len);
222
223 __ustream_set_read_blocked(s, s->read_blocked & ~READ_BLOCKED_FULL);
224 }
225
226 static void ustream_fixup_string(struct ustream *s, struct ustream_buf *buf)
227 {
228 if (!s->string_data)
229 return;
230
231 *buf->tail = 0;
232 }
233
234 static bool ustream_prepare_buf(struct ustream *s, struct ustream_buf_list *l, int len)
235 {
236 struct ustream_buf *buf;
237
238 buf = l->data_tail;
239 if (buf) {
240 if (ustream_should_move(l, buf, len)) {
241 int len = buf->tail - buf->data;
242
243 memmove(buf->head, buf->data, len);
244 buf->data = buf->head;
245 buf->tail = buf->data + len;
246
247 if (l == &s->r)
248 ustream_fixup_string(s, buf);
249 }
250 if (buf->tail != buf->end)
251 return true;
252 }
253
254 if (buf && buf->next) {
255 l->data_tail = buf->next;
256 return true;
257 }
258
259 if (!ustream_can_alloc(l))
260 return false;
261
262 if (l->alloc(s, l) < 0)
263 return false;
264
265 l->data_tail = l->tail;
266 return true;
267 }
268
269 char *ustream_reserve(struct ustream *s, int len, int *maxlen)
270 {
271 struct ustream_buf *buf = s->r.head;
272
273 if (!ustream_prepare_buf(s, &s->r, len)) {
274 __ustream_set_read_blocked(s, s->read_blocked | READ_BLOCKED_FULL);
275 *maxlen = 0;
276 return NULL;
277 }
278
279 buf = s->r.data_tail;
280 *maxlen = buf->end - buf->tail;
281 return buf->tail;
282 }
283
284 void ustream_fill_read(struct ustream *s, int len)
285 {
286 struct ustream_buf *buf = s->r.data_tail;
287 int n = len;
288 int maxlen;
289
290 s->r.data_bytes += len;
291 do {
292 if (!buf)
293 abort();
294
295 maxlen = buf->end - buf->tail;
296 if (len < maxlen)
297 maxlen = len;
298
299 len -= maxlen;
300 buf->tail += maxlen;
301 ustream_fixup_string(s, buf);
302
303 s->r.data_tail = buf;
304 buf = buf->next;
305 } while (len);
306
307 if (s->notify_read)
308 s->notify_read(s, n);
309 }
310
311 char *ustream_get_read_buf(struct ustream *s, int *buflen)
312 {
313 char *data;
314 int len;
315
316 if (s->r.head) {
317 len = s->r.head->tail - s->r.head->data;
318 data = s->r.head->data;
319 } else {
320 len = 0;
321 data = NULL;
322 }
323
324 if (buflen)
325 *buflen = len;
326
327 return data;
328 }
329
330 static void ustream_write_error(struct ustream *s)
331 {
332 s->write_error = true;
333 ustream_state_change(s);
334 }
335
336 bool ustream_write_pending(struct ustream *s)
337 {
338 struct ustream_buf *buf = s->w.head;
339 int wr = 0, len;
340
341 if (s->write_error)
342 return false;
343
344 while (buf && s->w.data_bytes) {
345 struct ustream_buf *next = buf->next;
346 int maxlen = buf->tail - buf->data;
347
348 len = s->write(s, buf->data, maxlen, !!buf->next);
349 if (len < 0) {
350 ustream_write_error(s);
351 break;
352 }
353
354 if (len == 0)
355 break;
356
357 wr += len;
358 s->w.data_bytes -= len;
359 if (len < maxlen) {
360 buf->data += len;
361 break;
362 }
363
364 ustream_free_buf(&s->w, buf);
365 buf = next;
366 }
367
368 if (s->notify_write)
369 s->notify_write(s, wr);
370
371 if (s->eof && wr && !s->w.data_bytes)
372 ustream_state_change(s);
373
374 return !s->w.data_bytes;
375 }
376
377 static int ustream_write_buffered(struct ustream *s, const char *data, int len, int wr)
378 {
379 struct ustream_buf_list *l = &s->w;
380 struct ustream_buf *buf;
381 int maxlen;
382 bool has_data = !!s->w.data_bytes;
383
384 while (len) {
385 if (!ustream_prepare_buf(s, &s->w, len))
386 break;
387
388 buf = l->data_tail;
389
390 maxlen = buf->end - buf->tail;
391 if (maxlen > len)
392 maxlen = len;
393
394 memcpy(buf->tail, data, maxlen);
395 buf->tail += maxlen;
396 data += maxlen;
397 len -= maxlen;
398 wr += maxlen;
399 l->data_bytes += maxlen;
400 }
401
402 if (s->poll && !has_data)
403 s->poll(s);
404
405 return wr;
406 }
407
408 int ustream_write(struct ustream *s, const char *data, int len, bool more)
409 {
410 struct ustream_buf_list *l = &s->w;
411 int wr = 0;
412
413 if (s->write_error)
414 return 0;
415
416 if (!l->data_bytes) {
417 wr = s->write(s, data, len, more);
418 if (wr == len)
419 return wr;
420
421 if (wr < 0) {
422 ustream_write_error(s);
423 return wr;
424 }
425
426 data += wr;
427 len -= wr;
428 }
429
430 return ustream_write_buffered(s, data, len, wr);
431 }
432
433 #define MAX_STACK_BUFLEN 256
434
435 int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
436 {
437 struct ustream_buf_list *l = &s->w;
438 char *buf;
439 va_list arg2;
440 int wr, maxlen, buflen;
441
442 if (s->write_error)
443 return 0;
444
445 if (!l->data_bytes) {
446 buf = alloca(MAX_STACK_BUFLEN);
447 va_copy(arg2, arg);
448 maxlen = vsnprintf(buf, MAX_STACK_BUFLEN, format, arg2);
449 va_end(arg2);
450 if (maxlen < MAX_STACK_BUFLEN) {
451 wr = s->write(s, buf, maxlen, false);
452 if (wr < 0) {
453 ustream_write_error(s);
454 return wr;
455 }
456 if (wr == maxlen)
457 return wr;
458
459 buf += wr;
460 maxlen -= wr;
461 return ustream_write_buffered(s, buf, maxlen, wr);
462 } else {
463 buf = malloc(maxlen + 1);
464 wr = vsnprintf(buf, maxlen + 1, format, arg);
465 wr = ustream_write(s, buf, wr, false);
466 free(buf);
467 return wr;
468 }
469 }
470
471 if (!ustream_prepare_buf(s, l, 1))
472 return 0;
473
474 buf = l->data_tail->tail;
475 buflen = l->data_tail->end - buf;
476
477 va_copy(arg2, arg);
478 maxlen = vsnprintf(buf, buflen, format, arg2);
479 va_end(arg2);
480
481 wr = maxlen;
482 if (wr >= buflen)
483 wr = buflen - 1;
484
485 l->data_tail->tail += wr;
486 l->data_bytes += wr;
487 if (maxlen < buflen)
488 return wr;
489
490 buf = malloc(maxlen + 1);
491 maxlen = vsnprintf(buf, maxlen + 1, format, arg);
492 wr = ustream_write_buffered(s, buf + wr, maxlen - wr, wr);
493 free(buf);
494
495 return wr;
496 }
497
498 int ustream_printf(struct ustream *s, const char *format, ...)
499 {
500 va_list arg;
501 int ret;
502
503 if (s->write_error)
504 return 0;
505
506 va_start(arg, format);
507 ret = ustream_vprintf(s, format, arg);
508 va_end(arg);
509
510 return ret;
511 }