ngtcp2_rob.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_ROB_H
  26. #define NGTCP2_ROB_H
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif /* defined(HAVE_CONFIG_H) */
  30. #include <ngtcp2/ngtcp2.h>
  31. #include "ngtcp2_mem.h"
  32. #include "ngtcp2_range.h"
  33. #include "ngtcp2_ksl.h"
  34. /*
  35. * ngtcp2_rob_gap represents the gap, which is the range of stream
  36. * data that is not received yet.
  37. */
  38. typedef struct ngtcp2_rob_gap {
  39. /* range is the range of this gap. */
  40. ngtcp2_range range;
  41. } ngtcp2_rob_gap;
  42. /*
  43. * ngtcp2_rob_gap_new allocates new ngtcp2_rob_gap object, and assigns
  44. * its pointer to |*pg|. The caller should call ngtcp2_rob_gap_del to
  45. * delete it when it is no longer used. The range of the gap is
  46. * [begin, end). |mem| is custom memory allocator to allocate memory.
  47. *
  48. * This function returns 0 if it succeeds, or one of the following
  49. * negative error codes:
  50. *
  51. * NGTCP2_ERR_NOMEM
  52. * Out of memory.
  53. */
  54. int ngtcp2_rob_gap_new(ngtcp2_rob_gap **pg, uint64_t begin, uint64_t end,
  55. const ngtcp2_mem *mem);
  56. /*
  57. * ngtcp2_rob_gap_del deallocates |g|. It deallocates the memory
  58. * pointed by |g| it self. |mem| is custom memory allocator to
  59. * deallocate memory.
  60. */
  61. void ngtcp2_rob_gap_del(ngtcp2_rob_gap *g, const ngtcp2_mem *mem);
  62. /*
  63. * ngtcp2_rob_data holds the buffered stream data.
  64. */
  65. typedef struct ngtcp2_rob_data {
  66. /* range is the range of this data. */
  67. ngtcp2_range range;
  68. /* begin points to the buffer. */
  69. uint8_t *begin;
  70. } ngtcp2_rob_data;
  71. /*
  72. * ngtcp2_rob_data_new allocates new ngtcp2_rob_data object, and
  73. * assigns its pointer to |*pd|. The caller should call
  74. * ngtcp2_rob_data_del to delete it when it is no longer used.
  75. * |offset| is the stream offset of the first byte of this data.
  76. * |chunk| is the size of the buffer. |offset| must be multiple of
  77. * |chunk|. |mem| is custom memory allocator to allocate memory.
  78. *
  79. * This function returns 0 if it succeeds, or one of the following
  80. * negative error codes:
  81. *
  82. * NGTCP2_ERR_NOMEM
  83. * Out of memory.
  84. */
  85. int ngtcp2_rob_data_new(ngtcp2_rob_data **pd, uint64_t offset, size_t chunk,
  86. const ngtcp2_mem *mem);
  87. /*
  88. * ngtcp2_rob_data_del deallocates |d|. It deallocates the memory
  89. * pointed by |d| itself. |mem| is custom memory allocator to
  90. * deallocate memory.
  91. */
  92. void ngtcp2_rob_data_del(ngtcp2_rob_data *d, const ngtcp2_mem *mem);
  93. /*
  94. * ngtcp2_rob is the reorder buffer which reassembles stream data
  95. * received in out of order.
  96. */
  97. typedef struct ngtcp2_rob {
  98. /* gapksl maintains the range of offset which is not received
  99. yet. Initially, its range is [0, UINT64_MAX). */
  100. ngtcp2_ksl gapksl;
  101. /* dataksl maintains the buffers which store received out-of-order
  102. data ordered by stream offset. */
  103. ngtcp2_ksl dataksl;
  104. /* mem is custom memory allocator */
  105. const ngtcp2_mem *mem;
  106. /* chunk is the size of each buffer in data field */
  107. size_t chunk;
  108. } ngtcp2_rob;
  109. /*
  110. * ngtcp2_rob_init initializes |rob|. |chunk| is the size of buffer
  111. * per chunk.
  112. *
  113. * This function returns 0 if it succeeds, or one of the following
  114. * negative error codes:
  115. *
  116. * NGTCP2_ERR_NOMEM
  117. * Out of memory.
  118. */
  119. int ngtcp2_rob_init(ngtcp2_rob *rob, size_t chunk, const ngtcp2_mem *mem);
  120. /*
  121. * ngtcp2_rob_free frees resources allocated for |rob|.
  122. */
  123. void ngtcp2_rob_free(ngtcp2_rob *rob);
  124. /*
  125. * ngtcp2_rob_push adds new data pointed by |data| of length |datalen|
  126. * at the stream offset |offset|.
  127. *
  128. * This function returns 0 if it succeeds, or one of the following
  129. * negative error codes:
  130. *
  131. * NGTCP2_ERR_NOMEM
  132. * Out of memory
  133. */
  134. int ngtcp2_rob_push(ngtcp2_rob *rob, uint64_t offset, const uint8_t *data,
  135. size_t datalen);
  136. /*
  137. * ngtcp2_rob_remove_prefix removes gap up to |offset|, exclusive. It
  138. * also removes buffered data if it is completely included in
  139. * |offset|.
  140. */
  141. void ngtcp2_rob_remove_prefix(ngtcp2_rob *rob, uint64_t offset);
  142. /*
  143. * ngtcp2_rob_data_at stores the pointer to the buffer of stream
  144. * offset |offset| to |*pdest| if it is available, and returns the
  145. * valid length of available data. If no data is available, it
  146. * returns 0. This function only returns the data before the first
  147. * gap. It returns 0 even if data is available after the first gap.
  148. */
  149. size_t ngtcp2_rob_data_at(const ngtcp2_rob *rob, const uint8_t **pdest,
  150. uint64_t offset);
  151. /*
  152. * ngtcp2_rob_pop clears data at stream offset |offset| of length
  153. * |len|.
  154. *
  155. * |offset| must be the offset given in ngtcp2_rob_data_at. |len|
  156. * must be the return value of ngtcp2_rob_data_at when |offset| is
  157. * passed.
  158. *
  159. * Caller should call this function from offset 0 in non-decreasing
  160. * order.
  161. */
  162. void ngtcp2_rob_pop(ngtcp2_rob *rob, uint64_t offset, size_t len);
  163. /*
  164. * ngtcp2_rob_first_gap_offset returns the offset to the first gap.
  165. * If there is no gap, it returns UINT64_MAX.
  166. */
  167. uint64_t ngtcp2_rob_first_gap_offset(const ngtcp2_rob *rob);
  168. /*
  169. * ngtcp2_rob_data_buffered returns nonzero if any data is buffered.
  170. */
  171. int ngtcp2_rob_data_buffered(const ngtcp2_rob *rob);
  172. #endif /* !defined(NGTCP2_ROB_H) */