buf.c 20 KB

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