88f13326a471d58b17b67495fc833da30066fd7d
[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         if (!s->write_error)
334                 ustream_state_change(s);
335         s->write_error = true;
336 }
337
338 bool ustream_write_pending(struct ustream *s)
339 {
340         struct ustream_buf *buf = s->w.head;
341         int wr = 0, len;
342
343         if (s->write_error)
344                 return false;
345
346         while (buf && s->w.data_bytes) {
347                 struct ustream_buf *next = buf->next;
348                 int maxlen = buf->tail - buf->data;
349
350                 len = s->write(s, buf->data, maxlen, !!buf->next);
351                 if (len < 0) {
352                         ustream_write_error(s);
353                         break;
354                 }
355
356                 if (len == 0)
357                         break;
358
359                 wr += len;
360                 s->w.data_bytes -= len;
361                 if (len < maxlen) {
362                         buf->data += len;
363                         break;
364                 }
365
366                 ustream_free_buf(&s->w, buf);
367                 buf = next;
368         }
369
370         if (s->notify_write)
371                 s->notify_write(s, wr);
372
373         if (s->eof && wr && !s->w.data_bytes)
374                 ustream_state_change(s);
375
376         return !s->w.data_bytes;
377 }
378
379 static int ustream_write_buffered(struct ustream *s, const char *data, int len, int wr)
380 {
381         struct ustream_buf_list *l = &s->w;
382         struct ustream_buf *buf;
383         int maxlen;
384
385         while (len) {
386                 if (!ustream_prepare_buf(s, &s->w, len))
387                         break;
388
389                 buf = l->data_tail;
390
391                 maxlen = buf->end - buf->tail;
392                 if (maxlen > len)
393                         maxlen = len;
394
395                 memcpy(buf->tail, data, maxlen);
396                 buf->tail += maxlen;
397                 data += maxlen;
398                 len -= maxlen;
399                 wr += maxlen;
400                 l->data_bytes += maxlen;
401         }
402
403         return wr;
404 }
405
406 int ustream_write(struct ustream *s, const char *data, int len, bool more)
407 {
408         struct ustream_buf_list *l = &s->w;
409         int wr = 0;
410
411         if (s->write_error)
412                 return 0;
413
414         if (!l->data_bytes) {
415                 wr = s->write(s, data, len, more);
416                 if (wr == len)
417                         return wr;
418
419                 if (wr < 0) {
420                         ustream_write_error(s);
421                         return wr;
422                 }
423
424                 data += wr;
425                 len -= wr;
426         }
427
428         return ustream_write_buffered(s, data, len, wr);
429 }
430
431 #define MAX_STACK_BUFLEN        256
432
433 int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
434 {
435         struct ustream_buf_list *l = &s->w;
436         char *buf;
437         va_list arg2;
438         int wr, maxlen, buflen;
439
440         if (s->write_error)
441                 return 0;
442
443         if (!l->data_bytes) {
444                 buf = alloca(MAX_STACK_BUFLEN);
445                 va_copy(arg2, arg);
446                 maxlen = vsnprintf(buf, MAX_STACK_BUFLEN, format, arg2);
447                 va_end(arg2);
448                 if (maxlen < MAX_STACK_BUFLEN) {
449                         wr = s->write(s, buf, maxlen, false);
450                         if (wr < 0) {
451                                 ustream_write_error(s);
452                                 return wr;
453                         }
454                         if (wr == maxlen)
455                                 return wr;
456
457                         buf += wr;
458                         maxlen -= wr;
459                         return ustream_write_buffered(s, buf, maxlen, wr);
460                 } else {
461                         buf = malloc(maxlen + 1);
462                         wr = vsnprintf(buf, maxlen + 1, format, arg);
463                         wr = ustream_write(s, buf, wr, false);
464                         free(buf);
465                         return wr;
466                 }
467         }
468
469         if (!ustream_prepare_buf(s, l, 1))
470                 return 0;
471
472         buf = l->data_tail->tail;
473         buflen = l->data_tail->end - buf;
474
475         va_copy(arg2, arg);
476         maxlen = vsnprintf(buf, buflen, format, arg2);
477         va_end(arg2);
478
479         wr = maxlen;
480         if (wr >= buflen)
481                 wr = buflen - 1;
482
483         l->data_tail->tail += wr;
484         l->data_bytes += wr;
485         if (maxlen < buflen)
486                 return wr;
487
488         buf = malloc(maxlen + 1);
489         maxlen = vsnprintf(buf, maxlen + 1, format, arg);
490         wr = ustream_write_buffered(s, buf + wr, maxlen - wr, wr);
491         free(buf);
492
493         return wr;
494 }
495
496 int ustream_printf(struct ustream *s, const char *format, ...)
497 {
498         va_list arg;
499         int ret;
500
501         if (s->write_error)
502                 return 0;
503
504         va_start(arg, format);
505         ret = ustream_vprintf(s, format, arg);
506         va_end(arg);
507
508         return ret;
509 }