ngtcp2_gaptr.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #include "ngtcp2_gaptr.h"
  26. #include <string.h>
  27. #include <assert.h>
  28. void ngtcp2_gaptr_init(ngtcp2_gaptr *gaptr, const ngtcp2_mem *mem) {
  29. ngtcp2_ksl_init(&gaptr->gap, ngtcp2_ksl_range_compar, ngtcp2_ksl_range_search,
  30. sizeof(ngtcp2_range), mem);
  31. gaptr->mem = mem;
  32. }
  33. static int gaptr_gap_init(ngtcp2_gaptr *gaptr) {
  34. ngtcp2_range range = {0, UINT64_MAX};
  35. return ngtcp2_ksl_insert(&gaptr->gap, NULL, &range, NULL);
  36. }
  37. void ngtcp2_gaptr_free(ngtcp2_gaptr *gaptr) {
  38. if (gaptr == NULL) {
  39. return;
  40. }
  41. ngtcp2_ksl_free(&gaptr->gap);
  42. }
  43. int ngtcp2_gaptr_push(ngtcp2_gaptr *gaptr, uint64_t offset, uint64_t datalen) {
  44. int rv;
  45. ngtcp2_range k, m, l, r, q = {offset, offset + datalen};
  46. ngtcp2_ksl_it it;
  47. if (ngtcp2_ksl_len(&gaptr->gap) == 0) {
  48. rv = gaptr_gap_init(gaptr);
  49. if (rv != 0) {
  50. return rv;
  51. }
  52. }
  53. it = ngtcp2_ksl_lower_bound_search(&gaptr->gap, &q,
  54. ngtcp2_ksl_range_exclusive_search);
  55. for (; !ngtcp2_ksl_it_end(&it);) {
  56. k = *(ngtcp2_range *)ngtcp2_ksl_it_key(&it);
  57. m = ngtcp2_range_intersect(&q, &k);
  58. if (!ngtcp2_range_len(&m)) {
  59. break;
  60. }
  61. if (ngtcp2_range_eq(&k, &m)) {
  62. ngtcp2_ksl_remove_hint(&gaptr->gap, &it, &it, &k);
  63. continue;
  64. }
  65. ngtcp2_range_cut(&l, &r, &k, &m);
  66. if (ngtcp2_range_len(&l)) {
  67. ngtcp2_ksl_update_key(&gaptr->gap, &k, &l);
  68. if (ngtcp2_range_len(&r)) {
  69. rv = ngtcp2_ksl_insert(&gaptr->gap, &it, &r, NULL);
  70. if (rv != 0) {
  71. return rv;
  72. }
  73. }
  74. } else if (ngtcp2_range_len(&r)) {
  75. ngtcp2_ksl_update_key(&gaptr->gap, &k, &r);
  76. }
  77. ngtcp2_ksl_it_next(&it);
  78. }
  79. return 0;
  80. }
  81. uint64_t ngtcp2_gaptr_first_gap_offset(const ngtcp2_gaptr *gaptr) {
  82. ngtcp2_ksl_it it;
  83. if (ngtcp2_ksl_len(&gaptr->gap) == 0) {
  84. return 0;
  85. }
  86. it = ngtcp2_ksl_begin(&gaptr->gap);
  87. return ((ngtcp2_range *)ngtcp2_ksl_it_key(&it))->begin;
  88. }
  89. ngtcp2_range ngtcp2_gaptr_get_first_gap_after(const ngtcp2_gaptr *gaptr,
  90. uint64_t offset) {
  91. ngtcp2_range q = {offset, offset + 1};
  92. ngtcp2_ksl_it it;
  93. if (ngtcp2_ksl_len(&gaptr->gap) == 0) {
  94. ngtcp2_range r = {0, UINT64_MAX};
  95. return r;
  96. }
  97. it = ngtcp2_ksl_lower_bound_search(&gaptr->gap, &q,
  98. ngtcp2_ksl_range_exclusive_search);
  99. assert(!ngtcp2_ksl_it_end(&it));
  100. return *(ngtcp2_range *)ngtcp2_ksl_it_key(&it);
  101. }
  102. int ngtcp2_gaptr_is_pushed(const ngtcp2_gaptr *gaptr, uint64_t offset,
  103. uint64_t datalen) {
  104. ngtcp2_range q = {offset, offset + datalen};
  105. ngtcp2_ksl_it it;
  106. ngtcp2_range m;
  107. if (ngtcp2_ksl_len(&gaptr->gap) == 0) {
  108. return 0;
  109. }
  110. it = ngtcp2_ksl_lower_bound_search(&gaptr->gap, &q,
  111. ngtcp2_ksl_range_exclusive_search);
  112. m = ngtcp2_range_intersect(&q, (ngtcp2_range *)ngtcp2_ksl_it_key(&it));
  113. return ngtcp2_range_len(&m) == 0;
  114. }
  115. void ngtcp2_gaptr_drop_first_gap(ngtcp2_gaptr *gaptr) {
  116. ngtcp2_ksl_it it;
  117. ngtcp2_range r;
  118. if (ngtcp2_ksl_len(&gaptr->gap) == 0) {
  119. return;
  120. }
  121. it = ngtcp2_ksl_begin(&gaptr->gap);
  122. assert(!ngtcp2_ksl_it_end(&it));
  123. r = *(ngtcp2_range *)ngtcp2_ksl_it_key(&it);
  124. ngtcp2_ksl_remove_hint(&gaptr->gap, NULL, &it, &r);
  125. }