2
0

bufq.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #ifndef HEADER_CURL_BUFQ_H
  2. #define HEADER_CURL_BUFQ_H
  3. /***************************************************************************
  4. * _ _ ____ _
  5. * Project ___| | | | _ \| |
  6. * / __| | | | |_) | |
  7. * | (__| |_| | _ <| |___
  8. * \___|\___/|_| \_\_____|
  9. *
  10. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  11. *
  12. * This software is licensed as described in the file COPYING, which
  13. * you should have received as part of this distribution. The terms
  14. * are also available at https://curl.se/docs/copyright.html.
  15. *
  16. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  17. * copies of the Software, and permit persons to whom the Software is
  18. * furnished to do so, under the terms of the COPYING file.
  19. *
  20. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  21. * KIND, either express or implied.
  22. *
  23. * SPDX-License-Identifier: curl
  24. *
  25. ***************************************************************************/
  26. #include "curl_setup.h"
  27. #include <curl/curl.h>
  28. /**
  29. * A chunk of bytes for reading and writing.
  30. * The size is fixed a creation with read and write offset
  31. * for where unread content is.
  32. */
  33. struct buf_chunk {
  34. struct buf_chunk *next; /* to keep it in a list */
  35. size_t dlen; /* the amount of allocated x.data[] */
  36. size_t r_offset; /* first unread bytes */
  37. size_t w_offset; /* one after last written byte */
  38. union {
  39. unsigned char data[1]; /* the buffer for `dlen` bytes */
  40. void *dummy; /* alignment */
  41. } x;
  42. };
  43. /**
  44. * A pool for providing/keeping a number of chunks of the same size
  45. *
  46. * The same pool can be shared by many `bufq` instances. However, a pool
  47. * is not thread safe. All bufqs using it are supposed to operate in the
  48. * same thread.
  49. */
  50. struct bufc_pool {
  51. struct buf_chunk *spare; /* list of available spare chunks */
  52. size_t chunk_size; /* the size of chunks in this pool */
  53. size_t spare_count; /* current number of spare chunks in list */
  54. size_t spare_max; /* max number of spares to keep */
  55. };
  56. void Curl_bufcp_init(struct bufc_pool *pool,
  57. size_t chunk_size, size_t spare_max);
  58. void Curl_bufcp_free(struct bufc_pool *pool);
  59. /**
  60. * A queue of byte chunks for reading and writing.
  61. * Reading is done from `head`, writing is done to `tail`.
  62. *
  63. * `bufq`s can be empty or full or neither. Its `len` is the number
  64. * of bytes that can be read. For an empty bufq, `len` will be 0.
  65. *
  66. * By default, a bufq can hold up to `max_chunks * chunk_size` number
  67. * of bytes. When `max_chunks` are used (in the `head` list) and the
  68. * `tail` chunk is full, the bufq will report that it is full.
  69. *
  70. * On a full bufq, `len` may be less than the maximum number of bytes,
  71. * e.g. when the head chunk is partially read. `len` may also become
  72. * larger than the max when option `BUFQ_OPT_SOFT_LIMIT` is used.
  73. *
  74. * By default, writing to a full bufq will return (-1, CURLE_AGAIN). Same
  75. * as reading from an empty bufq.
  76. * With `BUFQ_OPT_SOFT_LIMIT` set, a bufq will allow writing becond this
  77. * limit and use more than `max_chunks`. However it will report that it
  78. * is full nevertheless. This is provided for situation where writes
  79. * preferably never fail (except for memory exhaustion).
  80. *
  81. * By default and without a pool, a bufq will keep chunks that read
  82. * empty in its `spare` list. Option `BUFQ_OPT_NO_SPARES` will
  83. * disable that and free chunks once they become empty.
  84. *
  85. * When providing a pool to a bufq, all chunk creation and spare handling
  86. * will be delegated to that pool.
  87. */
  88. struct bufq {
  89. struct buf_chunk *head; /* chunk with bytes to read from */
  90. struct buf_chunk *tail; /* chunk to write to */
  91. struct buf_chunk *spare; /* list of free chunks, unless `pool` */
  92. struct bufc_pool *pool; /* optional pool for free chunks */
  93. size_t chunk_count; /* current number of chunks in `head+spare` */
  94. size_t max_chunks; /* max `head` chunks to use */
  95. size_t chunk_size; /* size of chunks to manage */
  96. int opts; /* options for handling queue, see below */
  97. };
  98. /**
  99. * Default behaviour: chunk limit is "hard", meaning attempts to write
  100. * more bytes than can be hold in `max_chunks` is refused and will return
  101. * -1, CURLE_AGAIN. */
  102. #define BUFQ_OPT_NONE (0)
  103. /**
  104. * Make `max_chunks` a "soft" limit. A bufq will report that it is "full"
  105. * when `max_chunks` are used, but allows writing beyond this limit.
  106. */
  107. #define BUFQ_OPT_SOFT_LIMIT (1 << 0)
  108. /**
  109. * Do not keep spare chunks.
  110. */
  111. #define BUFQ_OPT_NO_SPARES (1 << 1)
  112. /**
  113. * Initialize a buffer queue that can hold up to `max_chunks` buffers
  114. * each of size `chunk_size`. The bufq will not allow writing of
  115. * more bytes than can be held in `max_chunks`.
  116. */
  117. void Curl_bufq_init(struct bufq *q, size_t chunk_size, size_t max_chunks);
  118. /**
  119. * Initialize a buffer queue that can hold up to `max_chunks` buffers
  120. * each of size `chunk_size` with the given options. See `BUFQ_OPT_*`.
  121. */
  122. void Curl_bufq_init2(struct bufq *q, size_t chunk_size,
  123. size_t max_chunks, int opts);
  124. void Curl_bufq_initp(struct bufq *q, struct bufc_pool *pool,
  125. size_t max_chunks, int opts);
  126. /**
  127. * Reset the buffer queue to be empty. Will keep any allocated buffer
  128. * chunks around.
  129. */
  130. void Curl_bufq_reset(struct bufq *q);
  131. /**
  132. * Free all resources held by the buffer queue.
  133. */
  134. void Curl_bufq_free(struct bufq *q);
  135. /**
  136. * Return the total amount of data in the queue.
  137. */
  138. size_t Curl_bufq_len(const struct bufq *q);
  139. /**
  140. * Return the total amount of free space in the queue.
  141. * The returned length is the number of bytes that can
  142. * be expected to be written successfully to the bufq,
  143. * providing no memory allocations fail.
  144. */
  145. size_t Curl_bufq_space(const struct bufq *q);
  146. /**
  147. * Returns TRUE iff there is no data in the buffer queue.
  148. */
  149. bool Curl_bufq_is_empty(const struct bufq *q);
  150. /**
  151. * Returns TRUE iff there is no space left in the buffer queue.
  152. */
  153. bool Curl_bufq_is_full(const struct bufq *q);
  154. /**
  155. * Write buf to the end of the buffer queue. The buf is copied
  156. * and the amount of copied bytes is returned.
  157. * A return code of -1 indicates an error, setting `err` to the
  158. * cause. An err of CURLE_AGAIN is returned if the buffer queue is full.
  159. */
  160. ssize_t Curl_bufq_write(struct bufq *q,
  161. const unsigned char *buf, size_t len,
  162. CURLcode *err);
  163. CURLcode Curl_bufq_cwrite(struct bufq *q,
  164. const char *buf, size_t len,
  165. size_t *pnwritten);
  166. /**
  167. * Remove `len` bytes from the end of the buffer queue again.
  168. * Returns CURLE_AGAIN if less than `len` bytes were in the queue.
  169. */
  170. CURLcode Curl_bufq_unwrite(struct bufq *q, size_t len);
  171. /**
  172. * Read buf from the start of the buffer queue. The buf is copied
  173. * and the amount of copied bytes is returned.
  174. * A return code of -1 indicates an error, setting `err` to the
  175. * cause. An err of CURLE_AGAIN is returned if the buffer queue is empty.
  176. */
  177. ssize_t Curl_bufq_read(struct bufq *q, unsigned char *buf, size_t len,
  178. CURLcode *err);
  179. CURLcode Curl_bufq_cread(struct bufq *q, char *buf, size_t len,
  180. size_t *pnread);
  181. /**
  182. * Peek at the head chunk in the buffer queue. Returns a pointer to
  183. * the chunk buf (at the current offset) and its length. Does not
  184. * modify the buffer queue.
  185. * Returns TRUE iff bytes are available. Sets `pbuf` to NULL and `plen`
  186. * to 0 when no bytes are available.
  187. * Repeated calls return the same information until the buffer queue
  188. * is modified, see `Curl_bufq_skip()``
  189. */
  190. bool Curl_bufq_peek(struct bufq *q,
  191. const unsigned char **pbuf, size_t *plen);
  192. bool Curl_bufq_peek_at(struct bufq *q, size_t offset,
  193. const unsigned char **pbuf, size_t *plen);
  194. /**
  195. * Tell the buffer queue to discard `amount` buf bytes at the head
  196. * of the queue. Skipping more buf than is currently buffered will
  197. * just empty the queue.
  198. */
  199. void Curl_bufq_skip(struct bufq *q, size_t amount);
  200. typedef ssize_t Curl_bufq_writer(void *writer_ctx,
  201. const unsigned char *buf, size_t len,
  202. CURLcode *err);
  203. /**
  204. * Passes the chunks in the buffer queue to the writer and returns
  205. * the amount of buf written. A writer may return -1 and CURLE_AGAIN
  206. * to indicate blocking at which point the queue will stop and return
  207. * the amount of buf passed so far.
  208. * -1 is returned on any other errors reported by the writer.
  209. * Note that in case of a -1 chunks may have been written and
  210. * the buffer queue will have different length than before.
  211. */
  212. ssize_t Curl_bufq_pass(struct bufq *q, Curl_bufq_writer *writer,
  213. void *writer_ctx, CURLcode *err);
  214. typedef ssize_t Curl_bufq_reader(void *reader_ctx,
  215. unsigned char *buf, size_t len,
  216. CURLcode *err);
  217. /**
  218. * Read date and append it to the end of the buffer queue until the
  219. * reader returns blocking or the queue is full. A reader returns
  220. * -1 and CURLE_AGAIN to indicate blocking.
  221. * Returns the total amount of buf read (may be 0) or -1 on other
  222. * reader errors.
  223. * Note that in case of a -1 chunks may have been read and
  224. * the buffer queue will have different length than before.
  225. */
  226. ssize_t Curl_bufq_slurp(struct bufq *q, Curl_bufq_reader *reader,
  227. void *reader_ctx, CURLcode *err);
  228. /**
  229. * Read *once* up to `max_len` bytes and append it to the buffer.
  230. * if `max_len` is 0, no limit is imposed besides the chunk space.
  231. * Returns the total amount of buf read (may be 0) or -1 on other
  232. * reader errors.
  233. */
  234. ssize_t Curl_bufq_sipn(struct bufq *q, size_t max_len,
  235. Curl_bufq_reader *reader, void *reader_ctx,
  236. CURLcode *err);
  237. /**
  238. * Write buf to the end of the buffer queue.
  239. * Will write bufq content or passed `buf` directly using the `writer`
  240. * callback when it sees fit. 'buf' might get passed directly
  241. * on or is placed into the buffer, depending on `len` and current
  242. * amount buffered, chunk size, etc.
  243. */
  244. ssize_t Curl_bufq_write_pass(struct bufq *q,
  245. const unsigned char *buf, size_t len,
  246. Curl_bufq_writer *writer, void *writer_ctx,
  247. CURLcode *err);
  248. #endif /* HEADER_CURL_BUFQ_H */