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