nghttp3_gaptr.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * nghttp3
  3. *
  4. * Copyright (c) 2019 nghttp3 contributors
  5. * Copyright (c) 2017 ngtcp2 contributors
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the
  9. * "Software"), to deal in the Software without restriction, including
  10. * without limitation the rights to use, copy, modify, merge, publish,
  11. * distribute, sublicense, and/or sell copies of the Software, and to
  12. * permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be
  16. * included in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  19. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  20. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  21. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  22. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  23. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  24. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. #include "nghttp3_gaptr.h"
  27. #include <string.h>
  28. #include <assert.h>
  29. void nghttp3_gaptr_init(nghttp3_gaptr *gaptr, const nghttp3_mem *mem) {
  30. nghttp3_ksl_init(&gaptr->gap, nghttp3_ksl_range_compar,
  31. nghttp3_ksl_range_search, sizeof(nghttp3_range), mem);
  32. gaptr->mem = mem;
  33. }
  34. static int gaptr_gap_init(nghttp3_gaptr *gaptr) {
  35. nghttp3_range range = {0, UINT64_MAX};
  36. return nghttp3_ksl_insert(&gaptr->gap, NULL, &range, NULL);
  37. }
  38. void nghttp3_gaptr_free(nghttp3_gaptr *gaptr) {
  39. if (gaptr == NULL) {
  40. return;
  41. }
  42. nghttp3_ksl_free(&gaptr->gap);
  43. }
  44. int nghttp3_gaptr_push(nghttp3_gaptr *gaptr, uint64_t offset,
  45. uint64_t datalen) {
  46. int rv;
  47. nghttp3_range k, m, l, r, q = {offset, offset + datalen};
  48. nghttp3_ksl_it it;
  49. if (nghttp3_ksl_len(&gaptr->gap) == 0) {
  50. rv = gaptr_gap_init(gaptr);
  51. if (rv != 0) {
  52. return rv;
  53. }
  54. }
  55. it = nghttp3_ksl_lower_bound_search(&gaptr->gap, &q,
  56. nghttp3_ksl_range_exclusive_search);
  57. for (; !nghttp3_ksl_it_end(&it);) {
  58. k = *(nghttp3_range *)nghttp3_ksl_it_key(&it);
  59. m = nghttp3_range_intersect(&q, &k);
  60. if (!nghttp3_range_len(&m)) {
  61. break;
  62. }
  63. if (nghttp3_range_eq(&k, &m)) {
  64. nghttp3_ksl_remove_hint(&gaptr->gap, &it, &it, &k);
  65. continue;
  66. }
  67. nghttp3_range_cut(&l, &r, &k, &m);
  68. if (nghttp3_range_len(&l)) {
  69. nghttp3_ksl_update_key(&gaptr->gap, &k, &l);
  70. if (nghttp3_range_len(&r)) {
  71. rv = nghttp3_ksl_insert(&gaptr->gap, &it, &r, NULL);
  72. if (rv != 0) {
  73. return rv;
  74. }
  75. }
  76. } else if (nghttp3_range_len(&r)) {
  77. nghttp3_ksl_update_key(&gaptr->gap, &k, &r);
  78. }
  79. nghttp3_ksl_it_next(&it);
  80. }
  81. return 0;
  82. }
  83. uint64_t nghttp3_gaptr_first_gap_offset(nghttp3_gaptr *gaptr) {
  84. nghttp3_ksl_it it;
  85. if (nghttp3_ksl_len(&gaptr->gap) == 0) {
  86. return 0;
  87. }
  88. it = nghttp3_ksl_begin(&gaptr->gap);
  89. return ((nghttp3_range *)nghttp3_ksl_it_key(&it))->begin;
  90. }
  91. nghttp3_range nghttp3_gaptr_get_first_gap_after(nghttp3_gaptr *gaptr,
  92. uint64_t offset) {
  93. nghttp3_range q = {offset, offset + 1};
  94. nghttp3_ksl_it it;
  95. if (nghttp3_ksl_len(&gaptr->gap) == 0) {
  96. nghttp3_range r = {0, UINT64_MAX};
  97. return r;
  98. }
  99. it = nghttp3_ksl_lower_bound_search(&gaptr->gap, &q,
  100. nghttp3_ksl_range_exclusive_search);
  101. assert(!nghttp3_ksl_it_end(&it));
  102. return *(nghttp3_range *)nghttp3_ksl_it_key(&it);
  103. }
  104. int nghttp3_gaptr_is_pushed(nghttp3_gaptr *gaptr, uint64_t offset,
  105. uint64_t datalen) {
  106. nghttp3_range q = {offset, offset + datalen};
  107. nghttp3_ksl_it it;
  108. nghttp3_range m;
  109. if (nghttp3_ksl_len(&gaptr->gap) == 0) {
  110. return 0;
  111. }
  112. it = nghttp3_ksl_lower_bound_search(&gaptr->gap, &q,
  113. nghttp3_ksl_range_exclusive_search);
  114. m = nghttp3_range_intersect(&q, (nghttp3_range *)nghttp3_ksl_it_key(&it));
  115. return nghttp3_range_len(&m) == 0;
  116. }
  117. void nghttp3_gaptr_drop_first_gap(nghttp3_gaptr *gaptr) {
  118. nghttp3_ksl_it it;
  119. nghttp3_range r;
  120. if (nghttp3_ksl_len(&gaptr->gap) == 0) {
  121. return;
  122. }
  123. it = nghttp3_ksl_begin(&gaptr->gap);
  124. assert(!nghttp3_ksl_it_end(&it));
  125. r = *(nghttp3_range *)nghttp3_ksl_it_key(&it);
  126. nghttp3_ksl_remove_hint(&gaptr->gap, NULL, &it, &r);
  127. }