nghttp2_buf.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright (C) 2015-2018 Alibaba Group Holding Limited
  3. */
  4. #ifndef NGHTTP2_BUF_H
  5. #define NGHTTP2_BUF_H
  6. #ifdef HAVE_CONFIG_H
  7. #include <config.h>
  8. #endif /* HAVE_CONFIG_H */
  9. #include "nghttp2.h"
  10. #include "nghttp2_int.h"
  11. #include "nghttp2_mem.h"
  12. typedef struct {
  13. /* This points to the beginning of the buffer. The effective range
  14. of buffer is [begin, end). */
  15. uint8_t *begin;
  16. /* This points to the memory one byte beyond the end of the
  17. buffer. */
  18. uint8_t *end;
  19. /* The position indicator for effective start of the buffer. pos <=
  20. last must be hold. */
  21. uint8_t *pos;
  22. /* The position indicator for effective one beyond of the end of the
  23. buffer. last <= end must be hold. */
  24. uint8_t *last;
  25. /* Mark arbitrary position in buffer [begin, end) */
  26. uint8_t *mark;
  27. } nghttp2_buf;
  28. #define nghttp2_buf_len(BUF) ((size_t)((BUF)->last - (BUF)->pos))
  29. #define nghttp2_buf_avail(BUF) ((size_t)((BUF)->end - (BUF)->last))
  30. #define nghttp2_buf_mark_avail(BUF) ((size_t)((BUF)->mark - (BUF)->last))
  31. #define nghttp2_buf_cap(BUF) ((size_t)((BUF)->end - (BUF)->begin))
  32. #define nghttp2_buf_pos_offset(BUF) ((size_t)((BUF)->pos - (BUF)->begin))
  33. #define nghttp2_buf_last_offset(BUF) ((size_t)((BUF)->last - (BUF)->begin))
  34. #define nghttp2_buf_shift_right(BUF, AMT) \
  35. do { \
  36. (BUF)->pos += AMT; \
  37. (BUF)->last += AMT; \
  38. } while (0)
  39. #define nghttp2_buf_shift_left(BUF, AMT) \
  40. do { \
  41. (BUF)->pos -= AMT; \
  42. (BUF)->last -= AMT; \
  43. } while (0)
  44. /*
  45. * Initializes the |buf|. No memory is allocated in this function. Use
  46. * nghttp2_buf_reserve() to allocate memory.
  47. */
  48. void nghttp2_buf_init(nghttp2_buf *buf);
  49. /*
  50. * Initializes the |buf| and allocates at least |initial| bytes of
  51. * memory.
  52. *
  53. * This function returns 0 if it succeeds, or one of the following
  54. * negative error codes:
  55. *
  56. * NGHTTP2_ERR_NOMEM
  57. * Out of memory
  58. */
  59. int nghttp2_buf_init2(nghttp2_buf *buf, size_t initial, nghttp2_mem *mem);
  60. /*
  61. * Frees buffer in |buf|.
  62. */
  63. void nghttp2_buf_free(nghttp2_buf *buf, nghttp2_mem *mem);
  64. /*
  65. * Extends buffer so that nghttp2_buf_cap() returns at least
  66. * |new_cap|. If extensions took place, buffer pointers in |buf| will
  67. * change.
  68. *
  69. * This function returns 0 if it succeeds, or one of the followings
  70. * negative error codes:
  71. *
  72. * NGHTTP2_ERR_NOMEM
  73. * Out of memory
  74. */
  75. int nghttp2_buf_reserve(nghttp2_buf *buf, size_t new_cap, nghttp2_mem *mem);
  76. /*
  77. * Resets pos, last, mark member of |buf| to buf->begin.
  78. */
  79. void nghttp2_buf_reset(nghttp2_buf *buf);
  80. /*
  81. * Initializes |buf| using supplied buffer |begin| of length
  82. * |len|. Semantically, the application should not call *_reserve() or
  83. * nghttp2_free() functions for |buf|.
  84. */
  85. void nghttp2_buf_wrap_init(nghttp2_buf *buf, uint8_t *begin, size_t len);
  86. struct nghttp2_buf_chain;
  87. typedef struct nghttp2_buf_chain nghttp2_buf_chain;
  88. /* Chains 2 buffers */
  89. struct nghttp2_buf_chain {
  90. /* Points to the subsequent buffer. NULL if there is no such
  91. buffer. */
  92. nghttp2_buf_chain *next;
  93. nghttp2_buf buf;
  94. };
  95. typedef struct {
  96. /* Points to the first buffer */
  97. nghttp2_buf_chain *head;
  98. /* Buffer pointer where write occurs. */
  99. nghttp2_buf_chain *cur;
  100. /* Memory allocator */
  101. nghttp2_mem *mem;
  102. /* The buffer capacity of each buf. This field may be 0 if
  103. nghttp2_bufs is initialized by nghttp2_bufs_wrap_init* family
  104. functions. */
  105. size_t chunk_length;
  106. /* The maximum number of nghttp2_buf_chain */
  107. size_t max_chunk;
  108. /* The number of nghttp2_buf_chain allocated */
  109. size_t chunk_used;
  110. /* The number of nghttp2_buf_chain to keep on reset */
  111. size_t chunk_keep;
  112. /* pos offset from begin in each buffers. On initialization and
  113. reset, buf->pos and buf->last are positioned at buf->begin +
  114. offset. */
  115. size_t offset;
  116. } nghttp2_bufs;
  117. /*
  118. * This is the same as calling nghttp2_bufs_init2 with the given
  119. * arguments and offset = 0.
  120. */
  121. int nghttp2_bufs_init(nghttp2_bufs *bufs, size_t chunk_length, size_t max_chunk,
  122. nghttp2_mem *mem);
  123. /*
  124. * This is the same as calling nghttp2_bufs_init3 with the given
  125. * arguments and chunk_keep = max_chunk.
  126. */
  127. int nghttp2_bufs_init2(nghttp2_bufs *bufs, size_t chunk_length,
  128. size_t max_chunk, size_t offset, nghttp2_mem *mem);
  129. /*
  130. * Initializes |bufs|. Each buffer size is given in the
  131. * |chunk_length|. The maximum number of buffers is given in the
  132. * |max_chunk|. On reset, first |chunk_keep| buffers are kept and
  133. * remaining buffers are deleted. Each buffer will have bufs->pos and
  134. * bufs->last shifted to left by |offset| bytes on creation and reset.
  135. *
  136. * This function allocates first buffer. bufs->head and bufs->cur
  137. * will point to the first buffer after this call.
  138. *
  139. * This function returns 0 if it succeeds, or one of the following
  140. * negative error codes:
  141. *
  142. * NGHTTP2_ERR_NOMEM
  143. * Out of memory.
  144. * NGHTTP2_ERR_INVALID_ARGUMENT
  145. * chunk_keep is 0; or max_chunk < chunk_keep; or offset is too
  146. * long.
  147. */
  148. int nghttp2_bufs_init3(nghttp2_bufs *bufs, size_t chunk_length,
  149. size_t max_chunk, size_t chunk_keep, size_t offset,
  150. nghttp2_mem *mem);
  151. /*
  152. * Frees any related resources to the |bufs|.
  153. */
  154. void nghttp2_bufs_free(nghttp2_bufs *bufs);
  155. /*
  156. * Initializes |bufs| using supplied buffer |begin| of length |len|.
  157. * The first buffer bufs->head uses buffer |begin|. The buffer size
  158. * is fixed and no extra chunk buffer is allocated. In other
  159. * words, max_chunk = chunk_keep = 1. To free the resource allocated
  160. * for |bufs|, use nghttp2_bufs_wrap_free().
  161. *
  162. * Don't use the function which performs allocation, such as
  163. * nghttp2_bufs_realloc().
  164. *
  165. * This function returns 0 if it succeeds, or one of the following
  166. * negative error codes:
  167. *
  168. * NGHTTP2_ERR_NOMEM
  169. * Out of memory.
  170. */
  171. int nghttp2_bufs_wrap_init(nghttp2_bufs *bufs, uint8_t *begin, size_t len,
  172. nghttp2_mem *mem);
  173. /*
  174. * Initializes |bufs| using supplied |veclen| size of buf vector
  175. * |vec|. The number of buffers is fixed and no extra chunk buffer is
  176. * allocated. In other words, max_chunk = chunk_keep = |in_len|. To
  177. * free the resource allocated for |bufs|, use
  178. * nghttp2_bufs_wrap_free().
  179. *
  180. * Don't use the function which performs allocation, such as
  181. * nghttp2_bufs_realloc().
  182. *
  183. * This function returns 0 if it succeeds, or one of the following
  184. * negative error codes:
  185. *
  186. * NGHTTP2_ERR_NOMEM
  187. * Out of memory.
  188. */
  189. int nghttp2_bufs_wrap_init2(nghttp2_bufs *bufs, const nghttp2_vec *vec,
  190. size_t veclen, nghttp2_mem *mem);
  191. /*
  192. * Frees any related resource to the |bufs|. This function does not
  193. * free supplied buffer provided in nghttp2_bufs_wrap_init().
  194. */
  195. void nghttp2_bufs_wrap_free(nghttp2_bufs *bufs);
  196. /*
  197. * Reallocates internal buffer using |chunk_length|. The max_chunk,
  198. * chunk_keep and offset do not change. After successful allocation
  199. * of new buffer, previous buffers are deallocated without copying
  200. * anything into new buffers. chunk_used is reset to 1.
  201. *
  202. * This function returns 0 if it succeeds, or one of the following
  203. * negative error codes:
  204. *
  205. * NGHTTP2_ERR_NOMEM
  206. * Out of memory.
  207. * NGHTTP2_ERR_INVALID_ARGUMENT
  208. * chunk_length < offset
  209. */
  210. int nghttp2_bufs_realloc(nghttp2_bufs *bufs, size_t chunk_length);
  211. /*
  212. * Appends the |data| of length |len| to the |bufs|. The write starts
  213. * at bufs->cur->buf.last. A new buffers will be allocated to store
  214. * all data.
  215. *
  216. * This function returns 0 if it succeeds, or one of the following
  217. * negative error codes:
  218. *
  219. * NGHTTP2_ERR_NOMEM
  220. * Out of memory.
  221. * NGHTTP2_ERR_BUFFER_ERROR
  222. * Out of buffer space.
  223. */
  224. int nghttp2_bufs_add(nghttp2_bufs *bufs, const void *data, size_t len);
  225. /*
  226. * Appends a single byte |b| to the |bufs|. The write starts at
  227. * bufs->cur->buf.last. A new buffers will be allocated to store all
  228. * data.
  229. *
  230. * This function returns 0 if it succeeds, or one of the following
  231. * negative error codes:
  232. *
  233. * NGHTTP2_ERR_NOMEM
  234. * Out of memory.
  235. * NGHTTP2_ERR_BUFFER_ERROR
  236. * Out of buffer space.
  237. */
  238. int nghttp2_bufs_addb(nghttp2_bufs *bufs, uint8_t b);
  239. /*
  240. * Behaves like nghttp2_bufs_addb(), but this does not update
  241. * buf->last pointer.
  242. */
  243. int nghttp2_bufs_addb_hold(nghttp2_bufs *bufs, uint8_t b);
  244. #define nghttp2_bufs_fast_addb(BUFS, B) \
  245. do { \
  246. *(BUFS)->cur->buf.last++ = B; \
  247. } while (0)
  248. #define nghttp2_bufs_fast_addb_hold(BUFS, B) \
  249. do { \
  250. *(BUFS)->cur->buf.last = B; \
  251. } while (0)
  252. /*
  253. * Performs bitwise-OR of |b| at bufs->cur->buf.last. A new buffers
  254. * will be allocated if necessary.
  255. *
  256. * This function returns 0 if it succeeds, or one of the following
  257. * negative error codes:
  258. *
  259. * NGHTTP2_ERR_NOMEM
  260. * Out of memory.
  261. * NGHTTP2_ERR_BUFFER_ERROR
  262. * Out of buffer space.
  263. */
  264. int nghttp2_bufs_orb(nghttp2_bufs *bufs, uint8_t b);
  265. /*
  266. * Behaves like nghttp2_bufs_orb(), but does not update buf->last
  267. * pointer.
  268. */
  269. int nghttp2_bufs_orb_hold(nghttp2_bufs *bufs, uint8_t b);
  270. #define nghttp2_bufs_fast_orb(BUFS, B) \
  271. do { \
  272. uint8_t **p = &(BUFS)->cur->buf.last; \
  273. **p = (uint8_t)(**p | (B)); \
  274. ++(*p); \
  275. } while (0)
  276. #define nghttp2_bufs_fast_orb_hold(BUFS, B) \
  277. do { \
  278. uint8_t *p = (BUFS)->cur->buf.last; \
  279. *p = (uint8_t)(*p | (B)); \
  280. } while (0)
  281. /*
  282. * Copies all data stored in |bufs| to the contiguous buffer. This
  283. * function allocates the contiguous memory to store all data in
  284. * |bufs| and assigns it to |*out|.
  285. *
  286. * The contents of |bufs| is left unchanged.
  287. *
  288. * This function returns the length of copied data and assigns the
  289. * pointer to copied data to |*out| if it succeeds, or one of the
  290. * following negative error codes:
  291. *
  292. * NGHTTP2_ERR_NOMEM
  293. * Out of memory
  294. */
  295. ssize_t nghttp2_bufs_remove(nghttp2_bufs *bufs, uint8_t **out);
  296. /*
  297. * Copies all data stored in |bufs| to |out|. This function assumes
  298. * that the buffer space pointed by |out| has at least
  299. * nghttp2_bufs(bufs) bytes.
  300. *
  301. * The contents of |bufs| is left unchanged.
  302. *
  303. * This function returns the length of copied data.
  304. */
  305. size_t nghttp2_bufs_remove_copy(nghttp2_bufs *bufs, uint8_t *out);
  306. /*
  307. * Resets |bufs| and makes the buffers empty.
  308. */
  309. void nghttp2_bufs_reset(nghttp2_bufs *bufs);
  310. /*
  311. * Moves bufs->cur to bufs->cur->next. If resulting bufs->cur is
  312. * NULL, this function allocates new buffers and bufs->cur points to
  313. * it.
  314. *
  315. * This function returns 0 if it succeeds, or one of the following
  316. * negative error codes:
  317. *
  318. * NGHTTP2_ERR_NOMEM
  319. * Out of memory
  320. * NGHTTP2_ERR_BUFFER_ERROR
  321. * Out of buffer space.
  322. */
  323. int nghttp2_bufs_advance(nghttp2_bufs *bufs);
  324. /* Sets bufs->cur to bufs->head */
  325. #define nghttp2_bufs_rewind(BUFS) \
  326. do { \
  327. (BUFS)->cur = (BUFS)->head; \
  328. } while (0)
  329. /*
  330. * Move bufs->cur, from the current position, using next member, to
  331. * the last buf which has nghttp2_buf_len(buf) > 0 without seeing buf
  332. * which satisfies nghttp2_buf_len(buf) == 0. If
  333. * nghttp2_buf_len(&bufs->cur->buf) == 0 or bufs->cur->next is NULL,
  334. * bufs->cur is unchanged.
  335. */
  336. void nghttp2_bufs_seek_last_present(nghttp2_bufs *bufs);
  337. /*
  338. * Returns nonzero if bufs->cur->next is not empty.
  339. */
  340. int nghttp2_bufs_next_present(nghttp2_bufs *bufs);
  341. #define nghttp2_bufs_cur_avail(BUFS) nghttp2_buf_avail(&(BUFS)->cur->buf)
  342. /*
  343. * Returns the total buffer length of |bufs|.
  344. */
  345. size_t nghttp2_bufs_len(nghttp2_bufs *bufs);
  346. #endif /* NGHTTP2_BUF_H */