buf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /** @file buf.c
  2. * @brief Dynamic buffer.
  3. *
  4. */
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <assert.h>
  10. #include "faux/faux.h"
  11. #include "faux/str.h"
  12. #include "faux/buf.h"
  13. // Default chunk size
  14. #define DATA_CHUNK 4096
  15. struct faux_buf_s {
  16. faux_list_t *list; // List of chunks
  17. faux_list_node_t *wchunk; // Chunk to write to
  18. size_t rpos; // Read position within first chunk
  19. size_t wpos; // Write position within wchunk (can be non-last chunk)
  20. size_t chunk_size; // Size of chunk
  21. size_t len; // Whole data length
  22. size_t limit; // Overflow limit
  23. size_t rlocked;
  24. size_t wlocked;
  25. };
  26. /** @brief Create new dynamic buffer object.
  27. *
  28. * @param [in] chunk_size Chunk size. If "0" then default size will be used.
  29. * @return Allocated object or NULL on error.
  30. */
  31. faux_buf_t *faux_buf_new(size_t chunk_size)
  32. {
  33. faux_buf_t *buf = NULL;
  34. buf = faux_zmalloc(sizeof(*buf));
  35. assert(buf);
  36. if (!buf)
  37. return NULL;
  38. // Init
  39. buf->chunk_size = (chunk_size != 0) ? chunk_size : DATA_CHUNK;
  40. buf->limit = FAUX_BUF_UNLIMITED;
  41. buf->list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  42. NULL, NULL, faux_free);
  43. buf->rpos = 0;
  44. buf->wpos = buf->chunk_size;
  45. buf->len = 0;
  46. buf->wchunk = NULL;
  47. buf->rlocked = 0; // Unlocked
  48. buf->wlocked = 0; // Unlocked
  49. return buf;
  50. }
  51. /** @brief Free dynamic buffer object.
  52. *
  53. * @param [in] buf Buffer object.
  54. */
  55. void faux_buf_free(faux_buf_t *buf)
  56. {
  57. if (!buf)
  58. return;
  59. faux_list_free(buf->list);
  60. faux_free(buf);
  61. }
  62. ssize_t faux_buf_len(const faux_buf_t *buf)
  63. {
  64. assert(buf);
  65. if (!buf)
  66. return -1;
  67. return buf->len;
  68. }
  69. FAUX_HIDDEN ssize_t faux_buf_chunk_num(const faux_buf_t *buf)
  70. {
  71. assert(buf);
  72. if (!buf)
  73. return -1;
  74. assert(buf->list);
  75. if (!buf->list)
  76. return -1;
  77. return faux_list_len(buf->list);
  78. }
  79. ssize_t faux_buf_limit(const faux_buf_t *buf)
  80. {
  81. assert(buf);
  82. if (!buf)
  83. return -1;
  84. return buf->limit;
  85. }
  86. /** @brief Set size limit.
  87. *
  88. * Read limits define conditions when the read callback will be executed.
  89. * Buffer must contain data amount greater or equal to "min" value. Callback
  90. * will not get data amount greater than "max" value. If min == max then
  91. * callback will be executed with fixed data size. The "max" value can be "0".
  92. * It means indefinite i.e. data transferred to callback can be really large.
  93. *
  94. * @param [in] buf Allocated and initialized buf I/O object.
  95. * @param [in] min Minimal data amount.
  96. * @param [in] max Maximal data amount. The "0" means indefinite.
  97. * @return BOOL_TRUE - success, BOOL_FALSE - error.
  98. */
  99. bool_t faux_buf_set_limit(faux_buf_t *buf, size_t limit)
  100. {
  101. assert(buf);
  102. if (!buf)
  103. return BOOL_FALSE;
  104. buf->limit = limit;
  105. return BOOL_TRUE;
  106. }
  107. /** @brief Get amount of unused space within current data chunk.
  108. *
  109. * Inernal static function.
  110. *
  111. * @param [in] list Internal buffer (list of chunks) to inspect.
  112. * @param [in] pos Current write position within last chunk
  113. * @return Size of unused space or < 0 on error.
  114. */
  115. static ssize_t faux_buf_wavail(const faux_buf_t *buf)
  116. {
  117. assert(buf);
  118. if (!buf)
  119. return -1;
  120. if (!buf->wchunk)
  121. return 0; // Empty list
  122. return (buf->chunk_size - buf->wpos);
  123. }
  124. static ssize_t faux_buf_ravail(const faux_buf_t *buf)
  125. {
  126. assert(buf);
  127. if (!buf)
  128. return -1;
  129. // Empty list
  130. if (buf->len == 0)
  131. return 0;
  132. // Read and write within the same chunk
  133. if (faux_list_head(buf->list) == buf->wchunk)
  134. return (buf->wpos - buf->rpos);
  135. // Write pointer is far away from read pointer (more than chunk)
  136. return (buf->chunk_size - buf->rpos);
  137. }
  138. size_t faux_buf_is_wlocked(const faux_buf_t *buf)
  139. {
  140. assert(buf);
  141. if (!buf)
  142. return BOOL_FALSE;
  143. return buf->wlocked;
  144. }
  145. size_t faux_buf_is_rlocked(const faux_buf_t *buf)
  146. {
  147. assert(buf);
  148. if (!buf)
  149. return BOOL_FALSE;
  150. return buf->rlocked;
  151. }
  152. static faux_list_node_t *faux_buf_alloc_chunk(faux_buf_t *buf)
  153. {
  154. char *chunk = NULL;
  155. assert(buf);
  156. if (!buf)
  157. return NULL;
  158. assert(buf->list);
  159. if (!buf->list)
  160. return NULL;
  161. chunk = faux_malloc(buf->chunk_size);
  162. assert(chunk);
  163. if (!chunk)
  164. return NULL;
  165. return faux_list_add(buf->list, chunk);
  166. }
  167. bool_t faux_buf_will_be_overflow(const faux_buf_t *buf, size_t add_len)
  168. {
  169. assert(buf);
  170. if (!buf)
  171. return BOOL_FALSE;
  172. if (FAUX_BUF_UNLIMITED == buf->limit)
  173. return BOOL_FALSE;
  174. if ((buf->len + add_len) > buf->limit)
  175. return BOOL_TRUE;
  176. return BOOL_FALSE;
  177. }
  178. /** @brief buf data write.
  179. *
  180. * All given data will be stored to internal buffer (list of data chunks).
  181. * Then function will try to write stored data to file descriptor in
  182. * non-locking mode. Note some data can be left within buffer. In this case
  183. * the "stall" callback will be executed to inform about it. To try to write
  184. * the rest of the data user can be call faux_buf_out() function. Both
  185. * functions will not lock.
  186. *
  187. * @param [in] buf Allocated and initialized buf I/O object.
  188. * @param [in] data Data buffer to write.
  189. * @param [in] len Data length to write.
  190. * @return Length of stored/writed data or < 0 on error.
  191. */
  192. ssize_t faux_buf_write(faux_buf_t *buf, const void *data, size_t len)
  193. {
  194. struct iovec *iov = NULL;
  195. size_t iov_num = 0;
  196. ssize_t total = 0;
  197. char *src = (char *)data;
  198. size_t i = 0;
  199. assert(data);
  200. if (!data)
  201. return -1;
  202. total = faux_buf_dwrite_lock(buf, len, &iov, &iov_num);
  203. if (total <= 0)
  204. return total;
  205. for (i = 0; i < iov_num; i++) {
  206. memcpy(iov[i].iov_base, src, iov[i].iov_len);
  207. src += iov[i].iov_len;
  208. }
  209. if (faux_buf_dwrite_unlock(buf, total, iov) != total)
  210. return -1;
  211. return total;
  212. }
  213. /** @brief Write output buffer to fd in non-locking mode.
  214. *
  215. * Previously data must be written to internal buffer by faux_buf_write()
  216. * function. But some data can be left within internal buffer because can't be
  217. * written to fd in non-locking mode. This function tries to write the rest of
  218. * data to fd in non-locking mode. So function doesn't lock. It can be called
  219. * after select() or poll() if fd is ready to be written to. If function can't
  220. * to write all buffer to fd it executes "stall" callback to inform about it.
  221. *
  222. * @param [in] buf Allocated and initialized buf I/O object.
  223. * @return Length of data actually written or < 0 on error.
  224. */
  225. ssize_t faux_buf_read(faux_buf_t *buf, void *data, size_t len)
  226. {
  227. struct iovec *iov = NULL;
  228. size_t iov_num = 0;
  229. ssize_t total = 0;
  230. char *dst = (char *)data;
  231. size_t i = 0;
  232. assert(data);
  233. if (!data)
  234. return -1;
  235. total = faux_buf_dread_lock(buf, len, &iov, &iov_num);
  236. if (total <= 0)
  237. return total;
  238. for (i = 0; i < iov_num; i++) {
  239. memcpy(dst, iov[i].iov_base, iov[i].iov_len);
  240. dst += iov[i].iov_len;
  241. }
  242. if (faux_buf_dread_unlock(buf, total, iov) != total)
  243. return -1;
  244. return total;
  245. }
  246. ssize_t faux_buf_dread_lock(faux_buf_t *buf, size_t len,
  247. struct iovec **iov_out, size_t *iov_num_out)
  248. {
  249. size_t vec_entries_num = 0;
  250. struct iovec *iov = NULL;
  251. unsigned int i = 0;
  252. faux_list_node_t *iter = NULL;
  253. size_t len_to_lock = 0;
  254. size_t avail = 0;
  255. size_t must_be_read = 0;
  256. assert(buf);
  257. if (!buf)
  258. return -1;
  259. assert(iov_out);
  260. if (!iov_out)
  261. return -1;
  262. assert(iov_num_out);
  263. if (!iov_num_out)
  264. return -1;
  265. // Don't use already locked buffer
  266. if (faux_buf_is_rlocked(buf))
  267. return -1;
  268. len_to_lock = (len < buf->len) ? len : buf->len;
  269. // Nothing to lock
  270. if (0 == len_to_lock) {
  271. *iov_out = NULL;
  272. *iov_num_out = 0;
  273. return 0;
  274. }
  275. // Calculate number of struct iovec entries
  276. avail = faux_buf_ravail(buf);
  277. vec_entries_num = 1; // Guaranteed
  278. if (avail < len_to_lock) {
  279. size_t l = buf->len - avail; // length wo first chunk
  280. vec_entries_num += l / buf->chunk_size;
  281. if ((l % buf->chunk_size) > 0)
  282. vec_entries_num++;
  283. }
  284. iov = faux_zmalloc(vec_entries_num * sizeof(*iov));
  285. // Iterate chunks
  286. must_be_read = len_to_lock;
  287. iter = faux_list_head(buf->list);
  288. while ((must_be_read > 0) && (iter)) {
  289. char *p = (char *)faux_list_data(iter);
  290. size_t l = buf->chunk_size;
  291. size_t p_len = 0;
  292. if (iter == faux_list_head(buf->list)) { // First chunk
  293. p += buf->rpos;
  294. l = avail;
  295. }
  296. p_len = (must_be_read < l) ? must_be_read : l;
  297. iov[i].iov_base = p;
  298. iov[i].iov_len = p_len;
  299. i++;
  300. must_be_read -= p_len;
  301. iter = faux_list_next_node(iter);
  302. }
  303. *iov_out = iov;
  304. *iov_num_out = vec_entries_num;
  305. buf->rlocked = len_to_lock;
  306. return len_to_lock;
  307. }
  308. ssize_t faux_buf_dread_unlock(faux_buf_t *buf, size_t really_readed,
  309. struct iovec *iov)
  310. {
  311. size_t must_be_read = 0;
  312. assert(buf);
  313. if (!buf)
  314. return -1;
  315. // Can't unlock non-locked buffer
  316. if (!faux_buf_is_rlocked(buf))
  317. return -1;
  318. if (buf->rlocked < really_readed)
  319. return -1; // Something went wrong
  320. if (buf->len < really_readed)
  321. return -1; // Something went wrong
  322. if (0 == really_readed)
  323. goto unlock;
  324. must_be_read = really_readed;
  325. while (must_be_read > 0) {
  326. size_t avail = faux_buf_ravail(buf);
  327. ssize_t data_to_rm = (must_be_read < avail) ? must_be_read : avail;
  328. buf->len -= data_to_rm;
  329. buf->rpos += data_to_rm;
  330. must_be_read -= data_to_rm;
  331. // Current chunk was fully readed. So remove it from list.
  332. if ((buf->rpos == buf->chunk_size) ||
  333. ((faux_buf_chunk_num(buf) == 1) && (buf->rpos == buf->wpos))
  334. ) {
  335. buf->rpos = 0; // 0 position within next chunk
  336. faux_list_del(buf->list, faux_list_head(buf->list));
  337. }
  338. if (faux_buf_chunk_num(buf) == 0)
  339. buf->wpos = buf->chunk_size;
  340. }
  341. unlock:
  342. // Unlock whole buffer. Not 'really readed' bytes only
  343. buf->rlocked = 0;
  344. faux_free(iov);
  345. return really_readed;
  346. }
  347. ssize_t faux_buf_dwrite_lock(faux_buf_t *buf, size_t len,
  348. struct iovec **iov_out, size_t *iov_num_out)
  349. {
  350. size_t vec_entries_num = 0;
  351. struct iovec *iov = NULL;
  352. unsigned int i = 0;
  353. faux_list_node_t *iter = NULL;
  354. size_t avail = 0;
  355. size_t must_be_write = len;
  356. assert(buf);
  357. if (!buf)
  358. return -1;
  359. assert(iov_out);
  360. if (!iov_out)
  361. return -1;
  362. assert(iov_num_out);
  363. if (!iov_num_out)
  364. return -1;
  365. // Don't use already locked buffer
  366. if (faux_buf_is_wlocked(buf))
  367. return -1;
  368. // It will be overflow after writing
  369. if (faux_buf_will_be_overflow(buf, len))
  370. return -1;
  371. // Nothing to lock
  372. if (0 == len) {
  373. *iov_out = NULL;
  374. *iov_num_out = 0;
  375. return 0;
  376. }
  377. // Write lock
  378. buf->wlocked = len;
  379. // Calculate number of struct iovec entries
  380. avail = faux_buf_wavail(buf);
  381. if (avail > 0)
  382. vec_entries_num++;
  383. if (avail < len) {
  384. size_t i = 0;
  385. size_t new_chunk_num = 0;
  386. size_t l = len - avail; // length w/o first chunk
  387. new_chunk_num += l / buf->chunk_size;
  388. if ((l % buf->chunk_size) > 0)
  389. new_chunk_num++;
  390. vec_entries_num += new_chunk_num;
  391. for (i = 0; i < new_chunk_num; i++)
  392. faux_buf_alloc_chunk(buf);
  393. }
  394. iov = faux_zmalloc(vec_entries_num * sizeof(*iov));
  395. assert(iov);
  396. // Iterate chunks
  397. iter = buf->wchunk;
  398. if (!iter)
  399. iter = faux_list_head(buf->list);
  400. i = 0;
  401. while ((must_be_write > 0) && (iter)) {
  402. char *p = (char *)faux_list_data(iter);
  403. size_t l = buf->chunk_size;
  404. size_t p_len = 0;
  405. if (iter == buf->wchunk) {
  406. p += buf->wpos;
  407. l = faux_buf_wavail(buf);
  408. }
  409. p_len = (must_be_write < l) ? must_be_write : l;
  410. iov[i].iov_base = p;
  411. iov[i].iov_len = p_len;
  412. i++;
  413. must_be_write -= p_len;
  414. iter = faux_list_next_node(iter);
  415. }
  416. *iov_out = iov;
  417. *iov_num_out = vec_entries_num;
  418. return len;
  419. }
  420. ssize_t faux_buf_dwrite_unlock(faux_buf_t *buf, size_t really_written,
  421. struct iovec *iov)
  422. {
  423. size_t must_be_write = 0;
  424. faux_list_node_t *iter = NULL;
  425. assert(buf);
  426. if (!buf)
  427. return -1;
  428. // Can't unlock non-locked buffer
  429. if (!faux_buf_is_wlocked(buf))
  430. return -1;
  431. if (buf->wlocked < really_written)
  432. return -1; // Something went wrong
  433. must_be_write = really_written;
  434. while (must_be_write > 0) {
  435. size_t avail = 0;
  436. ssize_t data_to_add = 0;
  437. // Current chunk was fully written. So move to next one
  438. if (buf->wpos == buf->chunk_size) {
  439. buf->wpos = 0; // 0 position within next chunk
  440. if (buf->wchunk)
  441. buf->wchunk = faux_list_next_node(buf->wchunk);
  442. else
  443. buf->wchunk = faux_list_head(buf->list);
  444. }
  445. avail = faux_buf_wavail(buf);
  446. data_to_add = (must_be_write < avail) ? must_be_write : avail;
  447. buf->len += data_to_add;
  448. buf->wpos += data_to_add;
  449. must_be_write -= data_to_add;
  450. }
  451. if (buf->wchunk) {
  452. // Remove trailing empty chunks after wchunk
  453. while ((iter = faux_list_next_node(buf->wchunk)))
  454. faux_list_del(buf->list, iter);
  455. // When really_written == 0 then all data can be read after
  456. // dwrite_lock() and dwrite_unlock() so chunk can be empty.
  457. if ((faux_list_head(buf->list) == buf->wchunk) &&
  458. (buf->wpos == buf->rpos)) {
  459. faux_list_del(buf->list, buf->wchunk);
  460. buf->wchunk = NULL;
  461. buf->wpos = buf->chunk_size;
  462. }
  463. }
  464. // Unlock whole buffer. Not 'really written' bytes only
  465. buf->wlocked = 0;
  466. faux_free(iov);
  467. return really_written;
  468. }