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