ngtcp2_buf.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2017 ngtcp2 contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef NGTCP2_BUF_H
  26. #define NGTCP2_BUF_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <ngtcp2/ngtcp2.h>
  31. typedef struct ngtcp2_buf {
  32. /* begin points to the beginning of the buffer. */
  33. uint8_t *begin;
  34. /* end points to the one beyond of the last byte of the buffer */
  35. uint8_t *end;
  36. /* pos points to the start of data. Typically, this points to the
  37. point that next data should be read. Initially, it points to
  38. |begin|. */
  39. uint8_t *pos;
  40. /* last points to the one beyond of the last data of the buffer.
  41. Typically, new data is written at this point. Initially, it
  42. points to |begin|. */
  43. uint8_t *last;
  44. } ngtcp2_buf;
  45. /*
  46. * ngtcp2_buf_init initializes |buf| with the given buffer.
  47. */
  48. void ngtcp2_buf_init(ngtcp2_buf *buf, uint8_t *begin, size_t len);
  49. /*
  50. * ngtcp2_buf_reset resets pos and last fields to match begin field to
  51. * make ngtcp2_buf_len(buf) return 0.
  52. */
  53. void ngtcp2_buf_reset(ngtcp2_buf *buf);
  54. /*
  55. * ngtcp2_buf_left returns the number of additional bytes which can be
  56. * written to the underlying buffer. In other words, it returns
  57. * buf->end - buf->last.
  58. */
  59. #define ngtcp2_buf_left(BUF) (size_t)((BUF)->end - (BUF)->last)
  60. /*
  61. * ngtcp2_buf_len returns the number of bytes left to read. In other
  62. * words, it returns buf->last - buf->pos.
  63. */
  64. #define ngtcp2_buf_len(BUF) (size_t)((BUF)->last - (BUF)->pos)
  65. /*
  66. * ngtcp2_buf_cap returns the capacity of the buffer. In other words,
  67. * it returns buf->end - buf->begin.
  68. */
  69. size_t ngtcp2_buf_cap(const ngtcp2_buf *buf);
  70. /*
  71. * ngtcp2_buf_chain is a linked list of ngtcp2_buf.
  72. */
  73. typedef struct ngtcp2_buf_chain ngtcp2_buf_chain;
  74. struct ngtcp2_buf_chain {
  75. ngtcp2_buf_chain *next;
  76. ngtcp2_buf buf;
  77. };
  78. /*
  79. * ngtcp2_buf_chain_new creates new ngtcp2_buf_chain and initializes
  80. * the internal buffer with |len| bytes space.
  81. *
  82. * This function returns 0 if it succeeds, or one of the following
  83. * negative error codes:
  84. *
  85. * NGTCP2_ERR_NOMEM
  86. * Out of memory
  87. */
  88. int ngtcp2_buf_chain_new(ngtcp2_buf_chain **pbufchain, size_t len,
  89. const ngtcp2_mem *mem);
  90. /*
  91. * ngtcp2_buf_chain_del deletes the resource allocated by |bufchain|.
  92. * It also deletes the memory pointed by |bufchain|.
  93. */
  94. void ngtcp2_buf_chain_del(ngtcp2_buf_chain *bufchain, const ngtcp2_mem *mem);
  95. #endif /* !defined(NGTCP2_BUF_H) */