1
0

nghttp2_pq.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * nghttp2 - HTTP/2 C Library
  3. *
  4. * Copyright (c) 2012 Tatsuhiro Tsujikawa
  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 "nghttp2_pq.h"
  26. #include <stdio.h>
  27. #include <assert.h>
  28. #include "nghttp2_helper.h"
  29. int nghttp2_pq_init(nghttp2_pq *pq, nghttp2_less less, nghttp2_mem *mem) {
  30. pq->mem = mem;
  31. pq->capacity = 0;
  32. pq->q = NULL;
  33. pq->length = 0;
  34. pq->less = less;
  35. return 0;
  36. }
  37. void nghttp2_pq_free(nghttp2_pq *pq) {
  38. nghttp2_mem_free(pq->mem, pq->q);
  39. pq->q = NULL;
  40. }
  41. static void swap(nghttp2_pq *pq, size_t i, size_t j) {
  42. nghttp2_pq_entry *a = pq->q[i];
  43. nghttp2_pq_entry *b = pq->q[j];
  44. pq->q[i] = b;
  45. b->index = i;
  46. pq->q[j] = a;
  47. a->index = j;
  48. }
  49. static void bubble_up(nghttp2_pq *pq, size_t index) {
  50. size_t parent;
  51. while (index != 0) {
  52. parent = (index - 1) / 2;
  53. if (!pq->less(pq->q[index], pq->q[parent])) {
  54. return;
  55. }
  56. swap(pq, parent, index);
  57. index = parent;
  58. }
  59. }
  60. int nghttp2_pq_push(nghttp2_pq *pq, nghttp2_pq_entry *item) {
  61. if (pq->capacity <= pq->length) {
  62. void *nq;
  63. size_t ncapacity;
  64. ncapacity = nghttp2_max(4, (pq->capacity * 2));
  65. nq = nghttp2_mem_realloc(pq->mem, pq->q,
  66. ncapacity * sizeof(nghttp2_pq_entry *));
  67. if (nq == NULL) {
  68. return NGHTTP2_ERR_NOMEM;
  69. }
  70. pq->capacity = ncapacity;
  71. pq->q = nq;
  72. }
  73. pq->q[pq->length] = item;
  74. item->index = pq->length;
  75. ++pq->length;
  76. bubble_up(pq, pq->length - 1);
  77. return 0;
  78. }
  79. nghttp2_pq_entry *nghttp2_pq_top(nghttp2_pq *pq) {
  80. if (pq->length == 0) {
  81. return NULL;
  82. } else {
  83. return pq->q[0];
  84. }
  85. }
  86. static void bubble_down(nghttp2_pq *pq, size_t index) {
  87. size_t i, j, minindex;
  88. for (;;) {
  89. j = index * 2 + 1;
  90. minindex = index;
  91. for (i = 0; i < 2; ++i, ++j) {
  92. if (j >= pq->length) {
  93. break;
  94. }
  95. if (pq->less(pq->q[j], pq->q[minindex])) {
  96. minindex = j;
  97. }
  98. }
  99. if (minindex == index) {
  100. return;
  101. }
  102. swap(pq, index, minindex);
  103. index = minindex;
  104. }
  105. }
  106. void nghttp2_pq_pop(nghttp2_pq *pq) {
  107. if (pq->length > 0) {
  108. pq->q[0] = pq->q[pq->length - 1];
  109. pq->q[0]->index = 0;
  110. --pq->length;
  111. bubble_down(pq, 0);
  112. }
  113. }
  114. void nghttp2_pq_remove(nghttp2_pq *pq, nghttp2_pq_entry *item) {
  115. assert(pq->q[item->index] == item);
  116. if (item->index == 0) {
  117. nghttp2_pq_pop(pq);
  118. return;
  119. }
  120. if (item->index == pq->length - 1) {
  121. --pq->length;
  122. return;
  123. }
  124. pq->q[item->index] = pq->q[pq->length - 1];
  125. pq->q[item->index]->index = item->index;
  126. --pq->length;
  127. if (pq->less(item, pq->q[item->index])) {
  128. bubble_down(pq, item->index);
  129. } else {
  130. bubble_up(pq, item->index);
  131. }
  132. }
  133. int nghttp2_pq_empty(nghttp2_pq *pq) { return pq->length == 0; }
  134. size_t nghttp2_pq_size(nghttp2_pq *pq) { return pq->length; }
  135. void nghttp2_pq_update(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg) {
  136. size_t i;
  137. int rv = 0;
  138. if (pq->length == 0) {
  139. return;
  140. }
  141. for (i = 0; i < pq->length; ++i) {
  142. rv |= (*fun)(pq->q[i], arg);
  143. }
  144. if (rv) {
  145. for (i = pq->length; i > 0; --i) {
  146. bubble_down(pq, i - 1);
  147. }
  148. }
  149. }
  150. int nghttp2_pq_each(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg) {
  151. size_t i;
  152. if (pq->length == 0) {
  153. return 0;
  154. }
  155. for (i = 0; i < pq->length; ++i) {
  156. if ((*fun)(pq->q[i], arg)) {
  157. return 1;
  158. }
  159. }
  160. return 0;
  161. }