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