buf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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 Frees "struct iovec" array and unlocks read data.
  351. *
  352. * The length of actually readed data can be less than length of locked data.
  353. * In this case all the data will be unlocked but only actually readed length
  354. * will be removed from buffer.
  355. *
  356. * Function gets "struct iovec" array to free it. It was previously allocated
  357. * by faux_dread_lock() function.
  358. *
  359. * @param [in] buf Allocated and initialized dynamic buffer object.
  360. * @param [in] really_readed Length of data actually read.
  361. * @param [out] iov "struct iovec" array to free.
  362. * @param [out] iov_num_out Number of "struct iovec" array elements.
  363. * @return Length of data actually unlocked or < 0 on error.
  364. */
  365. ssize_t faux_buf_dread_unlock(faux_buf_t *buf, size_t really_readed,
  366. struct iovec *iov)
  367. {
  368. size_t must_be_read = really_readed;
  369. assert(buf);
  370. if (!buf)
  371. return -1;
  372. // Can't unlock non-locked buffer
  373. if (!faux_buf_is_rlocked(buf))
  374. return -1;
  375. if (buf->rlocked < really_readed)
  376. return -1; // Something went wrong
  377. if (buf->len < really_readed)
  378. return -1; // Something went wrong
  379. if (0 == really_readed)
  380. goto unlock;
  381. // Suppose list is not empty
  382. while (must_be_read > 0) {
  383. size_t avail = faux_buf_ravail(buf);
  384. ssize_t data_to_rm = (must_be_read < avail) ? must_be_read : avail;
  385. faux_list_node_t *iter = faux_list_head(buf->list);
  386. buf->len -= data_to_rm;
  387. buf->rpos += data_to_rm;
  388. must_be_read -= data_to_rm;
  389. // Current chunk was fully readed. So remove it from list.
  390. // Chunk is not wchunk
  391. if ((iter != buf->wchunk) &&
  392. (buf->rpos == buf->chunk_size)) {
  393. buf->rpos = 0; // 0 position within next chunk
  394. faux_list_del(buf->list, iter);
  395. if (faux_buf_chunk_num(buf) == 0) { // Empty list w/o locks
  396. buf->wchunk = NULL;
  397. buf->wpos = buf->chunk_size;
  398. }
  399. // Chunk is wchunk
  400. } else if ((iter == buf->wchunk) &&
  401. (buf->rpos == buf->wpos) &&
  402. (!buf->wlocked || // Chunk can be locked for writing
  403. (buf->wpos == buf->chunk_size))) { // Chunk can be filled
  404. buf->rpos = 0; // 0 position within next chunk
  405. buf->wchunk = NULL;
  406. buf->wpos = buf->chunk_size;
  407. faux_list_del(buf->list, iter);
  408. }
  409. }
  410. unlock:
  411. // Unlock whole buffer. Not 'really readed' bytes only
  412. buf->rlocked = 0;
  413. faux_free(iov);
  414. return really_readed;
  415. }
  416. /** @brief Write data from linear buffer to dynamic buffer.
  417. *
  418. * @param [in] buf Allocated and initialized dynamic buffer object.
  419. * @param [in] data Linear buffer. Source of data.
  420. * @param [in] len Length of data to write.
  421. * @return Length of data actually written or < 0 on error.
  422. */
  423. ssize_t faux_buf_write(faux_buf_t *buf, const void *data, size_t len)
  424. {
  425. struct iovec *iov = NULL;
  426. size_t iov_num = 0;
  427. ssize_t total = 0;
  428. char *src = (char *)data;
  429. size_t i = 0;
  430. assert(data);
  431. if (!data)
  432. return -1;
  433. total = faux_buf_dwrite_lock(buf, len, &iov, &iov_num);
  434. if (total <= 0)
  435. return total;
  436. for (i = 0; i < iov_num; i++) {
  437. memcpy(iov[i].iov_base, src, iov[i].iov_len);
  438. src += iov[i].iov_len;
  439. }
  440. if (faux_buf_dwrite_unlock(buf, total, iov) != total)
  441. return -1;
  442. return total;
  443. }
  444. /** @brief Gets "struct iovec" array for direct writing and locks data.
  445. *
  446. * @param [in] buf Allocated and initialized dynamic buffer object.
  447. * @param [in] len Length of data to lock.
  448. * @param [out] iov_out "struct iovec" array to direct write to.
  449. * @param [out] iov_num_out Number of "struct iovec" array elements.
  450. * @return Length of data actually locked or < 0 on error.
  451. */
  452. ssize_t faux_buf_dwrite_lock(faux_buf_t *buf, size_t len,
  453. struct iovec **iov_out, size_t *iov_num_out)
  454. {
  455. size_t vec_entries_num = 0;
  456. struct iovec *iov = NULL;
  457. unsigned int i = 0;
  458. faux_list_node_t *iter = NULL;
  459. size_t avail = 0;
  460. size_t must_be_write = len;
  461. assert(buf);
  462. if (!buf)
  463. return -1;
  464. assert(iov_out);
  465. if (!iov_out)
  466. return -1;
  467. assert(iov_num_out);
  468. if (!iov_num_out)
  469. return -1;
  470. // Don't use already locked buffer
  471. if (faux_buf_is_wlocked(buf))
  472. return -1;
  473. // It will be overflow after writing
  474. if (faux_buf_will_be_overflow(buf, len))
  475. return -1;
  476. // Nothing to lock
  477. if (0 == len) {
  478. *iov_out = NULL;
  479. *iov_num_out = 0;
  480. return 0;
  481. }
  482. // Write lock
  483. buf->wlocked = len;
  484. // Calculate number of struct iovec entries
  485. avail = faux_buf_wavail(buf);
  486. if (avail > 0)
  487. vec_entries_num++;
  488. if (avail < len) {
  489. size_t i = 0;
  490. size_t new_chunk_num = 0;
  491. size_t l = len - avail; // length w/o first chunk
  492. new_chunk_num += l / buf->chunk_size;
  493. if ((l % buf->chunk_size) > 0)
  494. new_chunk_num++;
  495. vec_entries_num += new_chunk_num;
  496. for (i = 0; i < new_chunk_num; i++)
  497. faux_buf_alloc_chunk(buf);
  498. }
  499. iov = faux_zmalloc(vec_entries_num * sizeof(*iov));
  500. assert(iov);
  501. // Iterate chunks
  502. iter = buf->wchunk;
  503. i = 0;
  504. while ((must_be_write > 0)) {
  505. char *data = NULL;
  506. off_t data_offset = 0;
  507. size_t data_len = buf->chunk_size;
  508. size_t p_len = 0;
  509. // List was empty before writing
  510. if (!iter) {
  511. iter = faux_list_head(buf->list);
  512. // Not empty list. First element
  513. } else if (iter == buf->wchunk) {
  514. size_t l = faux_buf_wavail(buf);
  515. if (0 == l) { // Not enough space within current chunk
  516. iter = faux_list_next_node(iter);
  517. } else {
  518. data_offset = buf->wpos;
  519. data_len = l;
  520. }
  521. // Not empty list. Fully free chunk
  522. } else {
  523. iter = faux_list_next_node(iter);
  524. }
  525. p_len = (must_be_write < data_len) ? must_be_write : data_len;
  526. data = (char *)faux_list_data(iter) + data_offset;
  527. must_be_write -= p_len;
  528. iov[i].iov_base = data;
  529. iov[i].iov_len = p_len;
  530. i++;
  531. }
  532. *iov_out = iov;
  533. *iov_num_out = vec_entries_num;
  534. return len;
  535. }
  536. /** @brief Frees "struct iovec" array and unlocks written data.
  537. *
  538. * The length of actually written data can be less than length of locked data.
  539. * In this case all the data will be unlocked but only actually written length
  540. * will be stored within buffer.
  541. *
  542. * Function gets "struct iovec" array to free it. It was previously allocated
  543. * by faux_dwrite_lock() function.
  544. *
  545. * @param [in] buf Allocated and initialized dynamic buffer object.
  546. * @param [in] really_written Length of data actually written.
  547. * @param [out] iov "struct iovec" array to free.
  548. * @param [out] iov_num_out Number of "struct iovec" array elements.
  549. * @return Length of data actually unlocked or < 0 on error.
  550. */
  551. ssize_t faux_buf_dwrite_unlock(faux_buf_t *buf, size_t really_written,
  552. struct iovec *iov)
  553. {
  554. size_t must_be_write = really_written;
  555. assert(buf);
  556. if (!buf)
  557. return -1;
  558. // Can't unlock non-locked buffer
  559. if (!faux_buf_is_wlocked(buf))
  560. return -1;
  561. if (buf->wlocked < really_written)
  562. return -1; // Something went wrong
  563. while (must_be_write > 0) {
  564. size_t avail = 0;
  565. ssize_t data_to_add = 0;
  566. avail = faux_buf_wavail(buf);
  567. // Current chunk was fully written. So move to next one
  568. if (0 == avail) {
  569. buf->wpos = 0; // 0 position within next chunk
  570. if (buf->wchunk)
  571. buf->wchunk = faux_list_next_node(buf->wchunk);
  572. else
  573. buf->wchunk = faux_list_head(buf->list);
  574. avail = faux_buf_wavail(buf);
  575. }
  576. data_to_add = (must_be_write < avail) ? must_be_write : avail;
  577. buf->len += data_to_add;
  578. buf->wpos += data_to_add;
  579. must_be_write -= data_to_add;
  580. }
  581. if (buf->wchunk) {
  582. faux_list_node_t *iter = NULL;
  583. // Remove trailing empty chunks after wchunk
  584. while ((iter = faux_list_next_node(buf->wchunk)))
  585. faux_list_del(buf->list, iter);
  586. // When really_written == 0 then all data can be read after
  587. // dwrite_lock() and dwrite_unlock() so chunk can be empty.
  588. if ((faux_list_head(buf->list) == buf->wchunk) &&
  589. (buf->wpos == buf->rpos)) {
  590. faux_list_del(buf->list, buf->wchunk);
  591. buf->wchunk = NULL;
  592. buf->wpos = buf->chunk_size;
  593. }
  594. }
  595. // Unlock whole buffer. Not 'really written' bytes only
  596. buf->wlocked = 0;
  597. faux_free(iov);
  598. return really_written;
  599. }