nghttp3_pq.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * nghttp3
  3. *
  4. * Copyright (c) 2019 nghttp3 contributors
  5. * Copyright (c) 2017 ngtcp2 contributors
  6. * Copyright (c) 2012 nghttp2 contributors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files (the
  10. * "Software"), to deal in the Software without restriction, including
  11. * without limitation the rights to use, copy, modify, merge, publish,
  12. * distribute, sublicense, and/or sell copies of the Software, and to
  13. * permit persons to whom the Software is furnished to do so, subject to
  14. * the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. #ifndef NGHTTP3_PQ_H
  28. #define NGHTTP3_PQ_H
  29. #ifdef HAVE_CONFIG_H
  30. # include <config.h>
  31. #endif /* defined(HAVE_CONFIG_H) */
  32. #include <nghttp3/nghttp3.h>
  33. #include "nghttp3_mem.h"
  34. /* Implementation of priority queue */
  35. /* NGHTTP3_PQ_BAD_INDEX is the priority queue index which indicates
  36. that an entry is not queued. Assigning this value to
  37. nghttp3_pq_entry.index can check that the entry is queued or
  38. not. */
  39. #define NGHTTP3_PQ_BAD_INDEX SIZE_MAX
  40. typedef struct nghttp3_pq_entry {
  41. size_t index;
  42. } nghttp3_pq_entry;
  43. /* nghttp3_pq_less is a "less" function, that returns nonzero if |lhs|
  44. is considered to be less than |rhs|. */
  45. typedef int (*nghttp3_pq_less)(const nghttp3_pq_entry *lhs,
  46. const nghttp3_pq_entry *rhs);
  47. typedef struct nghttp3_pq {
  48. /* q is a pointer to an array that stores the items. */
  49. nghttp3_pq_entry **q;
  50. /* mem is a memory allocator. */
  51. const nghttp3_mem *mem;
  52. /* length is the number of items stored. */
  53. size_t length;
  54. /* capacity is the maximum number of items this queue can store.
  55. This is automatically extended when length is reached to this
  56. limit. */
  57. size_t capacity;
  58. /* less is the less function to compare items. */
  59. nghttp3_pq_less less;
  60. } nghttp3_pq;
  61. /*
  62. * nghttp3_pq_init initializes |pq| with compare function |cmp|.
  63. */
  64. void nghttp3_pq_init(nghttp3_pq *pq, nghttp3_pq_less less,
  65. const nghttp3_mem *mem);
  66. /*
  67. * nghttp3_pq_free deallocates any resources allocated for |pq|. The
  68. * stored items are not freed by this function.
  69. */
  70. void nghttp3_pq_free(nghttp3_pq *pq);
  71. /*
  72. * nghttp3_pq_push adds |item| to |pq|.
  73. *
  74. * This function returns 0 if it succeeds, or one of the following
  75. * negative error codes:
  76. *
  77. * NGHTTP3_ERR_NOMEM
  78. * Out of memory.
  79. */
  80. int nghttp3_pq_push(nghttp3_pq *pq, nghttp3_pq_entry *item);
  81. /*
  82. * nghttp3_pq_top returns item at the top of |pq|. It is undefined if
  83. * |pq| is empty.
  84. */
  85. nghttp3_pq_entry *nghttp3_pq_top(const nghttp3_pq *pq);
  86. /*
  87. * nghttp3_pq_pop pops item at the top of |pq|. The popped item is
  88. * not freed by this function. It is undefined if |pq| is empty.
  89. */
  90. void nghttp3_pq_pop(nghttp3_pq *pq);
  91. /*
  92. * nghttp3_pq_empty returns nonzero if |pq| is empty.
  93. */
  94. int nghttp3_pq_empty(const nghttp3_pq *pq);
  95. /*
  96. * nghttp3_pq_size returns the number of items |pq| contains.
  97. */
  98. size_t nghttp3_pq_size(const nghttp3_pq *pq);
  99. typedef int (*nghttp3_pq_item_cb)(nghttp3_pq_entry *item, void *arg);
  100. /*
  101. * nghttp3_pq_each applies |fun| to each item in |pq|. The |arg| is
  102. * passed as arg parameter to callback function. This function must
  103. * not change the ordering key. If the return value from callback is
  104. * nonzero, this function returns 1 immediately without iterating
  105. * remaining items. Otherwise this function returns 0.
  106. */
  107. int nghttp3_pq_each(const nghttp3_pq *pq, nghttp3_pq_item_cb fun, void *arg);
  108. /*
  109. * nghttp3_pq_remove removes |item| from |pq|. |pq| must contain
  110. * |item| otherwise the behavior is undefined.
  111. */
  112. void nghttp3_pq_remove(nghttp3_pq *pq, nghttp3_pq_entry *item);
  113. /*
  114. * nghttp3_pq_clear removes all items from |pq|.
  115. */
  116. void nghttp3_pq_clear(nghttp3_pq *pq);
  117. #endif /* !defined(NGHTTP3_PQ_H) */