buf.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /** @file buf.c
  2. * @brief Dynamic buffer.
  3. *
  4. * Dynamic buffer can be written to and readed from. It grows while write
  5. * commands.
  6. *
  7. * User can get direct access to this buffer. For example we need to
  8. * read from file some data and save it to dynamic buffer. We pre-allocate
  9. * necessary space within buffer and lock it. Lock function returns a
  10. * "struct iovec" array to write to. After that we unlock buffer. So we don't
  11. * need additional temporary buffer beetween file's read() and dynamic buffer.
  12. * Dynamic buffer has the same functionality for reading from it.
  13. */
  14. #include <stdlib.h>
  15. #include <stdint.h>
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <assert.h>
  19. #include <syslog.h>
  20. #include "faux/faux.h"
  21. #include "faux/str.h"
  22. #include "faux/buf.h"
  23. // Default chunk size
  24. #define DATA_CHUNK 4096
  25. struct faux_buf_s {
  26. faux_list_t *list; // List of chunks
  27. faux_list_node_t *wchunk; // Chunk to write to. NULL if list is empty
  28. size_t rpos; // Read position within first chunk
  29. size_t wpos; // Write position within wchunk (can be non-last chunk)
  30. size_t chunk_size; // Size of chunk
  31. size_t len; // Whole data length
  32. size_t limit; // Overflow limit
  33. size_t rlocked; // How much space is locked for reading
  34. size_t wlocked; // How much space is locked for writing
  35. };
  36. /** @brief Create new dynamic buffer object.
  37. *
  38. * @param [in] chunk_size Chunk size. If "0" then default size will be used.
  39. * @return Allocated object or NULL on error.
  40. */
  41. faux_buf_t *faux_buf_new(size_t chunk_size)
  42. {
  43. faux_buf_t *buf = NULL;
  44. buf = faux_zmalloc(sizeof(*buf));
  45. assert(buf);
  46. if (!buf)
  47. return NULL;
  48. // Init
  49. buf->chunk_size = (chunk_size != 0) ? chunk_size : DATA_CHUNK;
  50. buf->limit = FAUX_BUF_UNLIMITED;
  51. buf->list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  52. NULL, NULL, faux_free);
  53. buf->rpos = 0;
  54. buf->wpos = buf->chunk_size;
  55. buf->len = 0;
  56. buf->wchunk = NULL;
  57. buf->rlocked = 0; // Unlocked
  58. buf->wlocked = 0; // Unlocked
  59. return buf;
  60. }
  61. /** @brief Free dynamic buffer object.
  62. *
  63. * @param [in] buf Buffer object.
  64. */
  65. void faux_buf_free(faux_buf_t *buf)
  66. {
  67. if (!buf)
  68. return;
  69. faux_list_free(buf->list);
  70. faux_free(buf);
  71. }
  72. /** @brief Empty dynamic buffer object.
  73. *
  74. * @param [in] buf Buffer object.
  75. */
  76. bool_t faux_buf_empty(faux_buf_t *buf)
  77. {
  78. if (!buf)
  79. return BOOL_FALSE;
  80. // Don't empty locked buffer
  81. if (faux_buf_is_rlocked(buf) ||
  82. faux_buf_is_wlocked(buf))
  83. return BOOL_FALSE;
  84. faux_list_del_all(buf->list);
  85. buf->rpos = 0;
  86. buf->wpos = buf->chunk_size;
  87. buf->len = 0;
  88. buf->wchunk = NULL;
  89. return BOOL_TRUE;
  90. }
  91. /** @brief Returns length of buffer.
  92. *
  93. * @param [in] buf Allocated and initialized buffer object.
  94. * @return Length of buffer or < 0 on error.
  95. */
  96. ssize_t faux_buf_len(const faux_buf_t *buf)
  97. {
  98. assert(buf);
  99. if (!buf)
  100. return -1;
  101. return buf->len;
  102. }
  103. /** @brief Returns number of allocated data chunks.
  104. *
  105. * Function is not exported to DSO.
  106. *
  107. * @param [in] buf Allocated and initialized buffer object.
  108. * @return Number of allocated chunks or < 0 on error.
  109. */
  110. FAUX_HIDDEN ssize_t faux_buf_chunk_num(const faux_buf_t *buf)
  111. {
  112. assert(buf);
  113. if (!buf)
  114. return -1;
  115. assert(buf->list);
  116. if (!buf->list)
  117. return -1;
  118. return faux_list_len(buf->list);
  119. }
  120. /** @brief Returns limit of buffer length.
  121. *
  122. * The returned "0" means unlimited.
  123. *
  124. * @param [in] buf Allocated and initialized buffer object.
  125. * @return Maximum buffer length or < 0 on error.
  126. */
  127. ssize_t faux_buf_limit(const faux_buf_t *buf)
  128. {
  129. assert(buf);
  130. if (!buf)
  131. return -1;
  132. return buf->limit;
  133. }
  134. /** @brief Set buffer length limit.
  135. *
  136. * Writing more data than this limit will lead to error. The "0" value means
  137. * unlimited buffer. Default is unlimited.
  138. *
  139. * @param [in] buf Allocated and initialized buffer object.
  140. * @param [in] limit Maximum buffer length.
  141. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  142. */
  143. bool_t faux_buf_set_limit(faux_buf_t *buf, size_t limit)
  144. {
  145. assert(buf);
  146. if (!buf)
  147. return BOOL_FALSE;
  148. buf->limit = limit;
  149. return BOOL_TRUE;
  150. }
  151. /** @brief Get amount of unused space within current data chunk.
  152. *
  153. * Inernal static function. Current chunk is "wchunk".
  154. *
  155. * @param [in] buf Allocated and initialized buffer object.
  156. * @return Size of unused space or < 0 on error.
  157. */
  158. static ssize_t faux_buf_wavail(const faux_buf_t *buf)
  159. {
  160. assert(buf);
  161. if (!buf)
  162. return -1;
  163. if (!buf->wchunk)
  164. return 0; // Empty list
  165. return (buf->chunk_size - buf->wpos);
  166. }
  167. /** @brief Get amount of available data within current data chunk.
  168. *
  169. * Inernal static function. Current chunk first chunk.
  170. *
  171. * @param [in] buf Allocated and initialized buffer object.
  172. * @return Size of available data or < 0 on error.
  173. */
  174. static ssize_t faux_buf_ravail(const faux_buf_t *buf)
  175. {
  176. assert(buf);
  177. if (!buf)
  178. return -1;
  179. // Empty list
  180. if (buf->len == 0)
  181. return 0;
  182. // Read and write within the same chunk
  183. if (faux_list_head(buf->list) == buf->wchunk)
  184. return (buf->wpos - buf->rpos);
  185. // Write pointer is far away from read pointer (more than chunk)
  186. return (buf->chunk_size - buf->rpos);
  187. }
  188. /** @brief Get amount of locked space for writing.
  189. *
  190. * The "0" means that buffer is not locked for writing.
  191. *
  192. * @param [in] buf Allocated and initialized buffer object.
  193. * @return Size of locked space or "0" if unlocked.
  194. */
  195. size_t faux_buf_is_wlocked(const faux_buf_t *buf)
  196. {
  197. assert(buf);
  198. if (!buf)
  199. return BOOL_FALSE;
  200. return buf->wlocked;
  201. }
  202. /** @brief Get amount of locked space for reading.
  203. *
  204. * The "0" means that buffer is not locked for reading.
  205. *
  206. * @param [in] buf Allocated and initialized buffer object.
  207. * @return Size of locked data or "0" if unlocked.
  208. */
  209. size_t faux_buf_is_rlocked(const faux_buf_t *buf)
  210. {
  211. assert(buf);
  212. if (!buf)
  213. return BOOL_FALSE;
  214. return buf->rlocked;
  215. }
  216. /** @brief Allocates new chunk and adds it to the end of chunk list.
  217. *
  218. * Static internal function.
  219. *
  220. * @param [in] buf Allocated and initialized buffer object.
  221. * @return Newly created list node or NULL on error.
  222. */
  223. static faux_list_node_t *faux_buf_alloc_chunk(faux_buf_t *buf)
  224. {
  225. char *chunk = NULL;
  226. assert(buf);
  227. if (!buf)
  228. return NULL;
  229. assert(buf->list);
  230. if (!buf->list)
  231. return NULL;
  232. chunk = faux_malloc(buf->chunk_size);
  233. assert(chunk);
  234. if (!chunk)
  235. return NULL;
  236. return faux_list_add(buf->list, chunk);
  237. }
  238. /** @brief Checks if it will be overflow while writing some data.
  239. *
  240. * It uses previously set "limit" value for calculations.
  241. *
  242. * @param [in] buf Allocated and initialized buffer object.
  243. * @param [in] add_len Length of data we want to write to buffer.
  244. * @return BOOL_TRUE - it will be overflow, BOOL_FALSE - enough space.
  245. */
  246. bool_t faux_buf_will_be_overflow(const faux_buf_t *buf, size_t add_len)
  247. {
  248. assert(buf);
  249. if (!buf)
  250. return BOOL_FALSE;
  251. if (FAUX_BUF_UNLIMITED == buf->limit)
  252. return BOOL_FALSE;
  253. if ((buf->len + add_len) > buf->limit)
  254. return BOOL_TRUE;
  255. return BOOL_FALSE;
  256. }
  257. /** @brief Reads dynamic buffer data to specified linear buffer.
  258. *
  259. * @param [in] buf Allocated and initialized dynamic buffer object.
  260. * @param [in] data Linear buffer to read data to.
  261. * @param [in] len Length of data to read.
  262. * @return Length of data actually readed or < 0 on error.
  263. */
  264. ssize_t faux_buf_read(faux_buf_t *buf, void *data, size_t len)
  265. {
  266. struct iovec *iov = NULL;
  267. size_t iov_num = 0;
  268. ssize_t total = 0;
  269. char *dst = (char *)data;
  270. size_t i = 0;
  271. assert(data);
  272. if (!data)
  273. return -1;
  274. total = faux_buf_dread_lock(buf, len, &iov, &iov_num);
  275. if (total <= 0)
  276. return total;
  277. for (i = 0; i < iov_num; i++) {
  278. memcpy(dst, iov[i].iov_base, iov[i].iov_len);
  279. dst += iov[i].iov_len;
  280. }
  281. if (faux_buf_dread_unlock(buf, total, iov) != total)
  282. return -1;
  283. return total;
  284. }
  285. /** @brief Gets "struct iovec" array for direct reading and locks data.
  286. *
  287. * The length of actually locked data can differ from length specified by user.
  288. * When buffer length is less than specified length then return value will be
  289. * equal to buffer length.
  290. *
  291. * @param [in] buf Allocated and initialized dynamic buffer object.
  292. * @param [in] len Length of data to read.
  293. * @param [out] iov_out "struct iovec" array to direct read from.
  294. * @param [out] iov_num_out Number of "struct iovec" array elements.
  295. * @return Length of data actually locked or < 0 on error.
  296. */
  297. ssize_t faux_buf_dread_lock(faux_buf_t *buf, size_t len,
  298. struct iovec **iov_out, size_t *iov_num_out)
  299. {
  300. size_t vec_entries_num = 0;
  301. struct iovec *iov = NULL;
  302. unsigned int i = 0;
  303. faux_list_node_t *iter = NULL;
  304. size_t len_to_lock = 0;
  305. size_t avail = 0;
  306. size_t must_be_read = 0;
  307. assert(buf);
  308. if (!buf)
  309. return -1;
  310. assert(iov_out);
  311. if (!iov_out)
  312. return -1;
  313. assert(iov_num_out);
  314. if (!iov_num_out)
  315. return -1;
  316. // Don't use already locked buffer
  317. if (faux_buf_is_rlocked(buf))
  318. return -1;
  319. len_to_lock = (len < buf->len) ? len : buf->len;
  320. // Nothing to lock
  321. if (0 == len_to_lock) {
  322. *iov_out = NULL;
  323. *iov_num_out = 0;
  324. return 0;
  325. }
  326. // Calculate number of struct iovec entries
  327. avail = faux_buf_ravail(buf);
  328. if (avail > 0)
  329. vec_entries_num++;
  330. if (avail < len_to_lock) {
  331. size_t l = buf->len - avail; // length w/o first chunk
  332. vec_entries_num += l / buf->chunk_size;
  333. if ((l % buf->chunk_size) > 0)
  334. vec_entries_num++;
  335. }
  336. iov = faux_zmalloc(vec_entries_num * sizeof(*iov));
  337. // Iterate chunks. Suppose list is not empty
  338. must_be_read = len_to_lock;
  339. iter = NULL;
  340. while (must_be_read > 0) {
  341. char *data = NULL;
  342. off_t data_offset = 0;
  343. size_t data_len = buf->chunk_size;
  344. size_t p_len = 0;
  345. // First chunk
  346. if (!iter) {
  347. iter = faux_list_head(buf->list);
  348. if (avail > 0) {
  349. data_offset = buf->rpos;
  350. data_len = avail; // Calculated earlier
  351. } else { // Empty chunk. Go to next
  352. iter = faux_list_next_node(iter);
  353. }
  354. // Not-first chunks
  355. } else {
  356. iter = faux_list_next_node(iter);
  357. }
  358. data = (char *)faux_list_data(iter) + data_offset;
  359. p_len = (must_be_read < data_len) ? must_be_read : data_len;
  360. must_be_read -= p_len;
  361. iov[i].iov_base = data;
  362. iov[i].iov_len = p_len;
  363. i++;
  364. }
  365. *iov_out = iov;
  366. *iov_num_out = vec_entries_num;
  367. buf->rlocked = len_to_lock;
  368. return len_to_lock;
  369. }
  370. /** @brief Locks data for reading.
  371. *
  372. * The complimentary function is faux_buf_dread_unlock_easy().
  373. * This function has the same functionality as faux_buf_dread_lock() but
  374. * chooses the lentgh of locked space itself to return single continuous buffer.
  375. *
  376. * @param [in] buf Allocated and initialized dynamic buffer object.
  377. * @param [out] data Continuous buffer for direct reading.
  378. * @return Length of data actually locked or < 0 on error.
  379. */
  380. ssize_t faux_buf_dread_lock_easy(faux_buf_t *buf, void **data)
  381. {
  382. struct iovec *iov = NULL;
  383. size_t iov_num = 0;
  384. ssize_t len_to_lock = 0;
  385. ssize_t avail = 0;
  386. ssize_t locked_len = 0;
  387. assert(buf);
  388. if (!buf)
  389. return -1;
  390. assert(data);
  391. if (!data)
  392. return -1;
  393. // Don't use already locked buffer
  394. if (faux_buf_is_rlocked(buf))
  395. return -1;
  396. avail = faux_buf_ravail(buf);
  397. if (avail < 0)
  398. return -1;
  399. if (0 == avail)
  400. avail = buf->chunk_size; // Next chunk
  401. len_to_lock = ((size_t)avail < buf->len) ? (size_t)avail : buf->len;
  402. // Nothing to lock
  403. if (0 == len_to_lock) {
  404. *data = NULL;
  405. return 0;
  406. }
  407. locked_len = faux_buf_dread_lock(buf, len_to_lock, &iov, &iov_num);
  408. if (locked_len <= 0)
  409. return -1;
  410. if (iov_num < 1) {
  411. faux_free(iov);
  412. return -1;
  413. }
  414. *data = iov[0].iov_base;
  415. locked_len = iov[0].iov_len;
  416. faux_free(iov);
  417. return locked_len;
  418. }
  419. /** @brief Frees "struct iovec" array and unlocks read data.
  420. *
  421. * The length of actually readed data can be less than length of locked data.
  422. * In this case all the data will be unlocked but only actually readed length
  423. * will be removed from buffer.
  424. *
  425. * Function gets "struct iovec" array to free it. It was previously allocated
  426. * by faux_dread_lock() function.
  427. *
  428. * @param [in] buf Allocated and initialized dynamic buffer object.
  429. * @param [in] really_readed Length of data actually read.
  430. * @param [out] iov "struct iovec" array to free.
  431. * @param [out] iov_num_out Number of "struct iovec" array elements.
  432. * @return Length of data actually unlocked or < 0 on error.
  433. */
  434. ssize_t faux_buf_dread_unlock(faux_buf_t *buf, size_t really_readed,
  435. struct iovec *iov)
  436. {
  437. size_t must_be_read = really_readed;
  438. assert(buf);
  439. if (!buf)
  440. return -1;
  441. // Can't unlock non-locked buffer
  442. if (!faux_buf_is_rlocked(buf))
  443. return -1;
  444. if (buf->rlocked < really_readed)
  445. return -1; // Something went wrong
  446. if (buf->len < really_readed)
  447. return -1; // Something went wrong
  448. if (0 == really_readed)
  449. goto unlock;
  450. // Suppose list is not empty
  451. while (must_be_read > 0) {
  452. size_t avail = faux_buf_ravail(buf);
  453. ssize_t data_to_rm = (must_be_read < avail) ? must_be_read : avail;
  454. faux_list_node_t *iter = faux_list_head(buf->list);
  455. buf->len -= data_to_rm;
  456. buf->rpos += data_to_rm;
  457. must_be_read -= data_to_rm;
  458. // Current chunk was fully readed. So remove it from list.
  459. // Chunk is not wchunk
  460. if ((iter != buf->wchunk) &&
  461. (buf->rpos == buf->chunk_size)) {
  462. buf->rpos = 0; // 0 position within next chunk
  463. faux_list_del(buf->list, iter);
  464. if (faux_buf_chunk_num(buf) == 0) { // Empty list w/o locks
  465. buf->wchunk = NULL;
  466. buf->wpos = buf->chunk_size;
  467. }
  468. // Chunk is wchunk
  469. } else if ((iter == buf->wchunk) &&
  470. (buf->rpos == buf->wpos) &&
  471. (!buf->wlocked || // Chunk can be locked for writing
  472. (buf->wpos == buf->chunk_size))) { // Chunk can be filled
  473. buf->rpos = 0; // 0 position within next chunk
  474. buf->wchunk = NULL;
  475. buf->wpos = buf->chunk_size;
  476. faux_list_del(buf->list, iter);
  477. }
  478. }
  479. unlock:
  480. // Unlock whole buffer. Not 'really readed' bytes only
  481. buf->rlocked = 0;
  482. faux_free(iov);
  483. return really_readed;
  484. }
  485. /** @brief Unlocks read data.
  486. *
  487. * It's a function complementary to faux_buf_dread_lock_easy().
  488. * It has the same functionality as faux_dread_unlock() but doesn't free
  489. * "struct iovec" array.
  490. *
  491. * @param [in] buf Allocated and initialized dynamic buffer object.
  492. * @param [in] really_readed Length of data actually readed.
  493. * @return Length of data actually unlocked or < 0 on error.
  494. */
  495. ssize_t faux_buf_dread_unlock_easy(faux_buf_t *buf, size_t really_readed)
  496. {
  497. return faux_buf_dread_unlock(buf, really_readed, NULL);
  498. }
  499. /** @brief Write data from linear buffer to dynamic buffer.
  500. *
  501. * @param [in] buf Allocated and initialized dynamic buffer object.
  502. * @param [in] data Linear buffer. Source of data.
  503. * @param [in] len Length of data to write.
  504. * @return Length of data actually written or < 0 on error.
  505. */
  506. ssize_t faux_buf_write(faux_buf_t *buf, const void *data, size_t len)
  507. {
  508. struct iovec *iov = NULL;
  509. size_t iov_num = 0;
  510. ssize_t total = 0;
  511. char *src = (char *)data;
  512. size_t i = 0;
  513. assert(data);
  514. if (!data)
  515. return -1;
  516. total = faux_buf_dwrite_lock(buf, len, &iov, &iov_num);
  517. if (total <= 0)
  518. return total;
  519. for (i = 0; i < iov_num; i++) {
  520. memcpy(iov[i].iov_base, src, iov[i].iov_len);
  521. src += iov[i].iov_len;
  522. }
  523. if (faux_buf_dwrite_unlock(buf, total, iov) != total)
  524. return -1;
  525. return total;
  526. }
  527. /** @brief Gets "struct iovec" array for direct writing and locks data.
  528. *
  529. * @param [in] buf Allocated and initialized dynamic buffer object.
  530. * @param [in] len Length of data to lock.
  531. * @param [out] iov_out "struct iovec" array to direct write to.
  532. * @param [out] iov_num_out Number of "struct iovec" array elements.
  533. * @return Length of data actually locked or < 0 on error.
  534. */
  535. ssize_t faux_buf_dwrite_lock(faux_buf_t *buf, size_t len,
  536. struct iovec **iov_out, size_t *iov_num_out)
  537. {
  538. size_t vec_entries_num = 0;
  539. struct iovec *iov = NULL;
  540. unsigned int i = 0;
  541. faux_list_node_t *iter = NULL;
  542. size_t avail = 0;
  543. size_t must_be_write = len;
  544. assert(buf);
  545. if (!buf)
  546. return -1;
  547. assert(iov_out);
  548. if (!iov_out)
  549. return -1;
  550. assert(iov_num_out);
  551. if (!iov_num_out)
  552. return -1;
  553. // Don't use already locked buffer
  554. if (faux_buf_is_wlocked(buf))
  555. return -1;
  556. // It will be overflow after writing
  557. if (faux_buf_will_be_overflow(buf, len))
  558. return -1;
  559. // Nothing to lock
  560. if (0 == len) {
  561. *iov_out = NULL;
  562. *iov_num_out = 0;
  563. return 0;
  564. }
  565. // Write lock
  566. buf->wlocked = len;
  567. // Calculate number of struct iovec entries
  568. avail = faux_buf_wavail(buf);
  569. if (avail > 0)
  570. vec_entries_num++;
  571. if (avail < len) {
  572. size_t i = 0;
  573. size_t new_chunk_num = 0;
  574. size_t l = len - avail; // length w/o first chunk
  575. new_chunk_num += l / buf->chunk_size;
  576. if ((l % buf->chunk_size) > 0)
  577. new_chunk_num++;
  578. vec_entries_num += new_chunk_num;
  579. for (i = 0; i < new_chunk_num; i++)
  580. faux_buf_alloc_chunk(buf);
  581. }
  582. iov = faux_zmalloc(vec_entries_num * sizeof(*iov));
  583. assert(iov);
  584. // Iterate chunks
  585. iter = buf->wchunk;
  586. i = 0;
  587. while ((must_be_write > 0)) {
  588. char *data = NULL;
  589. off_t data_offset = 0;
  590. size_t data_len = buf->chunk_size;
  591. size_t p_len = 0;
  592. // List was empty before writing
  593. if (!iter) {
  594. iter = faux_list_head(buf->list);
  595. // Not empty list. First element
  596. } else if ((iter == buf->wchunk) && (i == 0)) {
  597. size_t l = faux_buf_wavail(buf);
  598. if (0 == l) { // Not enough space within current chunk
  599. iter = faux_list_next_node(iter);
  600. } else {
  601. data_offset = buf->wpos;
  602. data_len = l;
  603. }
  604. // Not empty list. Fully free chunk
  605. } else {
  606. iter = faux_list_next_node(iter);
  607. }
  608. p_len = (must_be_write < data_len) ? must_be_write : data_len;
  609. data = (char *)faux_list_data(iter) + data_offset;
  610. must_be_write -= p_len;
  611. iov[i].iov_base = data;
  612. iov[i].iov_len = p_len;
  613. i++;
  614. }
  615. *iov_out = iov;
  616. *iov_num_out = vec_entries_num;
  617. return len;
  618. }
  619. /** @brief Gets a data buffer for direct writing and locks it.
  620. *
  621. * The complimentary function is faux_buf_dwrite_unlock_easy().
  622. * This function has the same functionality as faux_buf_dwrite_lock() but
  623. * choose the lentgh of locked space itself to return single continuous buffer.
  624. *
  625. * @param [in] buf Allocated and initialized dynamic buffer object.
  626. * @param [out] data Continuous buffer for direct writting.
  627. * @return Length of data actually locked or < 0 on error.
  628. */
  629. ssize_t faux_buf_dwrite_lock_easy(faux_buf_t *buf, void **data)
  630. {
  631. struct iovec *iov = NULL;
  632. size_t iov_num = 0;
  633. ssize_t len = 0;
  634. ssize_t locked_len = 0;
  635. assert(buf);
  636. if (!buf)
  637. return -1;
  638. assert(data);
  639. if (!data)
  640. return -1;
  641. // Don't use already locked buffer
  642. if (faux_buf_is_wlocked(buf))
  643. return -1;
  644. len = faux_buf_wavail(buf);
  645. if (len < 0)
  646. return -1;
  647. if (0 == len)
  648. len = buf->chunk_size; // It will use next chunk
  649. locked_len = faux_buf_dwrite_lock(buf, len, &iov, &iov_num);
  650. if (locked_len <= 0)
  651. return -1;
  652. if (iov_num < 1) {
  653. faux_free(iov);
  654. return -1;
  655. }
  656. *data = iov[0].iov_base;
  657. locked_len = iov[0].iov_len;
  658. faux_free(iov);
  659. return locked_len;
  660. }
  661. /** @brief Frees "struct iovec" array and unlocks written data.
  662. *
  663. * The length of actually written data can be less than length of locked data.
  664. * In this case all the data will be unlocked but only actually written length
  665. * will be stored within buffer.
  666. *
  667. * Function gets "struct iovec" array to free it. It was previously allocated
  668. * by faux_dwrite_lock() function.
  669. *
  670. * @param [in] buf Allocated and initialized dynamic buffer object.
  671. * @param [in] really_written Length of data actually written.
  672. * @param [out] iov "struct iovec" array to free.
  673. * @return Length of data actually unlocked or < 0 on error.
  674. */
  675. ssize_t faux_buf_dwrite_unlock(faux_buf_t *buf, size_t really_written,
  676. struct iovec *iov)
  677. {
  678. size_t must_be_write = really_written;
  679. assert(buf);
  680. if (!buf)
  681. return -1;
  682. // Can't unlock non-locked buffer
  683. if (!faux_buf_is_wlocked(buf))
  684. return -1;
  685. if (buf->wlocked < really_written)
  686. return -1; // Something went wrong
  687. while (must_be_write > 0) {
  688. size_t avail = 0;
  689. ssize_t data_to_add = 0;
  690. avail = faux_buf_wavail(buf);
  691. // Current chunk was fully written. So move to next one
  692. if (0 == avail) {
  693. buf->wpos = 0; // 0 position within next chunk
  694. if (buf->wchunk)
  695. buf->wchunk = faux_list_next_node(buf->wchunk);
  696. else
  697. buf->wchunk = faux_list_head(buf->list);
  698. avail = faux_buf_wavail(buf);
  699. }
  700. data_to_add = (must_be_write < avail) ? must_be_write : avail;
  701. buf->len += data_to_add;
  702. buf->wpos += data_to_add;
  703. must_be_write -= data_to_add;
  704. }
  705. if (buf->wchunk) {
  706. faux_list_node_t *iter = NULL;
  707. // Remove trailing empty chunks after wchunk
  708. while ((iter = faux_list_next_node(buf->wchunk)))
  709. faux_list_del(buf->list, iter);
  710. // When really_written == 0 then all data can be read after
  711. // dwrite_lock() and dwrite_unlock() so chunk can be empty.
  712. if ((faux_list_head(buf->list) == buf->wchunk) &&
  713. (buf->wpos == buf->rpos)) {
  714. faux_list_del(buf->list, buf->wchunk);
  715. buf->wchunk = NULL;
  716. buf->wpos = buf->chunk_size;
  717. buf->rpos = 0;
  718. }
  719. }
  720. // Unlock whole buffer. Not 'really written' bytes only
  721. buf->wlocked = 0;
  722. faux_free(iov);
  723. return really_written;
  724. }
  725. /** @brief Unlocks written data.
  726. *
  727. * It's a function complementary to faux_buf_dwrite_lock_easy().
  728. * It has the same functionality as faux_dwrite_unlock() but doesn't free
  729. * "struct iovec" array.
  730. *
  731. * @param [in] buf Allocated and initialized dynamic buffer object.
  732. * @param [in] really_written Length of data actually written.
  733. * @return Length of data actually unlocked or < 0 on error.
  734. */
  735. ssize_t faux_buf_dwrite_unlock_easy(faux_buf_t *buf, size_t really_written)
  736. {
  737. return faux_buf_dwrite_unlock(buf, really_written, NULL);
  738. }