blobmsg_example: fix output
[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 static void ustream_write_error(struct ustream *s)
337 {
338 if (!s->write_error)
339 ustream_state_change(s);
340 s->write_error = true;
341 }
342
343 bool ustream_write_pending(struct ustream *s)
344 {
345 struct ustream_buf *buf = s->w.head;
346 int wr = 0, len;
347
348 if (s->write_error)
349 return false;
350
351 while (buf && s->w.data_bytes) {
352 struct ustream_buf *next = buf->next;
353 int maxlen = buf->tail - buf->data;
354
355 len = s->write(s, buf->data, maxlen, !!buf->next);
356 if (len < 0) {
357 ustream_write_error(s);
358 break;
359 }
360
361 if (len == 0)
362 break;
363
364 wr += len;
365 s->w.data_bytes -= len;
366 if (len < maxlen) {
367 buf->data += len;
368 break;
369 }
370
371 ustream_free_buf(&s->w, buf);
372 buf = next;
373 }
374
375 if (s->notify_write)
376 s->notify_write(s, wr);
377
378 if (s->eof && wr && !s->w.data_bytes)
379 ustream_state_change(s);
380
381 return !s->w.data_bytes;
382 }
383
384 static int ustream_write_buffered(struct ustream *s, const char *data, int len, int wr)
385 {
386 struct ustream_buf_list *l = &s->w;
387 struct ustream_buf *buf;
388 int maxlen;
389
390 while (len) {
391 if (!ustream_prepare_buf(s, &s->w, len))
392 break;
393
394 buf = l->data_tail;
395
396 maxlen = buf->end - buf->tail;
397 if (maxlen > len)
398 maxlen = len;
399
400 memcpy(buf->tail, data, maxlen);
401 buf->tail += maxlen;
402 data += maxlen;
403 len -= maxlen;
404 wr += maxlen;
405 l->data_bytes += maxlen;
406 }
407
408 return wr;
409 }
410
411 int ustream_write(struct ustream *s, const char *data, int len, bool more)
412 {
413 struct ustream_buf_list *l = &s->w;
414 int wr = 0;
415
416 if (s->write_error)
417 return 0;
418
419 if (!l->data_bytes) {
420 wr = s->write(s, data, len, more);
421 if (wr == len)
422 return wr;
423
424 if (wr < 0) {
425 ustream_write_error(s);
426 return wr;
427 }
428
429 data += wr;
430 len -= wr;
431 }
432
433 return ustream_write_buffered(s, data, len, wr);
434 }
435
436 #define MAX_STACK_BUFLEN 256
437
438 int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
439 {
440 struct ustream_buf_list *l = &s->w;
441 char *buf;
442 va_list arg2;
443 int wr, maxlen, buflen;
444
445 if (s->write_error)
446 return 0;
447
448 if (!l->data_bytes) {
449 buf = alloca(MAX_STACK_BUFLEN);
450 va_copy(arg2, arg);
451 maxlen = vsnprintf(buf, MAX_STACK_BUFLEN, format, arg2);
452 va_end(arg2);
453 if (maxlen < MAX_STACK_BUFLEN) {
454 wr = s->write(s, buf, maxlen, false);
455 if (wr < 0) {
456 ustream_write_error(s);
457 return wr;
458 }
459 if (wr == maxlen)
460 return wr;
461
462 buf += wr;
463 maxlen -= wr;
464 return ustream_write_buffered(s, buf, maxlen, wr);
465 } else {
466 buf = malloc(maxlen + 1);
467 wr = vsnprintf(buf, maxlen + 1, format, arg);
468 wr = ustream_write(s, buf, wr, false);
469 free(buf);
470 return wr;
471 }
472 }
473
474 if (!ustream_prepare_buf(s, l, 1))
475 return 0;
476
477 buf = l->data_tail->tail;
478 buflen = l->data_tail->end - buf;
479
480 va_copy(arg2, arg);
481 maxlen = vsnprintf(buf, buflen, format, arg2);
482 va_end(arg2);
483
484 wr = maxlen;
485 if (wr >= buflen)
486 wr = buflen - 1;
487
488 l->data_tail->tail += wr;
489 l->data_bytes += wr;
490 if (maxlen < buflen)
491 return wr;
492
493 buf = malloc(maxlen + 1);
494 maxlen = vsnprintf(buf, maxlen + 1, format, arg);
495 wr = ustream_write_buffered(s, buf + wr, maxlen - wr, wr);
496 free(buf);
497
498 return wr;
499 }
500
501 int ustream_printf(struct ustream *s, const char *format, ...)
502 {
503 va_list arg;
504 int ret;
505
506 if (s->write_error)
507 return 0;
508
509 va_start(arg, format);
510 ret = ustream_vprintf(s, format, arg);
511 va_end(arg);
512
513 return ret;
514 }