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
)
48 l
->data_tail
= l
->head
;
51 static bool ustream_can_alloc(struct ustream_buf_list
*l
)
53 if (l
->max_buffers
<= 0)
56 return (l
->buffers
< l
->max_buffers
);
59 static int ustream_alloc_default(struct ustream
*s
, struct ustream_buf_list
*l
)
61 struct ustream_buf
*buf
;
63 if (!ustream_can_alloc(l
))
66 buf
= malloc(sizeof(*buf
) + l
->buffer_len
+ s
->string_data
);
67 ustream_init_buf(buf
, l
->buffer_len
);
68 ustream_add_buf(l
, buf
);
73 static void ustream_free_buffers(struct ustream_buf_list
*l
)
75 struct ustream_buf
*buf
= l
->head
;
78 struct ustream_buf
*next
= buf
->next
;
88 void ustream_free(struct ustream
*s
)
93 uloop_timeout_cancel(&s
->state_change
);
94 ustream_free_buffers(&s
->r
);
95 ustream_free_buffers(&s
->w
);
96 s
->write_error
= false;
101 static void ustream_state_change_cb(struct uloop_timeout
*t
)
103 struct ustream
*s
= container_of(t
, struct ustream
, state_change
);
106 ustream_free_buffers(&s
->w
);
111 void ustream_init_defaults(struct ustream
*s
)
113 #define DEFAULT_SET(_f, _default) \
119 DEFAULT_SET(s
->r
.alloc
, ustream_alloc_default
);
120 DEFAULT_SET(s
->w
.alloc
, ustream_alloc_default
);
122 DEFAULT_SET(s
->r
.min_buffers
, 1);
123 DEFAULT_SET(s
->r
.max_buffers
, 1);
124 DEFAULT_SET(s
->r
.buffer_len
, 4096);
126 DEFAULT_SET(s
->w
.min_buffers
, 2);
127 DEFAULT_SET(s
->w
.max_buffers
, -1);
128 DEFAULT_SET(s
->w
.buffer_len
, 256);
132 s
->state_change
.cb
= ustream_state_change_cb
;
135 static bool ustream_should_move(struct ustream_buf_list
*l
, struct ustream_buf
*buf
, int len
)
140 if (buf
->data
== buf
->head
)
143 maxlen
= buf
->end
- buf
->head
;
144 offset
= buf
->data
- buf
->head
;
146 if (offset
> maxlen
/ 2)
149 if (buf
->tail
- buf
->data
< 32 && offset
> maxlen
/ 4)
152 if (buf
!= l
->tail
|| ustream_can_alloc(l
))
155 return (buf
->end
- buf
->tail
< len
);
158 static void ustream_free_buf(struct ustream_buf_list
*l
, struct ustream_buf
*buf
)
163 if (buf
== l
->data_tail
)
164 l
->data_tail
= buf
->next
;
169 if (--l
->buffers
>= l
->min_buffers
) {
175 ustream_init_buf(buf
, buf
->end
- buf
->head
);
176 ustream_add_buf(l
, buf
);
179 static void __ustream_set_read_blocked(struct ustream
*s
, unsigned char val
)
181 bool changed
= !!s
->read_blocked
!= !!val
;
183 s
->read_blocked
= val
;
185 s
->set_read_blocked(s
);
188 void ustream_set_read_blocked(struct ustream
*s
, bool set
)
190 unsigned char val
= s
->read_blocked
& ~READ_BLOCKED_USER
;
193 val
|= READ_BLOCKED_USER
;
195 __ustream_set_read_blocked(s
, val
);
198 void ustream_consume(struct ustream
*s
, int len
)
200 struct ustream_buf
*buf
= s
->r
.head
;
205 s
->r
.data_bytes
-= len
;
206 if (s
->r
.data_bytes
< 0)
210 struct ustream_buf
*next
= buf
->next
;
211 int buf_len
= buf
->tail
- buf
->data
;
219 ustream_free_buf(&s
->r
, buf
);
223 __ustream_set_read_blocked(s
, s
->read_blocked
& ~READ_BLOCKED_FULL
);
226 static void ustream_fixup_string(struct ustream
*s
, struct ustream_buf
*buf
)
234 static bool ustream_prepare_buf(struct ustream
*s
, struct ustream_buf_list
*l
, int len
)
236 struct ustream_buf
*buf
;
240 if (ustream_should_move(l
, buf
, len
)) {
241 int len
= buf
->tail
- buf
->data
;
243 memmove(buf
->head
, buf
->data
, len
);
244 buf
->data
= buf
->head
;
245 buf
->tail
= buf
->data
+ len
;
248 ustream_fixup_string(s
, buf
);
250 if (buf
->tail
!= buf
->end
)
254 if (buf
&& buf
->next
) {
255 l
->data_tail
= buf
->next
;
259 if (!ustream_can_alloc(l
))
262 if (l
->alloc(s
, l
) < 0)
265 l
->data_tail
= l
->tail
;
269 char *ustream_reserve(struct ustream
*s
, int len
, int *maxlen
)
271 struct ustream_buf
*buf
= s
->r
.head
;
273 if (!ustream_prepare_buf(s
, &s
->r
, len
)) {
274 __ustream_set_read_blocked(s
, s
->read_blocked
| READ_BLOCKED_FULL
);
279 buf
= s
->r
.data_tail
;
280 *maxlen
= buf
->end
- buf
->tail
;
284 void ustream_fill_read(struct ustream
*s
, int len
)
286 struct ustream_buf
*buf
= s
->r
.data_tail
;
290 s
->r
.data_bytes
+= len
;
295 maxlen
= buf
->end
- buf
->tail
;
301 ustream_fixup_string(s
, buf
);
303 s
->r
.data_tail
= buf
;
308 s
->notify_read(s
, n
);
311 char *ustream_get_read_buf(struct ustream
*s
, int *buflen
)
317 len
= s
->r
.head
->tail
- s
->r
.head
->data
;
318 data
= s
->r
.head
->data
;
330 static void ustream_write_error(struct ustream
*s
)
332 s
->write_error
= true;
333 ustream_state_change(s
);
336 bool ustream_write_pending(struct ustream
*s
)
338 struct ustream_buf
*buf
= s
->w
.head
;
345 struct ustream_buf
*next
= buf
->next
;
346 int maxlen
= buf
->tail
- buf
->data
;
348 len
= s
->write(s
, buf
->data
, maxlen
, !!buf
->next
);
350 ustream_write_error(s
);
358 s
->w
.data_bytes
-= len
;
364 ustream_free_buf(&s
->w
, buf
);
369 s
->notify_write(s
, wr
);
371 if (s
->eof
&& wr
&& !s
->w
.data_bytes
)
372 ustream_state_change(s
);
374 return !s
->w
.data_bytes
;
377 static int ustream_write_buffered(struct ustream
*s
, const char *data
, int len
, int wr
)
379 struct ustream_buf_list
*l
= &s
->w
;
380 struct ustream_buf
*buf
;
384 if (!ustream_prepare_buf(s
, &s
->w
, len
))
389 maxlen
= buf
->end
- buf
->tail
;
393 memcpy(buf
->tail
, data
, maxlen
);
398 l
->data_bytes
+= maxlen
;
404 int ustream_write(struct ustream
*s
, const char *data
, int len
, bool more
)
406 struct ustream_buf_list
*l
= &s
->w
;
412 if (!l
->data_bytes
) {
413 wr
= s
->write(s
, data
, len
, more
);
418 ustream_write_error(s
);
426 return ustream_write_buffered(s
, data
, len
, wr
);
429 #define MAX_STACK_BUFLEN 256
431 int ustream_vprintf(struct ustream
*s
, const char *format
, va_list arg
)
433 struct ustream_buf_list
*l
= &s
->w
;
436 int wr
, maxlen
, buflen
;
438 if (!l
->data_bytes
) {
439 buf
= alloca(MAX_STACK_BUFLEN
);
441 maxlen
= vsnprintf(buf
, MAX_STACK_BUFLEN
, format
, arg2
);
443 if (maxlen
< MAX_STACK_BUFLEN
) {
444 wr
= s
->write(s
, buf
, maxlen
, false);
446 ustream_write_error(s
);
454 return ustream_write_buffered(s
, buf
, maxlen
, wr
);
456 buf
= malloc(maxlen
+ 1);
457 wr
= vsnprintf(buf
, maxlen
+ 1, format
, arg
);
458 wr
= ustream_write(s
, buf
, wr
, false);
464 if (!ustream_prepare_buf(s
, l
, 1))
467 buf
= l
->data_tail
->tail
;
468 buflen
= l
->data_tail
->end
- buf
;
471 maxlen
= vsnprintf(buf
, buflen
, format
, arg2
);
478 l
->data_tail
->tail
+= wr
;
483 buf
= malloc(maxlen
+ 1);
484 maxlen
= vsnprintf(buf
, maxlen
+ 1, format
, arg
);
485 wr
= ustream_write_buffered(s
, buf
+ wr
, maxlen
- wr
, wr
);
491 int ustream_printf(struct ustream
*s
, const char *format
, ...)
496 va_start(arg
, format
);
497 ret
= ustream_vprintf(s
, format
, arg
);