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