ustream: add function ustream_read().
[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 l->buffers++;
41 if (!l->tail)
42 l->head = buf;
43 else
44 l->tail->next = buf;
45
46 buf->next = NULL;
47 l->tail = buf;
48 if (!l->data_tail)
49 l->data_tail = l->head;
50 }
51
52 static bool ustream_can_alloc(struct ustream_buf_list *l)
53 {
54 if (l->max_buffers <= 0)
55 return true;
56
57 return (l->buffers < l->max_buffers);
58 }
59
60 static int ustream_alloc_default(struct ustream *s, struct ustream_buf_list *l)
61 {
62 struct ustream_buf *buf;
63
64 if (!ustream_can_alloc(l))
65 return -1;
66
67 buf = malloc(sizeof(*buf) + l->buffer_len + s->string_data);
68 ustream_init_buf(buf, l->buffer_len);
69 ustream_add_buf(l, buf);
70
71 return 0;
72 }
73
74 static void ustream_free_buffers(struct ustream_buf_list *l)
75 {
76 struct ustream_buf *buf = l->head;
77
78 while (buf) {
79 struct ustream_buf *next = buf->next;
80
81 free(buf);
82 buf = next;
83 }
84 l->head = NULL;
85 l->tail = NULL;
86 l->data_tail = NULL;
87 }
88
89 void ustream_free(struct ustream *s)
90 {
91 if (s->free)
92 s->free(s);
93
94 uloop_timeout_cancel(&s->state_change);
95 ustream_free_buffers(&s->r);
96 ustream_free_buffers(&s->w);
97 }
98
99 static void ustream_state_change_cb(struct uloop_timeout *t)
100 {
101 struct ustream *s = container_of(t, struct ustream, state_change);
102
103 if (s->write_error)
104 ustream_free_buffers(&s->w);
105 if (s->notify_state)
106 s->notify_state(s);
107 }
108
109 void ustream_init_defaults(struct ustream *s)
110 {
111 #define DEFAULT_SET(_f, _default) \
112 do { \
113 if (!_f) \
114 _f = _default; \
115 } while(0)
116
117 DEFAULT_SET(s->r.alloc, ustream_alloc_default);
118 DEFAULT_SET(s->w.alloc, ustream_alloc_default);
119
120 DEFAULT_SET(s->r.min_buffers, 1);
121 DEFAULT_SET(s->r.max_buffers, 1);
122 DEFAULT_SET(s->r.buffer_len, 4096);
123
124 DEFAULT_SET(s->w.min_buffers, 2);
125 DEFAULT_SET(s->w.max_buffers, -1);
126 DEFAULT_SET(s->w.buffer_len, 256);
127
128 #undef DEFAULT_SET
129
130 s->state_change.cb = ustream_state_change_cb;
131 s->write_error = false;
132 s->eof = false;
133 s->eof_write_done = false;
134 s->read_blocked = 0;
135
136 s->r.buffers = 0;
137 s->r.data_bytes = 0;
138
139 s->w.buffers = 0;
140 s->w.data_bytes = 0;
141 }
142
143 static bool ustream_should_move(struct ustream_buf_list *l, struct ustream_buf *buf, int len)
144 {
145 int maxlen;
146 int offset;
147
148 if (buf->data == buf->head)
149 return false;
150
151 maxlen = buf->end - buf->head;
152 offset = buf->data - buf->head;
153
154 if (offset > maxlen / 2)
155 return true;
156
157 if (buf->tail - buf->data < 32 && offset > maxlen / 4)
158 return true;
159
160 if (buf != l->tail || ustream_can_alloc(l))
161 return false;
162
163 return (buf->end - buf->tail < len);
164 }
165
166 static void ustream_free_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
167 {
168 if (buf == l->head)
169 l->head = buf->next;
170
171 if (buf == l->data_tail)
172 l->data_tail = buf->next;
173
174 if (buf == l->tail)
175 l->tail = NULL;
176
177 if (--l->buffers >= l->min_buffers) {
178 free(buf);
179 return;
180 }
181
182 /* recycle */
183 ustream_init_buf(buf, buf->end - buf->head);
184 ustream_add_buf(l, buf);
185 }
186
187 static void __ustream_set_read_blocked(struct ustream *s, unsigned char val)
188 {
189 bool changed = !!s->read_blocked != !!val;
190
191 s->read_blocked = val;
192 if (changed)
193 s->set_read_blocked(s);
194 }
195
196 void ustream_set_read_blocked(struct ustream *s, bool set)
197 {
198 unsigned char val = s->read_blocked & ~READ_BLOCKED_USER;
199
200 if (set)
201 val |= READ_BLOCKED_USER;
202
203 __ustream_set_read_blocked(s, val);
204 }
205
206 void ustream_consume(struct ustream *s, int len)
207 {
208 struct ustream_buf *buf = s->r.head;
209
210 if (!len)
211 return;
212
213 s->r.data_bytes -= len;
214 if (s->r.data_bytes < 0)
215 abort();
216
217 do {
218 struct ustream_buf *next = buf->next;
219 int buf_len = buf->tail - buf->data;
220
221 if (len < buf_len) {
222 buf->data += len;
223 break;
224 }
225
226 len -= buf_len;
227 ustream_free_buf(&s->r, buf);
228 buf = next;
229 } while(len);
230
231 __ustream_set_read_blocked(s, s->read_blocked & ~READ_BLOCKED_FULL);
232 }
233
234 static void ustream_fixup_string(struct ustream *s, struct ustream_buf *buf)
235 {
236 if (!s->string_data)
237 return;
238
239 *buf->tail = 0;
240 }
241
242 static bool ustream_prepare_buf(struct ustream *s, struct ustream_buf_list *l, int len)
243 {
244 struct ustream_buf *buf;
245
246 buf = l->data_tail;
247 if (buf) {
248 if (ustream_should_move(l, buf, len)) {
249 int len = buf->tail - buf->data;
250
251 memmove(buf->head, buf->data, len);
252 buf->data = buf->head;
253 buf->tail = buf->data + len;
254
255 if (l == &s->r)
256 ustream_fixup_string(s, buf);
257 }
258 if (buf->tail != buf->end)
259 return true;
260 }
261
262 if (buf && buf->next) {
263 l->data_tail = buf->next;
264 return true;
265 }
266
267 if (!ustream_can_alloc(l))
268 return false;
269
270 if (l->alloc(s, l) < 0)
271 return false;
272
273 l->data_tail = l->tail;
274 return true;
275 }
276
277 char *ustream_reserve(struct ustream *s, int len, int *maxlen)
278 {
279 struct ustream_buf *buf;
280
281 if (!ustream_prepare_buf(s, &s->r, len)) {
282 __ustream_set_read_blocked(s, s->read_blocked | READ_BLOCKED_FULL);
283 *maxlen = 0;
284 return NULL;
285 }
286
287 buf = s->r.data_tail;
288 *maxlen = buf->end - buf->tail;
289 return buf->tail;
290 }
291
292 void ustream_fill_read(struct ustream *s, int len)
293 {
294 struct ustream_buf *buf = s->r.data_tail;
295 int n = len;
296 int maxlen;
297
298 s->r.data_bytes += len;
299 do {
300 if (!buf)
301 abort();
302
303 maxlen = buf->end - buf->tail;
304 if (len < maxlen)
305 maxlen = len;
306
307 len -= maxlen;
308 buf->tail += maxlen;
309 ustream_fixup_string(s, buf);
310
311 s->r.data_tail = buf;
312 buf = buf->next;
313 } while (len);
314
315 if (s->notify_read)
316 s->notify_read(s, n);
317 }
318
319 char *ustream_get_read_buf(struct ustream *s, int *buflen)
320 {
321 char *data = NULL;
322 int len = 0;
323
324 if (s->r.head) {
325 len = s->r.head->tail - s->r.head->data;
326 if (len > 0)
327 data = s->r.head->data;
328 }
329
330 if (buflen)
331 *buflen = len;
332
333 return data;
334 }
335
336 int ustream_read(struct ustream *s, char *buf, int buflen)
337 {
338 char *chunk;
339 int chunk_len;
340 int len = 0;
341
342 do {
343 chunk = ustream_get_read_buf(s, &chunk_len);
344 if (!chunk)
345 break;
346 if (chunk_len > buflen - len)
347 chunk_len = buflen - len;
348 memcpy(buf + len, chunk, chunk_len);
349 ustream_consume(s, chunk_len);
350 len += chunk_len;
351 } while (len < buflen);
352
353 return len;
354 }
355
356 static void ustream_write_error(struct ustream *s)
357 {
358 if (!s->write_error)
359 ustream_state_change(s);
360 s->write_error = true;
361 }
362
363 bool ustream_write_pending(struct ustream *s)
364 {
365 struct ustream_buf *buf = s->w.head;
366 int wr = 0, len;
367
368 if (s->write_error)
369 return false;
370
371 while (buf && s->w.data_bytes) {
372 struct ustream_buf *next = buf->next;
373 int maxlen = buf->tail - buf->data;
374
375 len = s->write(s, buf->data, maxlen, !!buf->next);
376 if (len < 0) {
377 ustream_write_error(s);
378 break;
379 }
380
381 if (len == 0)
382 break;
383
384 wr += len;
385 s->w.data_bytes -= len;
386 if (len < maxlen) {
387 buf->data += len;
388 break;
389 }
390
391 ustream_free_buf(&s->w, buf);
392 buf = next;
393 }
394
395 if (s->notify_write)
396 s->notify_write(s, wr);
397
398 if (s->eof && wr && !s->w.data_bytes)
399 ustream_state_change(s);
400
401 return !s->w.data_bytes;
402 }
403
404 static int ustream_write_buffered(struct ustream *s, const char *data, int len, int wr)
405 {
406 struct ustream_buf_list *l = &s->w;
407 struct ustream_buf *buf;
408 int maxlen;
409
410 while (len) {
411 if (!ustream_prepare_buf(s, &s->w, len))
412 break;
413
414 buf = l->data_tail;
415
416 maxlen = buf->end - buf->tail;
417 if (maxlen > len)
418 maxlen = len;
419
420 memcpy(buf->tail, data, maxlen);
421 buf->tail += maxlen;
422 data += maxlen;
423 len -= maxlen;
424 wr += maxlen;
425 l->data_bytes += maxlen;
426 }
427
428 return wr;
429 }
430
431 int ustream_write(struct ustream *s, const char *data, int len, bool more)
432 {
433 struct ustream_buf_list *l = &s->w;
434 int wr = 0;
435
436 if (s->write_error)
437 return 0;
438
439 if (!l->data_bytes) {
440 wr = s->write(s, data, len, more);
441 if (wr == len)
442 return wr;
443
444 if (wr < 0) {
445 ustream_write_error(s);
446 return wr;
447 }
448
449 data += wr;
450 len -= wr;
451 }
452
453 return ustream_write_buffered(s, data, len, wr);
454 }
455
456 #define MAX_STACK_BUFLEN 256
457
458 int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
459 {
460 struct ustream_buf_list *l = &s->w;
461 char *buf;
462 va_list arg2;
463 int wr, maxlen, buflen;
464
465 if (s->write_error)
466 return 0;
467
468 if (!l->data_bytes) {
469 buf = alloca(MAX_STACK_BUFLEN);
470 va_copy(arg2, arg);
471 maxlen = vsnprintf(buf, MAX_STACK_BUFLEN, format, arg2);
472 va_end(arg2);
473 if (maxlen < MAX_STACK_BUFLEN) {
474 wr = s->write(s, buf, maxlen, false);
475 if (wr < 0) {
476 ustream_write_error(s);
477 return wr;
478 }
479 if (wr == maxlen)
480 return wr;
481
482 buf += wr;
483 maxlen -= wr;
484 return ustream_write_buffered(s, buf, maxlen, wr);
485 } else {
486 buf = malloc(maxlen + 1);
487 wr = vsnprintf(buf, maxlen + 1, format, arg);
488 wr = ustream_write(s, buf, wr, false);
489 free(buf);
490 return wr;
491 }
492 }
493
494 if (!ustream_prepare_buf(s, l, 1))
495 return 0;
496
497 buf = l->data_tail->tail;
498 buflen = l->data_tail->end - buf;
499
500 va_copy(arg2, arg);
501 maxlen = vsnprintf(buf, buflen, format, arg2);
502 va_end(arg2);
503
504 wr = maxlen;
505 if (wr >= buflen)
506 wr = buflen - 1;
507
508 l->data_tail->tail += wr;
509 l->data_bytes += wr;
510 if (maxlen < buflen)
511 return wr;
512
513 buf = malloc(maxlen + 1);
514 maxlen = vsnprintf(buf, maxlen + 1, format, arg);
515 wr = ustream_write_buffered(s, buf + wr, maxlen - wr, wr);
516 free(buf);
517
518 return wr;
519 }
520
521 int ustream_printf(struct ustream *s, const char *format, ...)
522 {
523 va_list arg;
524 int ret;
525
526 if (s->write_error)
527 return 0;
528
529 va_start(arg, format);
530 ret = ustream_vprintf(s, format, arg);
531 va_end(arg);
532
533 return ret;
534 }