buf.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /** @file buf.c
  2. * @brief Dynamic buffer.
  3. *
  4. */
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include "faux/faux.h"
  11. #include "faux/str.h"
  12. #include "faux/buf.h"
  13. // Default chunk size
  14. #define DATA_CHUNK 4096
  15. struct faux_buf_s {
  16. faux_list_t *list; // List of chunks
  17. faux_list_node_t *wchunk; // Chunk to write to
  18. size_t rpos; // Read position within first chunk
  19. size_t wpos; // Write position within wchunk (can be non-last chunk)
  20. size_t chunk_size; // Size of chunk
  21. size_t len; // Whole data length
  22. size_t limit; // Overflow limit
  23. size_t rblocked;
  24. size_t wblocked;
  25. };
  26. /** @brief Create new dynamic buffer object.
  27. *
  28. * @param [in] chunk_size Chunk size. If "0" then default size will be used.
  29. * @return Allocated object or NULL on error.
  30. */
  31. faux_buf_t *faux_buf_new(size_t chunk_size)
  32. {
  33. faux_buf_t *buf = NULL;
  34. buf = faux_zmalloc(sizeof(*buf));
  35. assert(buf);
  36. if (!buf)
  37. return NULL;
  38. // Init
  39. buf->chunk_size = (chunk_size != 0) ? chunk_size : DATA_CHUNK;
  40. buf->limit = FAUX_BUF_UNLIMITED;
  41. buf->list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  42. NULL, NULL, faux_free);
  43. buf->rpos = 0;
  44. buf->wpos = buf->chunk_size;
  45. buf->len = 0;
  46. buf->wchunk = NULL;
  47. buf->rblocked = 0; // Unblocked
  48. buf->wblocked = 0; // Unblocked
  49. return buf;
  50. }
  51. /** @brief Free dynamic buffer object.
  52. *
  53. * @param [in] buf Buffer object.
  54. */
  55. void faux_buf_free(faux_buf_t *buf)
  56. {
  57. if (!buf)
  58. return;
  59. faux_list_free(buf->list);
  60. faux_free(buf);
  61. }
  62. ssize_t faux_buf_len(const faux_buf_t *buf)
  63. {
  64. assert(buf);
  65. if (!buf)
  66. return -1;
  67. return buf->len;
  68. }
  69. static ssize_t faux_buf_chunk_num(const faux_buf_t *buf)
  70. {
  71. assert(buf);
  72. if (!buf)
  73. return -1;
  74. assert(buf->list);
  75. if (!buf->list)
  76. return -1;
  77. return faux_list_len(buf->list);
  78. }
  79. ssize_t faux_buf_limit(const faux_buf_t *buf)
  80. {
  81. assert(buf);
  82. if (!buf)
  83. return -1;
  84. return buf->limit;
  85. }
  86. /** @brief Set size limit.
  87. *
  88. * Read limits define conditions when the read callback will be executed.
  89. * Buffer must contain data amount greater or equal to "min" value. Callback
  90. * will not get data amount greater than "max" value. If min == max then
  91. * callback will be executed with fixed data size. The "max" value can be "0".
  92. * It means indefinite i.e. data transferred to callback can be really large.
  93. *
  94. * @param [in] buf Allocated and initialized buf I/O object.
  95. * @param [in] min Minimal data amount.
  96. * @param [in] max Maximal data amount. The "0" means indefinite.
  97. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  98. */
  99. bool_t faux_buf_set_limit(faux_buf_t *buf, size_t limit)
  100. {
  101. assert(buf);
  102. if (!buf)
  103. return BOOL_FALSE;
  104. buf->limit = limit;
  105. return BOOL_TRUE;
  106. }
  107. /** @brief Get amount of unused space within current data chunk.
  108. *
  109. * Inernal static function.
  110. *
  111. * @param [in] list Internal buffer (list of chunks) to inspect.
  112. * @param [in] pos Current write position within last chunk
  113. * @return Size of unused space or < 0 on error.
  114. */
  115. static ssize_t faux_buf_wavail(faux_buf_t *buf)
  116. {
  117. assert(buf);
  118. if (!buf)
  119. return -1;
  120. if (faux_buf_chunk_num(buf) == 0)
  121. return 0; // Empty list
  122. return (buf->chunk_size - buf->wpos);
  123. }
  124. static ssize_t faux_buf_ravail(faux_buf_t *buf)
  125. {
  126. ssize_t num = 0;
  127. assert(buf);
  128. if (!buf)
  129. return -1;
  130. num = faux_buf_chunk_num(buf);
  131. if (num == 0)
  132. return 0; // Empty list
  133. if (num > 1)
  134. return (buf->chunk_size - buf->rpos);
  135. // Single chunk
  136. return (buf->wpos - buf->rpos);
  137. }
  138. bool_t faux_buf_is_wblocked(const faux_buf_t *buf)
  139. {
  140. assert(buf);
  141. if (!buf)
  142. return BOOL_FALSE;
  143. if (buf->wblocked != 0)
  144. return BOOL_TRUE;
  145. return BOOL_FALSE;
  146. }
  147. bool_t faux_buf_is_rblocked(const faux_buf_t *buf)
  148. {
  149. assert(buf);
  150. if (!buf)
  151. return BOOL_FALSE;
  152. if (buf->rblocked != 0)
  153. return BOOL_TRUE;
  154. return BOOL_FALSE;
  155. }
  156. static faux_list_node_t *faux_buf_alloc_chunk(faux_buf_t *buf)
  157. {
  158. char *chunk = NULL;
  159. assert(buf);
  160. if (!buf)
  161. return NULL;
  162. assert(buf->list);
  163. if (!buf->list)
  164. return NULL;
  165. chunk = faux_malloc(buf->chunk_size);
  166. assert(chunk);
  167. if (!chunk)
  168. return NULL;
  169. return faux_list_add(buf->list, chunk);
  170. }
  171. /*
  172. static bool_t faux_buf_rm_trailing_empty_chunks(faux_buf_t *buf)
  173. {
  174. faux_list_node_t *node = NULL;
  175. assert(buf);
  176. if (!buf)
  177. return BOOL_FALSE;
  178. assert(buf->list);
  179. if (!buf->list)
  180. return BOOL_FALSE;
  181. if (faux_buf_chunk_num(buf) == 0)
  182. return BOOL_TRUE; // Empty list
  183. while ((node = faux_list_tail(buf->list)) != buf->wchunk)
  184. faux_list_del(buf->list, node);
  185. if (buf->wchunk &&
  186. ((buf->wpos == 0) || // Empty chunk
  187. ((faux_list_chunk_num(buf) == 1) && (buf->rpos == buf->wpos)))
  188. ) {
  189. faux_list_del(buf->list, buf->wchunk);
  190. buf->wchunk = NULL;
  191. buf->wpos = buf->chunk_size;
  192. }
  193. return BOOL_TRUE;
  194. }
  195. */
  196. static bool_t faux_buf_will_be_overflow(const faux_buf_t *buf, size_t add_len)
  197. {
  198. assert(buf);
  199. if (!buf)
  200. return BOOL_FALSE;
  201. if (FAUX_BUF_UNLIMITED == buf->limit)
  202. return BOOL_FALSE;
  203. if ((buf->len + add_len) > buf->limit)
  204. return BOOL_TRUE;
  205. return BOOL_FALSE;
  206. }
  207. bool_t faux_buf_is_overflow(const faux_buf_t *buf)
  208. {
  209. return faux_buf_will_be_overflow(buf, 0);
  210. }
  211. /** @brief buf data write.
  212. *
  213. * All given data will be stored to internal buffer (list of data chunks).
  214. * Then function will try to write stored data to file descriptor in
  215. * non-blocking mode. Note some data can be left within buffer. In this case
  216. * the "stall" callback will be executed to inform about it. To try to write
  217. * the rest of the data user can be call faux_buf_out() function. Both
  218. * functions will not block.
  219. *
  220. * @param [in] buf Allocated and initialized buf I/O object.
  221. * @param [in] data Data buffer to write.
  222. * @param [in] len Data length to write.
  223. * @return Length of stored/writed data or < 0 on error.
  224. */
  225. ssize_t faux_buf_write(faux_buf_t *buf, const void *data, size_t len)
  226. {
  227. size_t data_left = len;
  228. assert(buf);
  229. if (!buf)
  230. return -1;
  231. assert(data);
  232. if (!data)
  233. return -1;
  234. // It will be overflow after writing
  235. if (faux_buf_will_be_overflow(buf, len))
  236. return -1;
  237. // Don't write to the space reserved for direct write
  238. if (faux_buf_is_wblocked(buf))
  239. return -1;
  240. while (data_left > 0) {
  241. ssize_t bytes_free = 0;
  242. size_t copy_len = 0;
  243. char *chunk_ptr = NULL;
  244. // Allocate new chunk if necessary
  245. bytes_free = faux_buf_wavail(buf);
  246. if (bytes_free < 0)
  247. return -1;
  248. if (0 == bytes_free) {
  249. faux_list_node_t *node = faux_buf_alloc_chunk(buf);
  250. assert(node);
  251. if (!node) // Something went wrong. Strange.
  252. return -1;
  253. buf->wpos = 0;
  254. buf->wchunk = node;
  255. bytes_free = faux_buf_wavail(buf);
  256. }
  257. // Copy data
  258. chunk_ptr = faux_list_data(faux_list_tail(buf->list));
  259. copy_len = (data_left < (size_t)bytes_free) ? data_left : (size_t)bytes_free;
  260. memcpy(chunk_ptr + buf->wpos, data + len - data_left, copy_len);
  261. buf->wpos += copy_len;
  262. data_left -= copy_len;
  263. buf->len += copy_len;
  264. }
  265. return len;
  266. }
  267. /** @brief Write output buffer to fd in non-blocking mode.
  268. *
  269. * Previously data must be written to internal buffer by faux_buf_write()
  270. * function. But some data can be left within internal buffer because can't be
  271. * written to fd in non-blocking mode. This function tries to write the rest of
  272. * data to fd in non-blocking mode. So function doesn't block. It can be called
  273. * after select() or poll() if fd is ready to be written to. If function can't
  274. * to write all buffer to fd it executes "stall" callback to inform about it.
  275. *
  276. * @param [in] buf Allocated and initialized buf I/O object.
  277. * @return Length of data actually written or < 0 on error.
  278. */
  279. ssize_t faux_buf_read(faux_buf_t *buf, void *data, size_t len)
  280. {
  281. ssize_t total_written = 0;
  282. size_t must_be_read = 0;
  283. char *dst = (char *)data;
  284. assert(buf);
  285. if (!buf)
  286. return -1;
  287. assert(data);
  288. if (!data)
  289. return -1;
  290. if (0 == len)
  291. return -1;
  292. // Don't read from the space reserved for direct read
  293. if (faux_buf_is_rblocked(buf))
  294. return -1;
  295. must_be_read = (len < buf->len) ? len : buf->len;
  296. while (must_be_read > 0) {
  297. faux_list_node_t *node = NULL;
  298. char *chunk_ptr = NULL;
  299. ssize_t data_to_write = 0;
  300. size_t avail = 0;
  301. node = faux_list_head(buf->list);
  302. if (!node) // List is empty while buf->len > 0 : strange
  303. return -1;
  304. chunk_ptr = faux_list_data(node);
  305. avail = faux_buf_ravail(buf);
  306. if (avail <= 0) // Strange case
  307. return -1;
  308. data_to_write = (must_be_read < avail) ? must_be_read : avail;
  309. memcpy(dst, chunk_ptr + buf->rpos, data_to_write);
  310. buf->len -= data_to_write;
  311. buf->rpos += data_to_write;
  312. total_written += data_to_write;
  313. dst += data_to_write;
  314. must_be_read -= data_to_write;
  315. // Current chunk was fully copied. So remove it from list.
  316. if (buf->rpos == buf->chunk_size) {
  317. buf->rpos = 0; // 0 position of next chunk
  318. faux_list_del(buf->list, node);
  319. }
  320. }
  321. return total_written;
  322. }