ustream.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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. static void ustream_init_buf(struct ustream_buf *buf, int len)
  25. {
  26. if (!len)
  27. abort();
  28. memset(buf, 0, sizeof(*buf));
  29. buf->data = buf->tail = buf->head;
  30. buf->end = buf->head + len;
  31. *buf->head = 0;
  32. }
  33. static void ustream_add_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
  34. {
  35. l->buffers++;
  36. if (!l->tail)
  37. l->head = buf;
  38. else
  39. l->tail->next = buf;
  40. buf->next = NULL;
  41. l->tail = buf;
  42. if (!l->data_tail)
  43. l->data_tail = l->head;
  44. }
  45. static bool ustream_can_alloc(struct ustream_buf_list *l)
  46. {
  47. if (l->max_buffers <= 0)
  48. return true;
  49. return (l->buffers < l->max_buffers);
  50. }
  51. static int ustream_alloc_default(struct ustream *s, struct ustream_buf_list *l)
  52. {
  53. struct ustream_buf *buf;
  54. if (!ustream_can_alloc(l))
  55. return -1;
  56. buf = malloc(sizeof(*buf) + l->buffer_len + s->string_data);
  57. if (!buf)
  58. return -1;
  59. ustream_init_buf(buf, l->buffer_len);
  60. ustream_add_buf(l, buf);
  61. return 0;
  62. }
  63. static void ustream_free_buffers(struct ustream_buf_list *l)
  64. {
  65. struct ustream_buf *buf = l->head;
  66. while (buf) {
  67. struct ustream_buf *next = buf->next;
  68. free(buf);
  69. buf = next;
  70. }
  71. l->head = NULL;
  72. l->tail = NULL;
  73. l->data_tail = NULL;
  74. }
  75. void ustream_free(struct ustream *s)
  76. {
  77. if (s->free)
  78. s->free(s);
  79. uloop_timeout_cancel(&s->state_change);
  80. ustream_free_buffers(&s->r);
  81. ustream_free_buffers(&s->w);
  82. }
  83. static void ustream_state_change_cb(struct uloop_timeout *t)
  84. {
  85. struct ustream *s = container_of(t, struct ustream, state_change);
  86. if (s->write_error)
  87. ustream_free_buffers(&s->w);
  88. if (s->notify_state)
  89. s->notify_state(s);
  90. }
  91. void ustream_init_defaults(struct ustream *s)
  92. {
  93. #define DEFAULT_SET(_f, _default) \
  94. do { \
  95. if (!_f) \
  96. _f = _default; \
  97. } while(0)
  98. DEFAULT_SET(s->r.alloc, ustream_alloc_default);
  99. DEFAULT_SET(s->w.alloc, ustream_alloc_default);
  100. DEFAULT_SET(s->r.min_buffers, 1);
  101. DEFAULT_SET(s->r.max_buffers, 1);
  102. DEFAULT_SET(s->r.buffer_len, 4096);
  103. DEFAULT_SET(s->w.min_buffers, 2);
  104. DEFAULT_SET(s->w.max_buffers, -1);
  105. DEFAULT_SET(s->w.buffer_len, 256);
  106. #undef DEFAULT_SET
  107. s->state_change.cb = ustream_state_change_cb;
  108. s->write_error = false;
  109. s->eof = false;
  110. s->eof_write_done = 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 n = len;
  240. int maxlen;
  241. s->r.data_bytes += len;
  242. do {
  243. if (!buf)
  244. abort();
  245. maxlen = buf->end - buf->tail;
  246. if (len < maxlen)
  247. maxlen = len;
  248. len -= maxlen;
  249. buf->tail += maxlen;
  250. ustream_fixup_string(s, buf);
  251. s->r.data_tail = buf;
  252. buf = buf->next;
  253. } while (len);
  254. if (s->notify_read)
  255. s->notify_read(s, n);
  256. }
  257. char *ustream_get_read_buf(struct ustream *s, int *buflen)
  258. {
  259. char *data = NULL;
  260. int len = 0;
  261. if (s->r.head) {
  262. len = s->r.head->tail - s->r.head->data;
  263. if (len > 0)
  264. data = s->r.head->data;
  265. }
  266. if (buflen)
  267. *buflen = len;
  268. return data;
  269. }
  270. int ustream_read(struct ustream *s, char *buf, int buflen)
  271. {
  272. char *chunk;
  273. int chunk_len;
  274. int len = 0;
  275. do {
  276. chunk = ustream_get_read_buf(s, &chunk_len);
  277. if (!chunk)
  278. break;
  279. if (chunk_len > buflen - len)
  280. chunk_len = buflen - len;
  281. memcpy(buf + len, chunk, chunk_len);
  282. ustream_consume(s, chunk_len);
  283. len += chunk_len;
  284. } while (len < buflen);
  285. return len;
  286. }
  287. static void ustream_write_error(struct ustream *s)
  288. {
  289. if (!s->write_error)
  290. ustream_state_change(s);
  291. s->write_error = true;
  292. }
  293. bool ustream_write_pending(struct ustream *s)
  294. {
  295. struct ustream_buf *buf = s->w.head;
  296. int wr = 0, len;
  297. if (s->write_error)
  298. return false;
  299. while (buf && s->w.data_bytes) {
  300. struct ustream_buf *next = buf->next;
  301. int maxlen = buf->tail - buf->data;
  302. len = s->write(s, buf->data, maxlen, !!buf->next);
  303. if (len < 0) {
  304. ustream_write_error(s);
  305. break;
  306. }
  307. if (len == 0)
  308. break;
  309. wr += len;
  310. s->w.data_bytes -= len;
  311. if (len < maxlen) {
  312. buf->data += len;
  313. break;
  314. }
  315. ustream_free_buf(&s->w, buf);
  316. buf = next;
  317. }
  318. if (s->notify_write)
  319. s->notify_write(s, wr);
  320. if (s->eof && wr && !s->w.data_bytes)
  321. ustream_state_change(s);
  322. return !s->w.data_bytes;
  323. }
  324. static int ustream_write_buffered(struct ustream *s, const char *data, int len, int wr)
  325. {
  326. struct ustream_buf_list *l = &s->w;
  327. struct ustream_buf *buf;
  328. int maxlen;
  329. while (len) {
  330. if (!ustream_prepare_buf(s, &s->w, len))
  331. break;
  332. buf = l->data_tail;
  333. maxlen = buf->end - buf->tail;
  334. if (maxlen > len)
  335. maxlen = len;
  336. memcpy(buf->tail, data, maxlen);
  337. buf->tail += maxlen;
  338. data += maxlen;
  339. len -= maxlen;
  340. wr += maxlen;
  341. l->data_bytes += maxlen;
  342. }
  343. return wr;
  344. }
  345. int ustream_write(struct ustream *s, const char *data, int len, bool more)
  346. {
  347. struct ustream_buf_list *l = &s->w;
  348. int wr = 0;
  349. if (s->write_error)
  350. return 0;
  351. if (!l->data_bytes) {
  352. wr = s->write(s, data, len, more);
  353. if (wr == len)
  354. return wr;
  355. if (wr < 0) {
  356. ustream_write_error(s);
  357. return wr;
  358. }
  359. data += wr;
  360. len -= wr;
  361. }
  362. return ustream_write_buffered(s, data, len, wr);
  363. }
  364. #define MAX_STACK_BUFLEN 256
  365. int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
  366. {
  367. struct ustream_buf_list *l = &s->w;
  368. char *buf;
  369. va_list arg2;
  370. int wr, maxlen, buflen;
  371. if (s->write_error)
  372. return 0;
  373. if (!l->data_bytes) {
  374. buf = alloca(MAX_STACK_BUFLEN);
  375. va_copy(arg2, arg);
  376. maxlen = vsnprintf(buf, MAX_STACK_BUFLEN, format, arg2);
  377. va_end(arg2);
  378. if (maxlen < MAX_STACK_BUFLEN) {
  379. wr = s->write(s, buf, maxlen, false);
  380. if (wr < 0) {
  381. ustream_write_error(s);
  382. return wr;
  383. }
  384. if (wr == maxlen)
  385. return wr;
  386. buf += wr;
  387. maxlen -= wr;
  388. return ustream_write_buffered(s, buf, maxlen, wr);
  389. } else {
  390. buf = malloc(maxlen + 1);
  391. if (!buf)
  392. return 0;
  393. wr = vsnprintf(buf, maxlen + 1, format, arg);
  394. wr = ustream_write(s, buf, wr, false);
  395. free(buf);
  396. return wr;
  397. }
  398. }
  399. if (!ustream_prepare_buf(s, l, 1))
  400. return 0;
  401. buf = l->data_tail->tail;
  402. buflen = l->data_tail->end - buf;
  403. va_copy(arg2, arg);
  404. maxlen = vsnprintf(buf, buflen, format, arg2);
  405. va_end(arg2);
  406. wr = maxlen;
  407. if (wr >= buflen)
  408. wr = buflen - 1;
  409. l->data_tail->tail += wr;
  410. l->data_bytes += wr;
  411. if (maxlen < buflen)
  412. return wr;
  413. buf = malloc(maxlen + 1);
  414. if (!buf)
  415. return wr;
  416. maxlen = vsnprintf(buf, maxlen + 1, format, arg);
  417. wr = ustream_write_buffered(s, buf + wr, maxlen - wr, wr);
  418. free(buf);
  419. return wr;
  420. }
  421. int ustream_printf(struct ustream *s, const char *format, ...)
  422. {
  423. va_list arg;
  424. int ret;
  425. if (s->write_error)
  426. return 0;
  427. va_start(arg, format);
  428. ret = ustream_vprintf(s, format, arg);
  429. va_end(arg);
  430. return ret;
  431. }