ngtcp2_pq.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * ngtcp2
  3. *
  4. * Copyright (c) 2017 ngtcp2 contributors
  5. * Copyright (c) 2012 nghttp2 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. #ifndef NGTCP2_PQ_H
  27. #define NGTCP2_PQ_H
  28. #ifdef HAVE_CONFIG_H
  29. # include <config.h>
  30. #endif /* defined(HAVE_CONFIG_H) */
  31. #include <ngtcp2/ngtcp2.h>
  32. #include "ngtcp2_mem.h"
  33. /* Implementation of priority queue */
  34. /* NGTCP2_PQ_BAD_INDEX is the priority queue index which indicates
  35. that an entry is not queued. Assigning this value to
  36. ngtcp2_pq_entry.index can check that the entry is queued or not. */
  37. #define NGTCP2_PQ_BAD_INDEX SIZE_MAX
  38. typedef struct ngtcp2_pq_entry {
  39. size_t index;
  40. } ngtcp2_pq_entry;
  41. /* ngtcp2_pq_less is a "less" function, that returns nonzero if |lhs|
  42. is considered to be less than |rhs|. */
  43. typedef int (*ngtcp2_pq_less)(const ngtcp2_pq_entry *lhs,
  44. const ngtcp2_pq_entry *rhs);
  45. typedef struct ngtcp2_pq {
  46. /* q is a pointer to an array that stores the items. */
  47. ngtcp2_pq_entry **q;
  48. /* mem is a memory allocator. */
  49. const ngtcp2_mem *mem;
  50. /* length is the number of items stored. */
  51. size_t length;
  52. /* capacity is the maximum number of items this queue can store.
  53. This is automatically extended when length is reached to this
  54. limit. */
  55. size_t capacity;
  56. /* less is the less function to compare items. */
  57. ngtcp2_pq_less less;
  58. } ngtcp2_pq;
  59. /*
  60. * ngtcp2_pq_init initializes |pq| with compare function |cmp|.
  61. */
  62. void ngtcp2_pq_init(ngtcp2_pq *pq, ngtcp2_pq_less less, const ngtcp2_mem *mem);
  63. /*
  64. * ngtcp2_pq_free deallocates any resources allocated for |pq|. The
  65. * stored items are not freed by this function.
  66. */
  67. void ngtcp2_pq_free(ngtcp2_pq *pq);
  68. /*
  69. * ngtcp2_pq_push adds |item| to |pq|.
  70. *
  71. * This function returns 0 if it succeeds, or one of the following
  72. * negative error codes:
  73. *
  74. * NGTCP2_ERR_NOMEM
  75. * Out of memory.
  76. */
  77. int ngtcp2_pq_push(ngtcp2_pq *pq, ngtcp2_pq_entry *item);
  78. /*
  79. * ngtcp2_pq_top returns item at the top of |pq|. It is undefined if
  80. * |pq| is empty.
  81. */
  82. ngtcp2_pq_entry *ngtcp2_pq_top(const ngtcp2_pq *pq);
  83. /*
  84. * ngtcp2_pq_pop pops item at the top of |pq|. The popped item is not
  85. * freed by this function. It is undefined if |pq| is empty.
  86. */
  87. void ngtcp2_pq_pop(ngtcp2_pq *pq);
  88. /*
  89. * ngtcp2_pq_empty returns nonzero if |pq| is empty.
  90. */
  91. int ngtcp2_pq_empty(const ngtcp2_pq *pq);
  92. /*
  93. * ngtcp2_pq_size returns the number of items |pq| contains.
  94. */
  95. size_t ngtcp2_pq_size(const ngtcp2_pq *pq);
  96. typedef int (*ngtcp2_pq_item_cb)(ngtcp2_pq_entry *item, void *arg);
  97. /*
  98. * ngtcp2_pq_each applies |fun| to each item in |pq|. The |arg| is
  99. * passed as arg parameter to callback function. This function must
  100. * not change the ordering key. If the return value from callback is
  101. * nonzero, this function returns 1 immediately without iterating
  102. * remaining items. Otherwise this function returns 0.
  103. */
  104. int ngtcp2_pq_each(const ngtcp2_pq *pq, ngtcp2_pq_item_cb fun, void *arg);
  105. /*
  106. * ngtcp2_pq_remove removes |item| from |pq|. |pq| must contain
  107. * |item| otherwise the behavior is undefined.
  108. */
  109. void ngtcp2_pq_remove(ngtcp2_pq *pq, ngtcp2_pq_entry *item);
  110. #endif /* !defined(NGTCP2_PQ_H) */