nghttp2_pq.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. #ifndef NGHTTP2_PQ_H
  26. #define NGHTTP2_PQ_H
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif /* HAVE_CONFIG_H */
  30. #include <nghttp2/nghttp2.h>
  31. #include "nghttp2_int.h"
  32. #include "nghttp2_mem.h"
  33. /* Implementation of priority queue */
  34. typedef struct { size_t index; } nghttp2_pq_entry;
  35. typedef struct {
  36. /* The pointer to the pointer to the item stored */
  37. nghttp2_pq_entry **q;
  38. /* Memory allocator */
  39. nghttp2_mem *mem;
  40. /* The number of items sotred */
  41. size_t length;
  42. /* The maximum number of items this pq can store. This is
  43. automatically extended when length is reached to this value. */
  44. size_t capacity;
  45. /* The less function between items */
  46. nghttp2_less less;
  47. } nghttp2_pq;
  48. /*
  49. * Initializes priority queue |pq| with compare function |cmp|.
  50. *
  51. * This function returns 0 if it succeeds, or one of the following
  52. * negative error codes:
  53. *
  54. * NGHTTP2_ERR_NOMEM
  55. * Out of memory.
  56. */
  57. int nghttp2_pq_init(nghttp2_pq *pq, nghttp2_less less, nghttp2_mem *mem);
  58. /*
  59. * Deallocates any resources allocated for |pq|. The stored items are
  60. * not freed by this function.
  61. */
  62. void nghttp2_pq_free(nghttp2_pq *pq);
  63. /*
  64. * Adds |item| to the priority queue |pq|.
  65. *
  66. * This function returns 0 if it succeds, or one of the following
  67. * negative error codes:
  68. *
  69. * NGHTTP2_ERR_NOMEM
  70. * Out of memory.
  71. */
  72. int nghttp2_pq_push(nghttp2_pq *pq, nghttp2_pq_entry *item);
  73. /*
  74. * Returns item at the top of the queue |pq|. If the queue is empty,
  75. * this function returns NULL.
  76. */
  77. nghttp2_pq_entry *nghttp2_pq_top(nghttp2_pq *pq);
  78. /*
  79. * Pops item at the top of the queue |pq|. The popped item is not
  80. * freed by this function.
  81. */
  82. void nghttp2_pq_pop(nghttp2_pq *pq);
  83. /*
  84. * Returns nonzero if the queue |pq| is empty.
  85. */
  86. int nghttp2_pq_empty(nghttp2_pq *pq);
  87. /*
  88. * Returns the number of items in the queue |pq|.
  89. */
  90. size_t nghttp2_pq_size(nghttp2_pq *pq);
  91. typedef int (*nghttp2_pq_item_cb)(nghttp2_pq_entry *item, void *arg);
  92. /*
  93. * Updates each item in |pq| using function |fun| and re-construct
  94. * priority queue. The |fun| must return non-zero if it modifies the
  95. * item in a way that it affects ordering in the priority queue. The
  96. * |arg| is passed to the 2nd parameter of |fun|.
  97. */
  98. void nghttp2_pq_update(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg);
  99. /*
  100. * Applys |fun| to each item in |pq|. The |arg| is passed as arg
  101. * parameter to callback function. This function must not change the
  102. * ordering key. If the return value from callback is nonzero, this
  103. * function returns 1 immediately without iterating remaining items.
  104. * Otherwise this function returns 0.
  105. */
  106. int nghttp2_pq_each(nghttp2_pq *pq, nghttp2_pq_item_cb fun, void *arg);
  107. /*
  108. * Removes |item| from priority queue.
  109. */
  110. void nghttp2_pq_remove(nghttp2_pq *pq, nghttp2_pq_entry *item);
  111. #endif /* NGHTTP2_PQ_H */