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 if (buf->data == buf->head)
151 maxlen = buf->end - buf->head;
152 offset = buf->data - buf->head;
154 if (offset > maxlen / 2)
157 if (buf->tail - buf->data < 32 && offset > maxlen / 4)
160 if (buf != l->tail || ustream_can_alloc(l))
163 return (buf->end - buf->tail < len);
166 static void ustream_free_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
171 if (buf == l->data_tail)
172 l->data_tail = buf->next;
177 if (--l->buffers >= l->min_buffers) {
183 ustream_init_buf(buf, buf->end - buf->head);
184 ustream_add_buf(l, buf);
187 static void __ustream_set_read_blocked(struct ustream *s, unsigned char val)
189 bool changed = !!s->read_blocked != !!val;
191 s->read_blocked = val;
193 s->set_read_blocked(s);
196 void ustream_set_read_blocked(struct ustream *s, bool set)
198 unsigned char val = s->read_blocked & ~READ_BLOCKED_USER;
201 val |= READ_BLOCKED_USER;
203 __ustream_set_read_blocked(s, val);
206 void ustream_consume(struct ustream *s, int len)
208 struct ustream_buf *buf = s->r.head;
213 s->r.data_bytes -= len;
214 if (s->r.data_bytes < 0)
218 struct ustream_buf *next = buf->next;
219 int buf_len = buf->tail - buf->data;
227 ustream_free_buf(&s->r, buf);
231 __ustream_set_read_blocked(s, s->read_blocked & ~READ_BLOCKED_FULL);
234 static void ustream_fixup_string(struct ustream *s, struct ustream_buf *buf)
242 static bool ustream_prepare_buf(struct ustream *s, struct ustream_buf_list *l, int len)
244 struct ustream_buf *buf;
248 if (ustream_should_move(l, buf, len)) {
249 int len = buf->tail - buf->data;
251 memmove(buf->head, buf->data, len);
252 buf->data = buf->head;
253 buf->tail = buf->data + len;
256 ustream_fixup_string(s, buf);
258 if (buf->tail != buf->end)
262 if (buf && buf->next) {
263 l->data_tail = buf->next;
267 if (!ustream_can_alloc(l))
270 if (l->alloc(s, l) < 0)
273 l->data_tail = l->tail;
277 char *ustream_reserve(struct ustream *s, int len, int *maxlen)
279 struct ustream_buf *buf;
281 if (!ustream_prepare_buf(s, &s->r, len)) {
282 __ustream_set_read_blocked(s, s->read_blocked | READ_BLOCKED_FULL);
287 buf = s->r.data_tail;
288 *maxlen = buf->end - buf->tail;
292 void ustream_fill_read(struct ustream *s, int len)
294 struct ustream_buf *buf = s->r.data_tail;
298 s->r.data_bytes += len;
303 maxlen = buf->end - buf->tail;
309 ustream_fixup_string(s, buf);
311 s->r.data_tail = buf;
316 s->notify_read(s, n);
319 char *ustream_get_read_buf(struct ustream *s, int *buflen)
325 len = s->r.head->tail - s->r.head->data;
327 data = s->r.head->data;
336 int ustream_read(struct ustream *s, char *buf, int buflen)
343 chunk = ustream_get_read_buf(s, &chunk_len);
346 if (chunk_len > buflen - len)
347 chunk_len = buflen - len;
348 memcpy(buf + len, chunk, chunk_len);
349 ustream_consume(s, chunk_len);
351 } while (len < buflen);
356 static void ustream_write_error(struct ustream *s)
359 ustream_state_change(s);
360 s->write_error = true;
363 bool ustream_write_pending(struct ustream *s)
365 struct ustream_buf *buf = s->w.head;
371 while (buf && s->w.data_bytes) {
372 struct ustream_buf *next = buf->next;
373 int maxlen = buf->tail - buf->data;
375 len = s->write(s, buf->data, maxlen, !!buf->next);
377 ustream_write_error(s);
385 s->w.data_bytes -= len;
391 ustream_free_buf(&s->w, buf);
396 s->notify_write(s, wr);
398 if (s->eof && wr && !s->w.data_bytes)
399 ustream_state_change(s);
401 return !s->w.data_bytes;
404 static int ustream_write_buffered(struct ustream *s, const char *data, int len, int wr)
406 struct ustream_buf_list *l = &s->w;
407 struct ustream_buf *buf;
411 if (!ustream_prepare_buf(s, &s->w, len))
416 maxlen = buf->end - buf->tail;
420 memcpy(buf->tail, data, maxlen);
425 l->data_bytes += maxlen;
431 int ustream_write(struct ustream *s, const char *data, int len, bool more)
433 struct ustream_buf_list *l = &s->w;
439 if (!l->data_bytes) {
440 wr = s->write(s, data, len, more);
445 ustream_write_error(s);
453 return ustream_write_buffered(s, data, len, wr);
456 #define MAX_STACK_BUFLEN 256
458 int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
460 struct ustream_buf_list *l = &s->w;
463 int wr, maxlen, buflen;
468 if (!l->data_bytes) {
469 buf = alloca(MAX_STACK_BUFLEN);
471 maxlen = vsnprintf(buf, MAX_STACK_BUFLEN, format, arg2);
473 if (maxlen < MAX_STACK_BUFLEN) {
474 wr = s->write(s, buf, maxlen, false);
476 ustream_write_error(s);
484 return ustream_write_buffered(s, buf, maxlen, wr);
486 buf = malloc(maxlen + 1);
487 wr = vsnprintf(buf, maxlen + 1, format, arg);
488 wr = ustream_write(s, buf, wr, false);
494 if (!ustream_prepare_buf(s, l, 1))
497 buf = l->data_tail->tail;
498 buflen = l->data_tail->end - buf;
501 maxlen = vsnprintf(buf, buflen, format, arg2);
508 l->data_tail->tail += wr;
513 buf = malloc(maxlen + 1);
514 maxlen = vsnprintf(buf, maxlen + 1, format, arg);
515 wr = ustream_write_buffered(s, buf + wr, maxlen - wr, wr);
521 int ustream_printf(struct ustream *s, const char *format, ...)
529 va_start(arg, format);
530 ret = ustream_vprintf(s, format, arg);