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);
71 ustream_init_buf(buf, l->buffer_len);
72 ustream_add_buf(l, buf);
77 static void ustream_free_buffers(struct ustream_buf_list *l)
79 struct ustream_buf *buf = l->head;
82 struct ustream_buf *next = buf->next;
92 void ustream_free(struct ustream *s)
97 uloop_timeout_cancel(&s->state_change);
98 ustream_free_buffers(&s->r);
99 ustream_free_buffers(&s->w);
102 static void ustream_state_change_cb(struct uloop_timeout *t)
104 struct ustream *s = container_of(t, struct ustream, state_change);
107 ustream_free_buffers(&s->w);
112 void ustream_init_defaults(struct ustream *s)
114 #define DEFAULT_SET(_f, _default) \
120 DEFAULT_SET(s->r.alloc, ustream_alloc_default);
121 DEFAULT_SET(s->w.alloc, ustream_alloc_default);
123 DEFAULT_SET(s->r.min_buffers, 1);
124 DEFAULT_SET(s->r.max_buffers, 1);
125 DEFAULT_SET(s->r.buffer_len, 4096);
127 DEFAULT_SET(s->w.min_buffers, 2);
128 DEFAULT_SET(s->w.max_buffers, -1);
129 DEFAULT_SET(s->w.buffer_len, 256);
133 s->state_change.cb = ustream_state_change_cb;
134 s->write_error = false;
136 s->eof_write_done = false;
146 static bool ustream_should_move(struct ustream_buf_list *l, struct ustream_buf *buf, int len)
151 /* nothing to squeeze */
152 if (buf->data == buf->head)
155 maxlen = buf->end - buf->head;
156 offset = buf->data - buf->head;
158 /* less than half is available */
159 if (offset > maxlen / 2)
162 /* less than 32 bytes data but takes more than 1/4 space */
163 if (buf->tail - buf->data < 32 && offset > maxlen / 4)
166 /* more buf is already in list or can be allocated */
167 if (buf != l->tail || ustream_can_alloc(l))
170 /* no need to move if len is available at the tail */
171 return (buf->end - buf->tail < len);
174 static void ustream_free_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
179 if (buf == l->data_tail)
180 l->data_tail = buf->next;
185 if (--l->buffers >= l->min_buffers) {
191 ustream_init_buf(buf, buf->end - buf->head);
192 ustream_add_buf(l, buf);
195 static void __ustream_set_read_blocked(struct ustream *s, unsigned char val)
197 bool changed = !!s->read_blocked != !!val;
199 s->read_blocked = val;
201 s->set_read_blocked(s);
204 void ustream_set_read_blocked(struct ustream *s, bool set)
206 unsigned char val = s->read_blocked & ~READ_BLOCKED_USER;
209 val |= READ_BLOCKED_USER;
211 __ustream_set_read_blocked(s, val);
214 void ustream_consume(struct ustream *s, int len)
216 struct ustream_buf *buf = s->r.head;
221 s->r.data_bytes -= len;
222 if (s->r.data_bytes < 0)
226 struct ustream_buf *next = buf->next;
227 int buf_len = buf->tail - buf->data;
235 ustream_free_buf(&s->r, buf);
239 __ustream_set_read_blocked(s, s->read_blocked & ~READ_BLOCKED_FULL);
242 static void ustream_fixup_string(struct ustream *s, struct ustream_buf *buf)
250 static bool ustream_prepare_buf(struct ustream *s, struct ustream_buf_list *l, int len)
252 struct ustream_buf *buf;
256 if (ustream_should_move(l, buf, len)) {
257 int len = buf->tail - buf->data;
259 memmove(buf->head, buf->data, len);
260 buf->data = buf->head;
261 buf->tail = buf->data + len;
264 ustream_fixup_string(s, buf);
266 /* some chunks available at the tail */
267 if (buf->tail != buf->end)
269 /* next buf available */
271 l->data_tail = buf->next;
276 if (!ustream_can_alloc(l))
279 if (l->alloc(s, l) < 0)
282 l->data_tail = l->tail;
286 char *ustream_reserve(struct ustream *s, int len, int *maxlen)
288 struct ustream_buf *buf;
290 if (!ustream_prepare_buf(s, &s->r, len)) {
291 __ustream_set_read_blocked(s, s->read_blocked | READ_BLOCKED_FULL);
296 buf = s->r.data_tail;
297 *maxlen = buf->end - buf->tail;
301 void ustream_fill_read(struct ustream *s, int len)
303 struct ustream_buf *buf = s->r.data_tail;
307 s->r.data_bytes += len;
312 maxlen = buf->end - buf->tail;
318 ustream_fixup_string(s, buf);
320 s->r.data_tail = buf;
325 s->notify_read(s, n);
328 char *ustream_get_read_buf(struct ustream *s, int *buflen)
334 len = s->r.head->tail - s->r.head->data;
336 data = s->r.head->data;
345 int ustream_read(struct ustream *s, char *buf, int buflen)
352 chunk = ustream_get_read_buf(s, &chunk_len);
355 if (chunk_len > buflen - len)
356 chunk_len = buflen - len;
357 memcpy(buf + len, chunk, chunk_len);
358 ustream_consume(s, chunk_len);
360 } while (len < buflen);
365 static void ustream_write_error(struct ustream *s)
368 ustream_state_change(s);
369 s->write_error = true;
372 bool ustream_write_pending(struct ustream *s)
374 struct ustream_buf *buf = s->w.head;
380 while (buf && s->w.data_bytes) {
381 struct ustream_buf *next = buf->next;
382 int maxlen = buf->tail - buf->data;
384 len = s->write(s, buf->data, maxlen, !!buf->next);
386 ustream_write_error(s);
394 s->w.data_bytes -= len;
400 ustream_free_buf(&s->w, buf);
405 s->notify_write(s, wr);
407 if (s->eof && wr && !s->w.data_bytes)
408 ustream_state_change(s);
410 return !s->w.data_bytes;
413 static int ustream_write_buffered(struct ustream *s, const char *data, int len, int wr)
415 struct ustream_buf_list *l = &s->w;
416 struct ustream_buf *buf;
420 if (!ustream_prepare_buf(s, &s->w, len))
425 maxlen = buf->end - buf->tail;
429 memcpy(buf->tail, data, maxlen);
434 l->data_bytes += maxlen;
440 int ustream_write(struct ustream *s, const char *data, int len, bool more)
442 struct ustream_buf_list *l = &s->w;
448 if (!l->data_bytes) {
449 wr = s->write(s, data, len, more);
454 ustream_write_error(s);
462 return ustream_write_buffered(s, data, len, wr);
465 #define MAX_STACK_BUFLEN 256
467 int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
469 struct ustream_buf_list *l = &s->w;
472 int wr, maxlen, buflen;
477 if (!l->data_bytes) {
478 buf = alloca(MAX_STACK_BUFLEN);
480 maxlen = vsnprintf(buf, MAX_STACK_BUFLEN, format, arg2);
482 if (maxlen < MAX_STACK_BUFLEN) {
483 wr = s->write(s, buf, maxlen, false);
485 ustream_write_error(s);
493 return ustream_write_buffered(s, buf, maxlen, wr);
495 buf = malloc(maxlen + 1);
498 wr = vsnprintf(buf, maxlen + 1, format, arg);
499 wr = ustream_write(s, buf, wr, false);
505 if (!ustream_prepare_buf(s, l, 1))
508 buf = l->data_tail->tail;
509 buflen = l->data_tail->end - buf;
512 maxlen = vsnprintf(buf, buflen, format, arg2);
519 l->data_tail->tail += wr;
524 buf = malloc(maxlen + 1);
527 maxlen = vsnprintf(buf, maxlen + 1, format, arg);
528 wr = ustream_write_buffered(s, buf + wr, maxlen - wr, wr);
534 int ustream_printf(struct ustream *s, const char *format, ...)
542 va_start(arg, format);
543 ret = ustream_vprintf(s, format, arg);