2 * ustream - library for stream buffer management
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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.
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.
27 static void ustream_init_buf(struct ustream_buf *buf, int len)
32 memset(buf, 0, sizeof(*buf));
33 buf->data = buf->tail = buf->head;
34 buf->end = buf->head + len;
38 static void ustream_add_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
49 l->data_tail = l->head;
52 static bool ustream_can_alloc(struct ustream_buf_list *l)
54 if (l->max_buffers <= 0)
57 return (l->buffers < l->max_buffers);
60 static int ustream_alloc_default(struct ustream *s, struct ustream_buf_list *l)
62 struct ustream_buf *buf;
64 if (!ustream_can_alloc(l))
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);
74 static void ustream_free_buffers(struct ustream_buf_list *l)
76 struct ustream_buf *buf = l->head;
79 struct ustream_buf *next = buf->next;
89 void ustream_free(struct ustream *s)
94 uloop_timeout_cancel(&s->state_change);
95 ustream_free_buffers(&s->r);
96 ustream_free_buffers(&s->w);
99 static void ustream_state_change_cb(struct uloop_timeout *t)
101 struct ustream *s = container_of(t, struct ustream, state_change);
104 ustream_free_buffers(&s->w);
109 void ustream_init_defaults(struct ustream *s)
111 #define DEFAULT_SET(_f, _default) \
117 DEFAULT_SET(s->r.alloc, ustream_alloc_default);
118 DEFAULT_SET(s->w.alloc, ustream_alloc_default);
120 DEFAULT_SET(s->r.min_buffers, 1);
121 DEFAULT_SET(s->r.max_buffers, 1);
122 DEFAULT_SET(s->r.buffer_len, 4096);
124 DEFAULT_SET(s->w.min_buffers, 2);
125 DEFAULT_SET(s->w.max_buffers, -1);
126 DEFAULT_SET(s->w.buffer_len, 256);
130 s->state_change.cb = ustream_state_change_cb;
131 s->write_error = false;
133 s->eof_write_done = false;
143 static bool ustream_should_move(struct ustream_buf_list *l, struct ustream_buf *buf, int len)
148 /* nothing to squeeze */
149 if (buf->data == buf->head)
152 maxlen = buf->end - buf->head;
153 offset = buf->data - buf->head;
155 /* less than half is available */
156 if (offset > maxlen / 2)
159 /* less than 32 bytes data but takes more than 1/4 space */
160 if (buf->tail - buf->data < 32 && offset > maxlen / 4)
163 /* more buf is already in list or can be allocated */
164 if (buf != l->tail || ustream_can_alloc(l))
167 /* no need to move if len is available at the tail */
168 return (buf->end - buf->tail < len);
171 static void ustream_free_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
176 if (buf == l->data_tail)
177 l->data_tail = buf->next;
182 if (--l->buffers >= l->min_buffers) {
188 ustream_init_buf(buf, buf->end - buf->head);
189 ustream_add_buf(l, buf);
192 static void __ustream_set_read_blocked(struct ustream *s, unsigned char val)
194 bool changed = !!s->read_blocked != !!val;
196 s->read_blocked = val;
198 s->set_read_blocked(s);
201 void ustream_set_read_blocked(struct ustream *s, bool set)
203 unsigned char val = s->read_blocked & ~READ_BLOCKED_USER;
206 val |= READ_BLOCKED_USER;
208 __ustream_set_read_blocked(s, val);
211 void ustream_consume(struct ustream *s, int len)
213 struct ustream_buf *buf = s->r.head;
218 s->r.data_bytes -= len;
219 if (s->r.data_bytes < 0)
223 struct ustream_buf *next = buf->next;
224 int buf_len = buf->tail - buf->data;
232 ustream_free_buf(&s->r, buf);
236 __ustream_set_read_blocked(s, s->read_blocked & ~READ_BLOCKED_FULL);
239 static void ustream_fixup_string(struct ustream *s, struct ustream_buf *buf)
247 static bool ustream_prepare_buf(struct ustream *s, struct ustream_buf_list *l, int len)
249 struct ustream_buf *buf;
253 if (ustream_should_move(l, buf, len)) {
254 int len = buf->tail - buf->data;
256 memmove(buf->head, buf->data, len);
257 buf->data = buf->head;
258 buf->tail = buf->data + len;
261 ustream_fixup_string(s, buf);
263 /* some chunks available at the tail */
264 if (buf->tail != buf->end)
266 /* next buf available */
268 l->data_tail = buf->next;
273 if (!ustream_can_alloc(l))
276 if (l->alloc(s, l) < 0)
279 l->data_tail = l->tail;
283 char *ustream_reserve(struct ustream *s, int len, int *maxlen)
285 struct ustream_buf *buf;
287 if (!ustream_prepare_buf(s, &s->r, len)) {
288 __ustream_set_read_blocked(s, s->read_blocked | READ_BLOCKED_FULL);
293 buf = s->r.data_tail;
294 *maxlen = buf->end - buf->tail;
298 void ustream_fill_read(struct ustream *s, int len)
300 struct ustream_buf *buf = s->r.data_tail;
304 s->r.data_bytes += len;
309 maxlen = buf->end - buf->tail;
315 ustream_fixup_string(s, buf);
317 s->r.data_tail = buf;
322 s->notify_read(s, n);
325 char *ustream_get_read_buf(struct ustream *s, int *buflen)
331 len = s->r.head->tail - s->r.head->data;
333 data = s->r.head->data;
342 int ustream_read(struct ustream *s, char *buf, int buflen)
349 chunk = ustream_get_read_buf(s, &chunk_len);
352 if (chunk_len > buflen - len)
353 chunk_len = buflen - len;
354 memcpy(buf + len, chunk, chunk_len);
355 ustream_consume(s, chunk_len);
357 } while (len < buflen);
362 static void ustream_write_error(struct ustream *s)
365 ustream_state_change(s);
366 s->write_error = true;
369 bool ustream_write_pending(struct ustream *s)
371 struct ustream_buf *buf = s->w.head;
377 while (buf && s->w.data_bytes) {
378 struct ustream_buf *next = buf->next;
379 int maxlen = buf->tail - buf->data;
381 len = s->write(s, buf->data, maxlen, !!buf->next);
383 ustream_write_error(s);
391 s->w.data_bytes -= len;
397 ustream_free_buf(&s->w, buf);
402 s->notify_write(s, wr);
404 if (s->eof && wr && !s->w.data_bytes)
405 ustream_state_change(s);
407 return !s->w.data_bytes;
410 static int ustream_write_buffered(struct ustream *s, const char *data, int len, int wr)
412 struct ustream_buf_list *l = &s->w;
413 struct ustream_buf *buf;
417 if (!ustream_prepare_buf(s, &s->w, len))
422 maxlen = buf->end - buf->tail;
426 memcpy(buf->tail, data, maxlen);
431 l->data_bytes += maxlen;
437 int ustream_write(struct ustream *s, const char *data, int len, bool more)
439 struct ustream_buf_list *l = &s->w;
445 if (!l->data_bytes) {
446 wr = s->write(s, data, len, more);
451 ustream_write_error(s);
459 return ustream_write_buffered(s, data, len, wr);
462 #define MAX_STACK_BUFLEN 256
464 int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
466 struct ustream_buf_list *l = &s->w;
469 int wr, maxlen, buflen;
474 if (!l->data_bytes) {
475 buf = alloca(MAX_STACK_BUFLEN);
477 maxlen = vsnprintf(buf, MAX_STACK_BUFLEN, format, arg2);
479 if (maxlen < MAX_STACK_BUFLEN) {
480 wr = s->write(s, buf, maxlen, false);
482 ustream_write_error(s);
490 return ustream_write_buffered(s, buf, maxlen, wr);
492 buf = malloc(maxlen + 1);
493 wr = vsnprintf(buf, maxlen + 1, format, arg);
494 wr = ustream_write(s, buf, wr, false);
500 if (!ustream_prepare_buf(s, l, 1))
503 buf = l->data_tail->tail;
504 buflen = l->data_tail->end - buf;
507 maxlen = vsnprintf(buf, buflen, format, arg2);
514 l->data_tail->tail += wr;
519 buf = malloc(maxlen + 1);
520 maxlen = vsnprintf(buf, maxlen + 1, format, arg);
521 wr = ustream_write_buffered(s, buf + wr, maxlen - wr, wr);
527 int ustream_printf(struct ustream *s, const char *format, ...)
535 va_start(arg, format);
536 ret = ustream_vprintf(s, format, arg);