buf.c 20 KB

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