buf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 rlocked;
  24. size_t wlocked;
  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->rlocked = 0; // Unlocked
  48. buf->wlocked = 0; // Unlocked
  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. FAUX_HIDDEN 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(const faux_buf_t *buf)
  116. {
  117. assert(buf);
  118. if (!buf)
  119. return -1;
  120. if (!buf->wchunk)
  121. return 0; // Empty list
  122. return (buf->chunk_size - buf->wpos);
  123. }
  124. static ssize_t faux_buf_ravail(const faux_buf_t *buf)
  125. {
  126. assert(buf);
  127. if (!buf)
  128. return -1;
  129. // Empty list
  130. if (buf->len == 0)
  131. return 0;
  132. // Read and write within the same chunk
  133. if (faux_list_head(buf->list) == buf->wchunk)
  134. return (buf->wpos - buf->rpos);
  135. // Write pointer is far away from read pointer (more than chunk)
  136. return (buf->chunk_size - buf->rpos);
  137. }
  138. size_t faux_buf_is_wlocked(const faux_buf_t *buf)
  139. {
  140. assert(buf);
  141. if (!buf)
  142. return BOOL_FALSE;
  143. return buf->wlocked;
  144. }
  145. size_t faux_buf_is_rlocked(const faux_buf_t *buf)
  146. {
  147. assert(buf);
  148. if (!buf)
  149. return BOOL_FALSE;
  150. return buf->rlocked;
  151. }
  152. static faux_list_node_t *faux_buf_alloc_chunk(faux_buf_t *buf)
  153. {
  154. char *chunk = NULL;
  155. assert(buf);
  156. if (!buf)
  157. return NULL;
  158. assert(buf->list);
  159. if (!buf->list)
  160. return NULL;
  161. chunk = faux_malloc(buf->chunk_size);
  162. assert(chunk);
  163. if (!chunk)
  164. return NULL;
  165. return faux_list_add(buf->list, chunk);
  166. }
  167. bool_t faux_buf_will_be_overflow(const faux_buf_t *buf, size_t add_len)
  168. {
  169. assert(buf);
  170. if (!buf)
  171. return BOOL_FALSE;
  172. if (FAUX_BUF_UNLIMITED == buf->limit)
  173. return BOOL_FALSE;
  174. if ((buf->len + add_len) > buf->limit)
  175. return BOOL_TRUE;
  176. return BOOL_FALSE;
  177. }
  178. /** @brief buf data write.
  179. *
  180. * All given data will be stored to internal buffer (list of data chunks).
  181. * Then function will try to write stored data to file descriptor in
  182. * non-locking mode. Note some data can be left within buffer. In this case
  183. * the "stall" callback will be executed to inform about it. To try to write
  184. * the rest of the data user can be call faux_buf_out() function. Both
  185. * functions will not lock.
  186. *
  187. * @param [in] buf Allocated and initialized buf I/O object.
  188. * @param [in] data Data buffer to write.
  189. * @param [in] len Data length to write.
  190. * @return Length of stored/writed data or < 0 on error.
  191. */
  192. ssize_t faux_buf_write(faux_buf_t *buf, const void *data, size_t len)
  193. {
  194. struct iovec *iov = NULL;
  195. size_t iov_num = 0;
  196. ssize_t total = 0;
  197. char *src = (char *)data;
  198. size_t i = 0;
  199. assert(data);
  200. if (!data)
  201. return -1;
  202. total = faux_buf_dwrite_lock(buf, len, &iov, &iov_num);
  203. if (total <= 0)
  204. return total;
  205. for (i = 0; i < iov_num; i++) {
  206. memcpy(iov[i].iov_base, src, iov[i].iov_len);
  207. src += iov[i].iov_len;
  208. }
  209. if (faux_buf_dwrite_unlock(buf, total, iov) != total)
  210. return -1;
  211. return total;
  212. }
  213. /** @brief Write output buffer to fd in non-locking mode.
  214. *
  215. * Previously data must be written to internal buffer by faux_buf_write()
  216. * function. But some data can be left within internal buffer because can't be
  217. * written to fd in non-locking mode. This function tries to write the rest of
  218. * data to fd in non-locking mode. So function doesn't lock. It can be called
  219. * after select() or poll() if fd is ready to be written to. If function can't
  220. * to write all buffer to fd it executes "stall" callback to inform about it.
  221. *
  222. * @param [in] buf Allocated and initialized buf I/O object.
  223. * @return Length of data actually written or < 0 on error.
  224. */
  225. ssize_t faux_buf_read(faux_buf_t *buf, void *data, size_t len)
  226. {
  227. struct iovec *iov = NULL;
  228. size_t iov_num = 0;
  229. ssize_t total = 0;
  230. char *dst = (char *)data;
  231. size_t i = 0;
  232. assert(data);
  233. if (!data)
  234. return -1;
  235. total = faux_buf_dread_lock(buf, len, &iov, &iov_num);
  236. if (total <= 0)
  237. return total;
  238. for (i = 0; i < iov_num; i++) {
  239. memcpy(dst, iov[i].iov_base, iov[i].iov_len);
  240. dst += iov[i].iov_len;
  241. }
  242. if (faux_buf_dread_unlock(buf, total, iov) != total)
  243. return -1;
  244. return total;
  245. }
  246. ssize_t faux_buf_dread_lock(faux_buf_t *buf, size_t len,
  247. struct iovec **iov_out, size_t *iov_num_out)
  248. {
  249. size_t vec_entries_num = 0;
  250. struct iovec *iov = NULL;
  251. unsigned int i = 0;
  252. faux_list_node_t *iter = NULL;
  253. size_t len_to_lock = 0;
  254. size_t avail = 0;
  255. size_t must_be_read = 0;
  256. assert(buf);
  257. if (!buf)
  258. return -1;
  259. assert(iov_out);
  260. if (!iov_out)
  261. return -1;
  262. assert(iov_num_out);
  263. if (!iov_num_out)
  264. return -1;
  265. // Don't use already locked buffer
  266. if (faux_buf_is_rlocked(buf))
  267. return -1;
  268. len_to_lock = (len < buf->len) ? len : buf->len;
  269. // Nothing to lock
  270. if (0 == len_to_lock) {
  271. *iov_out = NULL;
  272. *iov_num_out = 0;
  273. return 0;
  274. }
  275. // Calculate number of struct iovec entries
  276. avail = faux_buf_ravail(buf);
  277. if (avail > 0)
  278. vec_entries_num++;
  279. if (avail < len_to_lock) {
  280. size_t l = buf->len - avail; // length wo first chunk
  281. vec_entries_num += l / buf->chunk_size;
  282. if ((l % buf->chunk_size) > 0)
  283. vec_entries_num++;
  284. }
  285. iov = faux_zmalloc(vec_entries_num * sizeof(*iov));
  286. // Iterate chunks
  287. must_be_read = len_to_lock;
  288. iter = faux_list_head(buf->list);
  289. while ((must_be_read > 0) && (iter)) {
  290. char *p = (char *)faux_list_data(iter);
  291. size_t l = buf->chunk_size;
  292. size_t p_len = 0;
  293. if (iter == faux_list_head(buf->list)) { // First chunk
  294. p += buf->rpos;
  295. l = avail;
  296. }
  297. p_len = (must_be_read < l) ? must_be_read : l;
  298. iov[i].iov_base = p;
  299. iov[i].iov_len = p_len;
  300. i++;
  301. must_be_read -= p_len;
  302. iter = faux_list_next_node(iter);
  303. }
  304. *iov_out = iov;
  305. *iov_num_out = vec_entries_num;
  306. buf->rlocked = len_to_lock;
  307. return len_to_lock;
  308. }
  309. ssize_t faux_buf_dread_unlock(faux_buf_t *buf, size_t really_readed,
  310. struct iovec *iov)
  311. {
  312. size_t must_be_read = 0;
  313. assert(buf);
  314. if (!buf)
  315. return -1;
  316. // Can't unlock non-locked buffer
  317. if (!faux_buf_is_rlocked(buf))
  318. return -1;
  319. if (buf->rlocked < really_readed)
  320. return -1; // Something went wrong
  321. if (buf->len < really_readed)
  322. return -1; // Something went wrong
  323. if (0 == really_readed)
  324. goto unlock;
  325. must_be_read = really_readed;
  326. while (must_be_read > 0) {
  327. size_t avail = faux_buf_ravail(buf);
  328. ssize_t data_to_rm = (must_be_read < avail) ? must_be_read : avail;
  329. buf->len -= data_to_rm;
  330. buf->rpos += data_to_rm;
  331. must_be_read -= data_to_rm;
  332. // Current chunk was fully readed. So remove it from list.
  333. if ((buf->rpos == buf->chunk_size) ||
  334. ((faux_buf_chunk_num(buf) == 1) && (buf->rpos == buf->wpos))
  335. ) {
  336. buf->rpos = 0; // 0 position within next chunk
  337. faux_list_del(buf->list, faux_list_head(buf->list));
  338. }
  339. if (faux_buf_chunk_num(buf) == 0)
  340. buf->wpos = buf->chunk_size;
  341. }
  342. unlock:
  343. // Unlock whole buffer. Not 'really readed' bytes only
  344. buf->rlocked = 0;
  345. faux_free(iov);
  346. return really_readed;
  347. }
  348. ssize_t faux_buf_dwrite_lock(faux_buf_t *buf, size_t len,
  349. struct iovec **iov_out, size_t *iov_num_out)
  350. {
  351. size_t vec_entries_num = 0;
  352. struct iovec *iov = NULL;
  353. unsigned int i = 0;
  354. faux_list_node_t *iter = NULL;
  355. size_t avail = 0;
  356. size_t must_be_write = len;
  357. assert(buf);
  358. if (!buf)
  359. return -1;
  360. assert(iov_out);
  361. if (!iov_out)
  362. return -1;
  363. assert(iov_num_out);
  364. if (!iov_num_out)
  365. return -1;
  366. // Don't use already locked buffer
  367. if (faux_buf_is_wlocked(buf))
  368. return -1;
  369. // It will be overflow after writing
  370. if (faux_buf_will_be_overflow(buf, len))
  371. return -1;
  372. // Nothing to lock
  373. if (0 == len) {
  374. *iov_out = NULL;
  375. *iov_num_out = 0;
  376. return 0;
  377. }
  378. // Write lock
  379. buf->wlocked = len;
  380. // Calculate number of struct iovec entries
  381. avail = faux_buf_wavail(buf);
  382. if (avail > 0)
  383. vec_entries_num++;
  384. if (avail < len) {
  385. size_t i = 0;
  386. size_t new_chunk_num = 0;
  387. size_t l = len - avail; // length w/o first chunk
  388. new_chunk_num += l / buf->chunk_size;
  389. if ((l % buf->chunk_size) > 0)
  390. new_chunk_num++;
  391. vec_entries_num += new_chunk_num;
  392. for (i = 0; i < new_chunk_num; i++)
  393. faux_buf_alloc_chunk(buf);
  394. }
  395. iov = faux_zmalloc(vec_entries_num * sizeof(*iov));
  396. assert(iov);
  397. // Iterate chunks
  398. iter = buf->wchunk;
  399. if (!iter)
  400. iter = faux_list_head(buf->list);
  401. i = 0;
  402. while ((must_be_write > 0) && (iter) && (i < vec_entries_num)) {
  403. // while ((must_be_write > 0) && (iter)) {
  404. char *p = (char *)faux_list_data(iter);
  405. size_t l = buf->chunk_size;
  406. size_t p_len = 0;
  407. if (iter == buf->wchunk) {
  408. p += buf->wpos;
  409. l = faux_buf_wavail(buf);
  410. }
  411. p_len = (must_be_write < l) ? must_be_write : l;
  412. printf("num=%lu i=%u must=%lu plen=%lu\n", vec_entries_num, i, must_be_write, p_len);
  413. iter = faux_list_next_node(iter);
  414. // If wpos == chunk_size then p_len = 0
  415. // So go to next iteration without iov filling
  416. if (0 == p_len)
  417. continue;
  418. iov[i].iov_base = p;
  419. iov[i].iov_len = p_len;
  420. i++;
  421. must_be_write -= p_len;
  422. }
  423. *iov_out = iov;
  424. *iov_num_out = vec_entries_num;
  425. return len;
  426. }
  427. ssize_t faux_buf_dwrite_unlock(faux_buf_t *buf, size_t really_written,
  428. struct iovec *iov)
  429. {
  430. size_t must_be_write = 0;
  431. faux_list_node_t *iter = NULL;
  432. assert(buf);
  433. if (!buf)
  434. return -1;
  435. // Can't unlock non-locked buffer
  436. if (!faux_buf_is_wlocked(buf))
  437. return -1;
  438. if (buf->wlocked < really_written)
  439. return -1; // Something went wrong
  440. must_be_write = really_written;
  441. while (must_be_write > 0) {
  442. size_t avail = 0;
  443. ssize_t data_to_add = 0;
  444. printf("must=%lu\n", must_be_write);
  445. // Current chunk was fully written. So move to next one
  446. if (buf->wpos == buf->chunk_size) {
  447. buf->wpos = 0; // 0 position within next chunk
  448. if (buf->wchunk)
  449. buf->wchunk = faux_list_next_node(buf->wchunk);
  450. else
  451. buf->wchunk = faux_list_head(buf->list);
  452. }
  453. avail = faux_buf_wavail(buf);
  454. data_to_add = (must_be_write < avail) ? must_be_write : avail;
  455. buf->len += data_to_add;
  456. buf->wpos += data_to_add;
  457. must_be_write -= data_to_add;
  458. }
  459. if (buf->wchunk) {
  460. // Remove trailing empty chunks after wchunk
  461. while ((iter = faux_list_next_node(buf->wchunk)))
  462. faux_list_del(buf->list, iter);
  463. // When really_written == 0 then all data can be read after
  464. // dwrite_lock() and dwrite_unlock() so chunk can be empty.
  465. if ((faux_list_head(buf->list) == buf->wchunk) &&
  466. (buf->wpos == buf->rpos)) {
  467. faux_list_del(buf->list, buf->wchunk);
  468. buf->wchunk = NULL;
  469. buf->wpos = buf->chunk_size;
  470. }
  471. }
  472. // Unlock whole buffer. Not 'really written' bytes only
  473. buf->wlocked = 0;
  474. faux_free(iov);
  475. return really_written;
  476. }