ustream.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <stdio.h>
  22. #include <stdarg.h>
  23. #include "ustream.h"
  24. #define CB_PENDING_READ (1 << 0)
  25. static void ustream_init_buf(struct ustream_buf *buf, int len)
  26. {
  27. if (!len)
  28. abort();
  29. memset(buf, 0, sizeof(*buf));
  30. buf->data = buf->tail = buf->head;
  31. buf->end = buf->head + len;
  32. *buf->head = 0;
  33. }
  34. static void ustream_add_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
  35. {
  36. l->buffers++;
  37. if (!l->tail)
  38. l->head = buf;
  39. else
  40. l->tail->next = buf;
  41. buf->next = NULL;
  42. l->tail = buf;
  43. if (!l->data_tail)
  44. l->data_tail = l->head;
  45. }
  46. static bool ustream_can_alloc(struct ustream_buf_list *l)
  47. {
  48. if (l->max_buffers <= 0)
  49. return true;
  50. return (l->buffers < l->max_buffers);
  51. }
  52. static int ustream_alloc_default(struct ustream *s, struct ustream_buf_list *l)
  53. {
  54. struct ustream_buf *buf;
  55. if (!ustream_can_alloc(l))
  56. return -1;
  57. buf = malloc(sizeof(*buf) + l->buffer_len + s->string_data);
  58. if (!buf)
  59. return -1;
  60. ustream_init_buf(buf, l->buffer_len);
  61. ustream_add_buf(l, buf);
  62. return 0;
  63. }
  64. static void ustream_free_buffers(struct ustream_buf_list *l)
  65. {
  66. struct ustream_buf *buf = l->head;
  67. while (buf) {
  68. struct ustream_buf *next = buf->next;
  69. free(buf);
  70. buf = next;
  71. }
  72. l->head = NULL;
  73. l->tail = NULL;
  74. l->data_tail = NULL;
  75. }
  76. void ustream_free(struct ustream *s)
  77. {
  78. if (s->free)
  79. s->free(s);
  80. uloop_timeout_cancel(&s->state_change);
  81. ustream_free_buffers(&s->r);
  82. ustream_free_buffers(&s->w);
  83. }
  84. static void ustream_state_change_cb(struct uloop_timeout *t)
  85. {
  86. struct ustream *s = container_of(t, struct ustream, state_change);
  87. if (s->write_error)
  88. ustream_free_buffers(&s->w);
  89. if (s->notify_state)
  90. s->notify_state(s);
  91. }
  92. void ustream_init_defaults(struct ustream *s)
  93. {
  94. #define DEFAULT_SET(_f, _default) \
  95. do { \
  96. if (!_f) \
  97. _f = _default; \
  98. } while(0)
  99. DEFAULT_SET(s->r.alloc, ustream_alloc_default);
  100. DEFAULT_SET(s->w.alloc, ustream_alloc_default);
  101. DEFAULT_SET(s->r.min_buffers, 1);
  102. DEFAULT_SET(s->r.max_buffers, 1);
  103. DEFAULT_SET(s->r.buffer_len, 4096);
  104. DEFAULT_SET(s->w.min_buffers, 2);
  105. DEFAULT_SET(s->w.max_buffers, -1);
  106. DEFAULT_SET(s->w.buffer_len, 256);
  107. #undef DEFAULT_SET
  108. s->state_change.cb = ustream_state_change_cb;
  109. s->write_error = false;
  110. s->eof = false;
  111. s->read_blocked = 0;
  112. s->r.buffers = 0;
  113. s->r.data_bytes = 0;
  114. s->w.buffers = 0;
  115. s->w.data_bytes = 0;
  116. }
  117. static bool ustream_should_move(struct ustream_buf_list *l, struct ustream_buf *buf, int len)
  118. {
  119. int maxlen;
  120. int offset;
  121. /* nothing to squeeze */
  122. if (buf->data == buf->head)
  123. return false;
  124. maxlen = buf->end - buf->head;
  125. offset = buf->data - buf->head;
  126. /* less than half is available */
  127. if (offset > maxlen / 2)
  128. return true;
  129. /* less than 32 bytes data but takes more than 1/4 space */
  130. if (buf->tail - buf->data < 32 && offset > maxlen / 4)
  131. return true;
  132. /* more buf is already in list or can be allocated */
  133. if (buf != l->tail || ustream_can_alloc(l))
  134. return false;
  135. /* no need to move if len is available at the tail */
  136. return (buf->end - buf->tail < len);
  137. }
  138. static void ustream_free_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
  139. {
  140. if (buf == l->head)
  141. l->head = buf->next;
  142. if (buf == l->data_tail)
  143. l->data_tail = buf->next;
  144. if (buf == l->tail)
  145. l->tail = NULL;
  146. if (--l->buffers >= l->min_buffers) {
  147. free(buf);
  148. return;
  149. }
  150. /* recycle */
  151. ustream_init_buf(buf, buf->end - buf->head);
  152. ustream_add_buf(l, buf);
  153. }
  154. static void __ustream_set_read_blocked(struct ustream *s, unsigned char val)
  155. {
  156. bool changed = !!s->read_blocked != !!val;
  157. s->read_blocked = val;
  158. if (changed)
  159. s->set_read_blocked(s);
  160. }
  161. void ustream_set_read_blocked(struct ustream *s, bool set)
  162. {
  163. unsigned char val = s->read_blocked & ~READ_BLOCKED_USER;
  164. if (set)
  165. val |= READ_BLOCKED_USER;
  166. __ustream_set_read_blocked(s, val);
  167. }
  168. void ustream_consume(struct ustream *s, int len)
  169. {
  170. struct ustream_buf *buf = s->r.head;
  171. if (!len)
  172. return;
  173. s->r.data_bytes -= len;
  174. if (s->r.data_bytes < 0)
  175. abort();
  176. do {
  177. struct ustream_buf *next = buf->next;
  178. int buf_len = buf->tail - buf->data;
  179. if (len < buf_len) {
  180. buf->data += len;
  181. break;
  182. }
  183. len -= buf_len;
  184. ustream_free_buf(&s->r, buf);
  185. buf = next;
  186. } while(len);
  187. __ustream_set_read_blocked(s, s->read_blocked & ~READ_BLOCKED_FULL);
  188. }
  189. static void ustream_fixup_string(struct ustream *s, struct ustream_buf *buf)
  190. {
  191. if (!s->string_data)
  192. return;
  193. *buf->tail = 0;
  194. }
  195. static bool ustream_prepare_buf(struct ustream *s, struct ustream_buf_list *l, int len)
  196. {
  197. struct ustream_buf *buf;
  198. buf = l->data_tail;
  199. if (buf) {
  200. if (ustream_should_move(l, buf, len)) {
  201. int len = buf->tail - buf->data;
  202. memmove(buf->head, buf->data, len);
  203. buf->data = buf->head;
  204. buf->tail = buf->data + len;
  205. if (l == &s->r)
  206. ustream_fixup_string(s, buf);
  207. }
  208. /* some chunks available at the tail */
  209. if (buf->tail != buf->end)
  210. return true;
  211. /* next buf available */
  212. if (buf->next) {
  213. l->data_tail = buf->next;
  214. return true;
  215. }
  216. }
  217. if (!ustream_can_alloc(l))
  218. return false;
  219. if (l->alloc(s, l) < 0)
  220. return false;
  221. l->data_tail = l->tail;
  222. return true;
  223. }
  224. char *ustream_reserve(struct ustream *s, int len, int *maxlen)
  225. {
  226. struct ustream_buf *buf;
  227. if (!ustream_prepare_buf(s, &s->r, len)) {
  228. __ustream_set_read_blocked(s, s->read_blocked | READ_BLOCKED_FULL);
  229. *maxlen = 0;
  230. return NULL;
  231. }
  232. buf = s->r.data_tail;
  233. *maxlen = buf->end - buf->tail;
  234. return buf->tail;
  235. }
  236. void ustream_fill_read(struct ustream *s, int len)
  237. {
  238. struct ustream_buf *buf = s->r.data_tail;
  239. int maxlen;
  240. s->r.data_bytes += len;
  241. do {
  242. if (!buf)
  243. abort();
  244. maxlen = buf->end - buf->tail;
  245. if (len < maxlen)
  246. maxlen = len;
  247. len -= maxlen;
  248. buf->tail += maxlen;
  249. ustream_fixup_string(s, buf);
  250. s->r.data_tail = buf;
  251. buf = buf->next;
  252. } while (len);
  253. if (s->notify_read) {
  254. if (s->pending_cb & CB_PENDING_READ)
  255. return;
  256. s->pending_cb |= CB_PENDING_READ;
  257. s->notify_read(s, s->r.data_bytes);
  258. s->pending_cb &= ~CB_PENDING_READ;
  259. }
  260. }
  261. char *ustream_get_read_buf(struct ustream *s, int *buflen)
  262. {
  263. char *data = NULL;
  264. int len = 0;
  265. if (s->r.head) {
  266. len = s->r.head->tail - s->r.head->data;
  267. if (len > 0)
  268. data = s->r.head->data;
  269. }
  270. if (buflen)
  271. *buflen = len;
  272. return data;
  273. }
  274. int ustream_read(struct ustream *s, char *buf, int buflen)
  275. {
  276. char *chunk;
  277. int chunk_len;
  278. int len = 0;
  279. do {
  280. chunk = ustream_get_read_buf(s, &chunk_len);
  281. if (!chunk)
  282. break;
  283. if (chunk_len > buflen - len)
  284. chunk_len = buflen - len;
  285. memcpy(buf + len, chunk, chunk_len);
  286. ustream_consume(s, chunk_len);
  287. len += chunk_len;
  288. } while (len < buflen);
  289. return len;
  290. }
  291. static void ustream_write_error(struct ustream *s)
  292. {
  293. if (!s->write_error)
  294. ustream_state_change(s);
  295. s->write_error = true;
  296. }
  297. bool ustream_write_pending(struct ustream *s)
  298. {
  299. struct ustream_buf *buf = s->w.head;
  300. int wr = 0, len;
  301. if (s->write_error)
  302. return false;
  303. while (buf && s->w.data_bytes) {
  304. struct ustream_buf *next = buf->next;
  305. int maxlen = buf->tail - buf->data;
  306. len = s->write(s, buf->data, maxlen, !!buf->next);
  307. if (len < 0) {
  308. ustream_write_error(s);
  309. break;
  310. }
  311. if (len == 0)
  312. break;
  313. wr += len;
  314. s->w.data_bytes -= len;
  315. if (len < maxlen) {
  316. buf->data += len;
  317. break;
  318. }
  319. ustream_free_buf(&s->w, buf);
  320. buf = next;
  321. }
  322. if (s->notify_write)
  323. s->notify_write(s, wr);
  324. if (s->eof && wr && !s->w.data_bytes)
  325. ustream_state_change(s);
  326. return !s->w.data_bytes;
  327. }
  328. static int ustream_write_buffered(struct ustream *s, const char *data, int len, int wr)
  329. {
  330. struct ustream_buf_list *l = &s->w;
  331. struct ustream_buf *buf;
  332. int maxlen;
  333. while (len) {
  334. if (!ustream_prepare_buf(s, &s->w, len))
  335. break;
  336. buf = l->data_tail;
  337. maxlen = buf->end - buf->tail;
  338. if (maxlen > len)
  339. maxlen = len;
  340. memcpy(buf->tail, data, maxlen);
  341. buf->tail += maxlen;
  342. data += maxlen;
  343. len -= maxlen;
  344. wr += maxlen;
  345. l->data_bytes += maxlen;
  346. }
  347. return wr;
  348. }
  349. int ustream_write(struct ustream *s, const char *data, int len, bool more)
  350. {
  351. struct ustream_buf_list *l = &s->w;
  352. int wr = 0;
  353. if (s->write_error)
  354. return 0;
  355. if (!l->data_bytes) {
  356. wr = s->write(s, data, len, more);
  357. if (wr == len)
  358. return wr;
  359. if (wr < 0) {
  360. ustream_write_error(s);
  361. return wr;
  362. }
  363. data += wr;
  364. len -= wr;
  365. }
  366. return ustream_write_buffered(s, data, len, wr);
  367. }
  368. #define MAX_STACK_BUFLEN 256
  369. int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
  370. {
  371. struct ustream_buf_list *l = &s->w;
  372. char *buf;
  373. va_list arg2;
  374. int wr, maxlen, buflen;
  375. if (s->write_error)
  376. return 0;
  377. if (!l->data_bytes) {
  378. buf = alloca(MAX_STACK_BUFLEN);
  379. va_copy(arg2, arg);
  380. maxlen = vsnprintf(buf, MAX_STACK_BUFLEN, format, arg2);
  381. va_end(arg2);
  382. if (maxlen < MAX_STACK_BUFLEN) {
  383. wr = s->write(s, buf, maxlen, false);
  384. if (wr < 0) {
  385. ustream_write_error(s);
  386. return wr;
  387. }
  388. if (wr == maxlen)
  389. return wr;
  390. buf += wr;
  391. maxlen -= wr;
  392. return ustream_write_buffered(s, buf, maxlen, wr);
  393. } else {
  394. buf = malloc(maxlen + 1);
  395. if (!buf)
  396. return 0;
  397. wr = vsnprintf(buf, maxlen + 1, format, arg);
  398. wr = ustream_write(s, buf, wr, false);
  399. free(buf);
  400. return wr;
  401. }
  402. }
  403. if (!ustream_prepare_buf(s, l, 1))
  404. return 0;
  405. buf = l->data_tail->tail;
  406. buflen = l->data_tail->end - buf;
  407. va_copy(arg2, arg);
  408. maxlen = vsnprintf(buf, buflen, format, arg2);
  409. va_end(arg2);
  410. wr = maxlen;
  411. if (wr >= buflen)
  412. wr = buflen - 1;
  413. l->data_tail->tail += wr;
  414. l->data_bytes += wr;
  415. if (maxlen < buflen)
  416. return wr;
  417. buf = malloc(maxlen + 1);
  418. if (!buf)
  419. return wr;
  420. maxlen = vsnprintf(buf, maxlen + 1, format, arg);
  421. wr = ustream_write_buffered(s, buf + wr, maxlen - wr, wr);
  422. free(buf);
  423. return wr;
  424. }
  425. int ustream_printf(struct ustream *s, const char *format, ...)
  426. {
  427. va_list arg;
  428. int ret;
  429. if (s->write_error)
  430. return 0;
  431. va_start(arg, format);
  432. ret = ustream_vprintf(s, format, arg);
  433. va_end(arg);
  434. return ret;
  435. }