ustream: increment receive buffer count
[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 s->write_error = false;
98 s->eof = false;
99 s->read_blocked = 0;
100 }
101
102 static void ustream_state_change_cb(struct uloop_timeout *t)
103 {
104 struct ustream *s = container_of(t, struct ustream, state_change);
105
106 if (s->write_error)
107 ustream_free_buffers(&s->w);
108 if (s->notify_state)
109 s->notify_state(s);
110 }
111
112 void ustream_init_defaults(struct ustream *s)
113 {
114 #define DEFAULT_SET(_f, _default) \
115 do { \
116 if (!_f) \
117 _f = _default; \
118 } while(0)
119
120 DEFAULT_SET(s->r.alloc, ustream_alloc_default);
121 DEFAULT_SET(s->w.alloc, ustream_alloc_default);
122
123 DEFAULT_SET(s->r.min_buffers, 1);
124 DEFAULT_SET(s->r.max_buffers, 1);
125 DEFAULT_SET(s->r.buffer_len, 4096);
126
127 DEFAULT_SET(s->w.min_buffers, 2);
128 DEFAULT_SET(s->w.max_buffers, -1);
129 DEFAULT_SET(s->w.buffer_len, 256);
130
131 #undef DEFAULT_SET
132
133 s->state_change.cb = ustream_state_change_cb;
134 }
135
136 static bool ustream_should_move(struct ustream_buf_list *l, struct ustream_buf *buf, int len)
137 {
138 int maxlen;
139 int offset;
140
141 if (buf->data == buf->head)
142 return false;
143
144 maxlen = buf->end - buf->head;
145 offset = buf->data - buf->head;
146
147 if (offset > maxlen / 2)
148 return true;
149
150 if (buf->tail - buf->data < 32 && offset > maxlen / 4)
151 return true;
152
153 if (buf != l->tail || ustream_can_alloc(l))
154 return false;
155
156 return (buf->end - buf->tail < len);
157 }
158
159 static void ustream_free_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
160 {
161 if (buf == l->head)
162 l->head = buf->next;
163
164 if (buf == l->data_tail)
165 l->data_tail = buf->next;
166
167 if (buf == l->tail)
168 l->tail = NULL;
169
170 if (--l->buffers >= l->min_buffers) {
171 free(buf);
172 return;
173 }
174
175 /* recycle */
176 ustream_init_buf(buf, buf->end - buf->head);
177 ustream_add_buf(l, buf);
178 }
179
180 static void __ustream_set_read_blocked(struct ustream *s, unsigned char val)
181 {
182 bool changed = !!s->read_blocked != !!val;
183
184 s->read_blocked = val;
185 if (changed)
186 s->set_read_blocked(s);
187 }
188
189 void ustream_set_read_blocked(struct ustream *s, bool set)
190 {
191 unsigned char val = s->read_blocked & ~READ_BLOCKED_USER;
192
193 if (set)
194 val |= READ_BLOCKED_USER;
195
196 __ustream_set_read_blocked(s, val);
197 }
198
199 void ustream_consume(struct ustream *s, int len)
200 {
201 struct ustream_buf *buf = s->r.head;
202
203 if (!len)
204 return;
205
206 s->r.data_bytes -= len;
207 if (s->r.data_bytes < 0)
208 abort();
209
210 do {
211 struct ustream_buf *next = buf->next;
212 int buf_len = buf->tail - buf->data;
213
214 if (len < buf_len) {
215 buf->data += len;
216 break;
217 }
218
219 len -= buf_len;
220 ustream_free_buf(&s->r, buf);
221 buf = next;
222 } while(len);
223
224 __ustream_set_read_blocked(s, s->read_blocked & ~READ_BLOCKED_FULL);
225 }
226
227 static void ustream_fixup_string(struct ustream *s, struct ustream_buf *buf)
228 {
229 if (!s->string_data)
230 return;
231
232 *buf->tail = 0;
233 }
234
235 static bool ustream_prepare_buf(struct ustream *s, struct ustream_buf_list *l, int len)
236 {
237 struct ustream_buf *buf;
238
239 buf = l->data_tail;
240 if (buf) {
241 if (ustream_should_move(l, buf, len)) {
242 int len = buf->tail - buf->data;
243
244 memmove(buf->head, buf->data, len);
245 buf->data = buf->head;
246 buf->tail = buf->data + len;
247
248 if (l == &s->r)
249 ustream_fixup_string(s, buf);
250 }
251 if (buf->tail != buf->end)
252 return true;
253 }
254
255 if (buf && buf->next) {
256 l->data_tail = buf->next;
257 return true;
258 }
259
260 if (!ustream_can_alloc(l))
261 return false;
262
263 if (l->alloc(s, l) < 0)
264 return false;
265
266 l->data_tail = l->tail;
267 return true;
268 }
269
270 char *ustream_reserve(struct ustream *s, int len, int *maxlen)
271 {
272 struct ustream_buf *buf = s->r.head;
273
274 if (!ustream_prepare_buf(s, &s->r, len)) {
275 __ustream_set_read_blocked(s, s->read_blocked | READ_BLOCKED_FULL);
276 *maxlen = 0;
277 return NULL;
278 }
279
280 buf = s->r.data_tail;
281 *maxlen = buf->end - buf->tail;
282 return buf->tail;
283 }
284
285 void ustream_fill_read(struct ustream *s, int len)
286 {
287 struct ustream_buf *buf = s->r.data_tail;
288 int n = len;
289 int maxlen;
290
291 s->r.data_bytes += len;
292 do {
293 if (!buf)
294 abort();
295
296 maxlen = buf->end - buf->tail;
297 if (len < maxlen)
298 maxlen = len;
299
300 len -= maxlen;
301 buf->tail += maxlen;
302 ustream_fixup_string(s, buf);
303
304 s->r.data_tail = buf;
305 buf = buf->next;
306 } while (len);
307
308 if (s->notify_read)
309 s->notify_read(s, n);
310 }
311
312 char *ustream_get_read_buf(struct ustream *s, int *buflen)
313 {
314 char *data;
315 int len;
316
317 if (s->r.head) {
318 len = s->r.head->tail - s->r.head->data;
319 data = s->r.head->data;
320 } else {
321 len = 0;
322 data = NULL;
323 }
324
325 if (buflen)
326 *buflen = len;
327
328 return data;
329 }
330
331 static void ustream_write_error(struct ustream *s)
332 {
333 s->write_error = true;
334 ustream_state_change(s);
335 }
336
337 bool ustream_write_pending(struct ustream *s)
338 {
339 struct ustream_buf *buf = s->w.head;
340 int wr = 0, len;
341
342 if (s->write_error)
343 return false;
344
345 while (buf && s->w.data_bytes) {
346 struct ustream_buf *next = buf->next;
347 int maxlen = buf->tail - buf->data;
348
349 len = s->write(s, buf->data, maxlen, !!buf->next);
350 if (len < 0) {
351 ustream_write_error(s);
352 break;
353 }
354
355 if (len == 0)
356 break;
357
358 wr += len;
359 s->w.data_bytes -= len;
360 if (len < maxlen) {
361 buf->data += len;
362 break;
363 }
364
365 ustream_free_buf(&s->w, buf);
366 buf = next;
367 }
368
369 if (s->notify_write)
370 s->notify_write(s, wr);
371
372 if (s->eof && wr && !s->w.data_bytes)
373 ustream_state_change(s);
374
375 return !s->w.data_bytes;
376 }
377
378 static int ustream_write_buffered(struct ustream *s, const char *data, int len, int wr)
379 {
380 struct ustream_buf_list *l = &s->w;
381 struct ustream_buf *buf;
382 int maxlen;
383
384 while (len) {
385 if (!ustream_prepare_buf(s, &s->w, len))
386 break;
387
388 buf = l->data_tail;
389
390 maxlen = buf->end - buf->tail;
391 if (maxlen > len)
392 maxlen = len;
393
394 memcpy(buf->tail, data, maxlen);
395 buf->tail += maxlen;
396 data += maxlen;
397 len -= maxlen;
398 wr += maxlen;
399 l->data_bytes += maxlen;
400 }
401
402 return wr;
403 }
404
405 int ustream_write(struct ustream *s, const char *data, int len, bool more)
406 {
407 struct ustream_buf_list *l = &s->w;
408 int wr = 0;
409
410 if (s->write_error)
411 return 0;
412
413 if (!l->data_bytes) {
414 wr = s->write(s, data, len, more);
415 if (wr == len)
416 return wr;
417
418 if (wr < 0) {
419 ustream_write_error(s);
420 return wr;
421 }
422
423 data += wr;
424 len -= wr;
425 }
426
427 return ustream_write_buffered(s, data, len, wr);
428 }
429
430 #define MAX_STACK_BUFLEN 256
431
432 int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
433 {
434 struct ustream_buf_list *l = &s->w;
435 char *buf;
436 va_list arg2;
437 int wr, maxlen, buflen;
438
439 if (s->write_error)
440 return 0;
441
442 if (!l->data_bytes) {
443 buf = alloca(MAX_STACK_BUFLEN);
444 va_copy(arg2, arg);
445 maxlen = vsnprintf(buf, MAX_STACK_BUFLEN, format, arg2);
446 va_end(arg2);
447 if (maxlen < MAX_STACK_BUFLEN) {
448 wr = s->write(s, buf, maxlen, false);
449 if (wr < 0) {
450 ustream_write_error(s);
451 return wr;
452 }
453 if (wr == maxlen)
454 return wr;
455
456 buf += wr;
457 maxlen -= wr;
458 return ustream_write_buffered(s, buf, maxlen, wr);
459 } else {
460 buf = malloc(maxlen + 1);
461 wr = vsnprintf(buf, maxlen + 1, format, arg);
462 wr = ustream_write(s, buf, wr, false);
463 free(buf);
464 return wr;
465 }
466 }
467
468 if (!ustream_prepare_buf(s, l, 1))
469 return 0;
470
471 buf = l->data_tail->tail;
472 buflen = l->data_tail->end - buf;
473
474 va_copy(arg2, arg);
475 maxlen = vsnprintf(buf, buflen, format, arg2);
476 va_end(arg2);
477
478 wr = maxlen;
479 if (wr >= buflen)
480 wr = buflen - 1;
481
482 l->data_tail->tail += wr;
483 l->data_bytes += wr;
484 if (maxlen < buflen)
485 return wr;
486
487 buf = malloc(maxlen + 1);
488 maxlen = vsnprintf(buf, maxlen + 1, format, arg);
489 wr = ustream_write_buffered(s, buf + wr, maxlen - wr, wr);
490 free(buf);
491
492 return wr;
493 }
494
495 int ustream_printf(struct ustream *s, const char *format, ...)
496 {
497 va_list arg;
498 int ret;
499
500 if (s->write_error)
501 return 0;
502
503 va_start(arg, format);
504 ret = ustream_vprintf(s, format, arg);
505 va_end(arg);
506
507 return ret;
508 }