• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/libubox/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 <limits.h>
 20 #include <stdlib.h>
 21 #include <string.h>
 22 #include <unistd.h>
 23 #include <stdio.h>
 24 #include <stdarg.h>
 25 
 26 #include "ustream.h"
 27 
 28 #define CB_PENDING_READ (1 << 0)
 29 
 30 static void ustream_init_buf(struct ustream_buf *buf, int len)
 31 {
 32         if (!len)
 33                 abort();
 34 
 35         memset(buf, 0, sizeof(*buf));
 36         buf->data = buf->tail = buf->head;
 37         buf->end = buf->head + len;
 38         *buf->head = 0;
 39 }
 40 
 41 static void ustream_add_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
 42 {
 43         l->buffers++;
 44         if (!l->tail)
 45                 l->head = buf;
 46         else
 47                 l->tail->next = buf;
 48 
 49         buf->next = NULL;
 50         l->tail = buf;
 51         if (!l->data_tail)
 52                 l->data_tail = l->head;
 53 }
 54 
 55 static bool ustream_can_alloc(struct ustream_buf_list *l)
 56 {
 57         if (l->max_buffers <= 0)
 58                 return true;
 59 
 60         return (l->buffers < l->max_buffers);
 61 }
 62 
 63 static int ustream_alloc_default(struct ustream *s, struct ustream_buf_list *l)
 64 {
 65         struct ustream_buf *buf;
 66 
 67         if (!ustream_can_alloc(l))
 68                 return -1;
 69 
 70         buf = malloc(sizeof(*buf) + l->buffer_len + s->string_data);
 71         if (!buf)
 72                 return -1;
 73 
 74         ustream_init_buf(buf, l->buffer_len);
 75         ustream_add_buf(l, buf);
 76 
 77         return 0;
 78 }
 79 
 80 static void ustream_free_buffers(struct ustream_buf_list *l)
 81 {
 82         struct ustream_buf *buf = l->head;
 83 
 84         while (buf) {
 85                 struct ustream_buf *next = buf->next;
 86 
 87                 free(buf);
 88                 buf = next;
 89         }
 90         l->head = NULL;
 91         l->tail = NULL;
 92         l->data_tail = NULL;
 93         l->data_bytes = 0;
 94         l->buffers = 0;
 95 }
 96 
 97 void ustream_free_guard_set(struct ustream *s, struct ustream_free_guard *g)
 98 {
 99         g->freed = false;
100         g->prev = s->free_flag;
101         s->free_flag = &g->freed;
102 }
103 
104 bool ustream_free_guard_check(struct ustream *s, struct ustream_free_guard *g)
105 {
106         if (g->freed) {
107                 if (g->prev)
108                         *g->prev = true;
109                 return true;
110         }
111 
112         s->free_flag = g->prev;
113         return false;
114 }
115 
116 void ustream_free(struct ustream *s)
117 {
118         if (s->free_flag)
119                 *s->free_flag = true;
120 
121         if (s->free)
122                 s->free(s);
123 
124         uloop_timeout_cancel(&s->state_change);
125         ustream_free_buffers(&s->r);
126         ustream_free_buffers(&s->w);
127 }
128 
129 static void ustream_state_change_cb(struct uloop_timeout *t)
130 {
131         struct ustream *s = container_of(t, struct ustream, state_change);
132 
133         if (s->write_error)
134                 ustream_free_buffers(&s->w);
135         if (s->notify_state)
136                 s->notify_state(s);
137 }
138 
139 void ustream_init_defaults(struct ustream *s)
140 {
141 #define DEFAULT_SET(_f, _default)       \
142         do {                            \
143                 if (!_f)                \
144                         _f = _default;  \
145         } while(0)
146 
147         DEFAULT_SET(s->r.alloc, ustream_alloc_default);
148         DEFAULT_SET(s->w.alloc, ustream_alloc_default);
149 
150         DEFAULT_SET(s->r.min_buffers, 1);
151         DEFAULT_SET(s->r.max_buffers, 1);
152         DEFAULT_SET(s->r.buffer_len, 4096);
153 
154         DEFAULT_SET(s->w.min_buffers, 2);
155         DEFAULT_SET(s->w.max_buffers, -1);
156         DEFAULT_SET(s->w.buffer_len, 256);
157 
158 #undef DEFAULT_SET
159 
160         s->state_change.cb = ustream_state_change_cb;
161         s->write_error = false;
162         s->eof = false;
163         s->free_flag = NULL;
164         s->read_blocked = 0;
165 
166         s->r.buffers = 0;
167         s->r.data_bytes = 0;
168 
169         s->w.buffers = 0;
170         s->w.data_bytes = 0;
171 }
172 
173 static bool ustream_should_move(struct ustream_buf_list *l, struct ustream_buf *buf, int len)
174 {
175         int maxlen;
176         int offset;
177 
178         /* nothing to squeeze */
179         if (buf->data == buf->head)
180                 return false;
181 
182         maxlen = buf->end - buf->head;
183         offset = buf->data - buf->head;
184 
185         /* less than half is available */
186         if (offset > maxlen / 2)
187                 return true;
188 
189         /* less than 32 bytes data but takes more than 1/4 space */
190         if (buf->tail - buf->data < 32 && offset > maxlen / 4)
191                 return true;
192 
193         /* more buf is already in list or can be allocated */
194         if (buf != l->tail || ustream_can_alloc(l))
195                 return false;
196 
197         /* no need to move if len is available at the tail */
198         return (buf->end - buf->tail < len);
199 }
200 
201 static void ustream_free_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
202 {
203         if (buf == l->head)
204                 l->head = buf->next;
205 
206         if (buf == l->data_tail)
207                 l->data_tail = buf->next;
208 
209         if (buf == l->tail)
210                 l->tail = NULL;
211 
212         if (--l->buffers >= l->min_buffers) {
213                 free(buf);
214                 return;
215         }
216 
217         /* recycle */
218         ustream_init_buf(buf, buf->end - buf->head);
219         ustream_add_buf(l, buf);
220 }
221 
222 static void __ustream_set_read_blocked(struct ustream *s, unsigned char val)
223 {
224         bool changed = !!s->read_blocked != !!val;
225 
226         s->read_blocked = val;
227         if (changed)
228                 s->set_read_blocked(s);
229 }
230 
231 void ustream_set_read_blocked(struct ustream *s, bool set)
232 {
233         unsigned char val = s->read_blocked & ~READ_BLOCKED_USER;
234 
235         if (set)
236                 val |= READ_BLOCKED_USER;
237 
238         __ustream_set_read_blocked(s, val);
239 }
240 
241 void ustream_consume(struct ustream *s, int len)
242 {
243         struct ustream_buf *buf = s->r.head;
244 
245         if (!len)
246                 return;
247 
248         s->r.data_bytes -= len;
249         if (s->r.data_bytes < 0)
250                 abort();
251 
252         do {
253                 struct ustream_buf *next = buf->next;
254                 int buf_len = buf->tail - buf->data;
255 
256                 if (len < buf_len) {
257                         buf->data += len;
258                         break;
259                 }
260 
261                 len -= buf_len;
262                 ustream_free_buf(&s->r, buf);
263                 buf = next;
264         } while(len);
265 
266         __ustream_set_read_blocked(s, s->read_blocked & ~READ_BLOCKED_FULL);
267 }
268 
269 static void ustream_fixup_string(struct ustream *s, struct ustream_buf *buf)
270 {
271         if (!s->string_data)
272                 return;
273 
274         *buf->tail = 0;
275 }
276 
277 static bool ustream_prepare_buf(struct ustream *s, struct ustream_buf_list *l, int len)
278 {
279         struct ustream_buf *buf;
280 
281         buf = l->data_tail;
282         if (buf) {
283                 if (ustream_should_move(l, buf, len)) {
284                         int len = buf->tail - buf->data;
285 
286                         memmove(buf->head, buf->data, len);
287                         buf->data = buf->head;
288                         buf->tail = buf->data + len;
289 
290                         if (l == &s->r)
291                                 ustream_fixup_string(s, buf);
292                 }
293                 /* some chunks available at the tail */
294                 if (buf->tail != buf->end)
295                         return true;
296                 /* next buf available */
297                 if (buf->next) {
298                         l->data_tail = buf->next;
299                         return true;
300                 }
301         }
302 
303         if (!ustream_can_alloc(l))
304                 return false;
305 
306         if (l->alloc(s, l) < 0)
307                 return false;
308 
309         l->data_tail = l->tail;
310         return true;
311 }
312 
313 char *ustream_reserve(struct ustream *s, int len, int *maxlen)
314 {
315         struct ustream_buf *buf;
316 
317         if (!ustream_prepare_buf(s, &s->r, len)) {
318                 __ustream_set_read_blocked(s, s->read_blocked | READ_BLOCKED_FULL);
319                 *maxlen = 0;
320                 return NULL;
321         }
322 
323         buf = s->r.data_tail;
324         *maxlen = buf->end - buf->tail;
325         return buf->tail;
326 }
327 
328 void ustream_fill_read(struct ustream *s, int len)
329 {
330         struct ustream_buf *buf = s->r.data_tail;
331         int maxlen;
332 
333         s->r.data_bytes += len;
334         do {
335                 if (!buf)
336                         abort();
337 
338                 maxlen = buf->end - buf->tail;
339                 if (len < maxlen)
340                         maxlen = len;
341 
342                 len -= maxlen;
343                 buf->tail += maxlen;
344                 ustream_fixup_string(s, buf);
345 
346                 s->r.data_tail = buf;
347                 buf = buf->next;
348         } while (len);
349 
350         if (s->notify_read) {
351                 struct ustream_free_guard guard;
352 
353                 if (s->pending_cb & CB_PENDING_READ)
354                         return;
355 
356                 s->pending_cb |= CB_PENDING_READ;
357                 ustream_free_guard_set(s, &guard);
358                 s->notify_read(s, s->r.data_bytes);
359                 if (ustream_free_guard_check(s, &guard))
360                         return;
361 
362                 s->pending_cb &= ~CB_PENDING_READ;
363         }
364 }
365 
366 char *ustream_get_read_buf(struct ustream *s, int *buflen)
367 {
368         char *data = NULL;
369         int len = 0;
370 
371         if (s->r.head) {
372                 len = s->r.head->tail - s->r.head->data;
373                 if (len > 0)
374                         data = s->r.head->data;
375         }
376 
377         if (buflen)
378                 *buflen = len;
379 
380         return data;
381 }
382 
383 int ustream_read(struct ustream *s, char *buf, int buflen)
384 {
385         char *chunk;
386         int chunk_len;
387         int len = 0;
388 
389         do {
390                 chunk = ustream_get_read_buf(s, &chunk_len);
391                 if (!chunk)
392                         break;
393                 if (chunk_len > buflen - len)
394                         chunk_len = buflen - len;
395                 memcpy(buf + len, chunk, chunk_len);
396                 ustream_consume(s, chunk_len);
397                 len += chunk_len;
398         } while (len < buflen);
399 
400         return len;
401 }
402 
403 static void ustream_write_error(struct ustream *s)
404 {
405         if (!s->write_error)
406                 ustream_state_change(s);
407         s->write_error = true;
408 }
409 
410 bool ustream_write_pending(struct ustream *s)
411 {
412         struct ustream_buf *buf = s->w.head;
413         int wr = 0, len;
414 
415         if (s->write_error)
416                 return false;
417 
418         while (buf && s->w.data_bytes) {
419                 struct ustream_buf *next = buf->next;
420                 int maxlen = buf->tail - buf->data;
421 
422                 len = s->write(s, buf->data, maxlen, !!buf->next);
423                 if (len < 0) {
424                         ustream_write_error(s);
425                         break;
426                 }
427 
428                 if (len == 0)
429                         break;
430 
431                 wr += len;
432                 s->w.data_bytes -= len;
433                 if (len < maxlen) {
434                         buf->data += len;
435                         break;
436                 }
437 
438                 ustream_free_buf(&s->w, buf);
439                 buf = next;
440         }
441 
442         if (s->notify_write) {
443                 struct ustream_free_guard guard;
444 
445                 ustream_free_guard_set(s, &guard);
446                 s->notify_write(s, wr);
447                 if (ustream_free_guard_check(s, &guard))
448                         return true;
449         }
450 
451         if (s->eof && wr && !s->w.data_bytes)
452                 ustream_state_change(s);
453 
454         return !s->w.data_bytes;
455 }
456 
457 static int ustream_write_buffered(struct ustream *s, const char *data, int len, int wr)
458 {
459         struct ustream_buf_list *l = &s->w;
460         struct ustream_buf *buf;
461         int maxlen;
462 
463         while (len) {
464                 if (!ustream_prepare_buf(s, &s->w, len))
465                         break;
466 
467                 buf = l->data_tail;
468 
469                 maxlen = buf->end - buf->tail;
470                 if (maxlen > len)
471                         maxlen = len;
472 
473                 memcpy(buf->tail, data, maxlen);
474                 buf->tail += maxlen;
475                 data += maxlen;
476                 len -= maxlen;
477                 wr += maxlen;
478                 l->data_bytes += maxlen;
479         }
480 
481         return wr;
482 }
483 
484 int ustream_write(struct ustream *s, const char *data, int len, bool more)
485 {
486         struct ustream_buf_list *l = &s->w;
487         int wr = 0;
488 
489         if (s->write_error)
490                 return 0;
491 
492         if (!l->data_bytes) {
493                 wr = s->write(s, data, len, more);
494                 if (wr == len)
495                         return wr;
496 
497                 if (wr < 0) {
498                         ustream_write_error(s);
499                         return wr;
500                 }
501 
502                 data += wr;
503                 len -= wr;
504         }
505 
506         return ustream_write_buffered(s, data, len, wr);
507 }
508 
509 #define MAX_STACK_BUFLEN        256
510 
511 int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
512 {
513         struct ustream_buf_list *l = &s->w;
514         char *buf;
515         va_list arg2;
516         int wr, maxlen, buflen;
517 
518         if (s->write_error)
519                 return 0;
520 
521         if (!l->data_bytes) {
522                 buf = alloca(MAX_STACK_BUFLEN);
523                 va_copy(arg2, arg);
524                 maxlen = vsnprintf(buf, MAX_STACK_BUFLEN, format, arg2);
525                 va_end(arg2);
526                 if (maxlen < MAX_STACK_BUFLEN) {
527                         wr = s->write(s, buf, maxlen, false);
528                         if (wr < 0) {
529                                 ustream_write_error(s);
530                                 return wr;
531                         }
532                         if (wr == maxlen)
533                                 return wr;
534 
535                         buf += wr;
536                         maxlen -= wr;
537                         return ustream_write_buffered(s, buf, maxlen, wr);
538                 } else {
539                         if (maxlen == INT_MAX)
540                                 return 0;
541                         buf = malloc(maxlen + 1);
542                         if (!buf)
543                                 return 0;
544                         wr = vsnprintf(buf, maxlen + 1, format, arg);
545                         wr = ustream_write(s, buf, wr, false);
546                         free(buf);
547                         return wr;
548                 }
549         }
550 
551         if (!ustream_prepare_buf(s, l, 1))
552                 return 0;
553 
554         buf = l->data_tail->tail;
555         buflen = l->data_tail->end - buf;
556 
557         va_copy(arg2, arg);
558         maxlen = vsnprintf(buf, buflen, format, arg2);
559         va_end(arg2);
560 
561         wr = maxlen;
562         if (wr >= buflen)
563                 wr = buflen - 1;
564 
565         l->data_tail->tail += wr;
566         l->data_bytes += wr;
567         if (maxlen < buflen)
568                 return wr;
569 
570         if (maxlen == INT_MAX)
571                 return wr;
572         buf = malloc(maxlen + 1);
573         if (!buf)
574                 return wr;
575         maxlen = vsnprintf(buf, maxlen + 1, format, arg);
576         wr = ustream_write_buffered(s, buf + wr, maxlen - wr, wr);
577         free(buf);
578 
579         return wr;
580 }
581 
582 int ustream_printf(struct ustream *s, const char *format, ...)
583 {
584         va_list arg;
585         int ret;
586 
587         if (s->write_error)
588                 return 0;
589 
590         va_start(arg, format);
591         ret = ustream_vprintf(s, format, arg);
592         va_end(arg);
593 
594         return ret;
595 }
596 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt