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