Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "curl_setup.h" |
26 | | |
27 | | #include "curlx/timeval.h" |
28 | | #include "splay.h" |
29 | | |
30 | | /* |
31 | | * This macro compares two node keys i and j and returns: |
32 | | * |
33 | | * negative value: when i is smaller than j |
34 | | * zero : when i is equal to j |
35 | | * positive when : when i is larger than j |
36 | | */ |
37 | 42.3M | #define compare(i, j) curlx_timediff_us(i, j) |
38 | | |
39 | | /* |
40 | | * Splay using the key i (which may or may not be in the tree.) The starting |
41 | | * root is t. |
42 | | */ |
43 | | struct Curl_tree *Curl_splay(struct curltime i, |
44 | | struct Curl_tree *t) |
45 | 21.0M | { |
46 | 21.0M | struct Curl_tree N, *l, *r, *y; |
47 | | |
48 | 21.0M | if(!t) |
49 | 0 | return NULL; |
50 | 21.0M | N.smaller = N.larger = NULL; |
51 | 21.0M | l = r = &N; |
52 | | |
53 | 21.0M | for(;;) { |
54 | 21.0M | timediff_t comp = compare(i, t->key); |
55 | 21.0M | if(comp < 0) { |
56 | 20.4M | if(!t->smaller) |
57 | 20.4M | break; |
58 | 5.66k | if(compare(i, t->smaller->key) < 0) { |
59 | 5.66k | y = t->smaller; /* rotate smaller */ |
60 | 5.66k | t->smaller = y->larger; |
61 | 5.66k | y->larger = t; |
62 | 5.66k | t = y; |
63 | 5.66k | if(!t->smaller) |
64 | 913 | break; |
65 | 5.66k | } |
66 | 4.75k | r->smaller = t; /* link smaller */ |
67 | 4.75k | r = t; |
68 | 4.75k | t = t->smaller; |
69 | 4.75k | } |
70 | 603k | else if(comp > 0) { |
71 | 30.0k | if(!t->larger) |
72 | 21.2k | break; |
73 | 8.78k | if(compare(i, t->larger->key) > 0) { |
74 | 4.28k | y = t->larger; /* rotate larger */ |
75 | 4.28k | t->larger = y->smaller; |
76 | 4.28k | y->smaller = t; |
77 | 4.28k | t = y; |
78 | 4.28k | if(!t->larger) |
79 | 2.32k | break; |
80 | 4.28k | } |
81 | 6.46k | l->larger = t; /* link larger */ |
82 | 6.46k | l = t; |
83 | 6.46k | t = t->larger; |
84 | 6.46k | } |
85 | 573k | else |
86 | 573k | break; |
87 | 21.0M | } |
88 | | |
89 | 21.0M | l->larger = t->smaller; /* assemble */ |
90 | 21.0M | r->smaller = t->larger; |
91 | 21.0M | t->smaller = N.larger; |
92 | 21.0M | t->larger = N.smaller; |
93 | | |
94 | 21.0M | return t; |
95 | 21.0M | } |
96 | | |
97 | | static const struct curltime SPLAY_SUBNODE = { |
98 | | ~0, -1 |
99 | | }; |
100 | | |
101 | | /* Insert key i into the tree t. Return a pointer to the resulting tree or |
102 | | * NULL if something went wrong. |
103 | | * |
104 | | * @unittest: 1309 |
105 | | */ |
106 | | struct Curl_tree *Curl_splayinsert(struct curltime i, |
107 | | struct Curl_tree *t, |
108 | | struct Curl_tree *node) |
109 | 575k | { |
110 | 575k | DEBUGASSERT(node); |
111 | | |
112 | 575k | if(t) { |
113 | 357k | t = Curl_splay(i, t); |
114 | 357k | DEBUGASSERT(t); |
115 | 357k | if(compare(i, t->key) == 0) { |
116 | | /* There already exists a node in the tree with the same key. Build a |
117 | | doubly-linked circular list of nodes. We add the new 'node' struct to |
118 | | the end of this list. */ |
119 | | |
120 | 3 | node->key = SPLAY_SUBNODE; /* identify this node as a subnode */ |
121 | 3 | node->samen = t; |
122 | 3 | node->samep = t->samep; |
123 | 3 | t->samep->samen = node; |
124 | 3 | t->samep = node; |
125 | | |
126 | 3 | return t; /* the root node always stays the same */ |
127 | 3 | } |
128 | 357k | } |
129 | | |
130 | 575k | if(!t) { |
131 | 217k | node->smaller = node->larger = NULL; |
132 | 217k | } |
133 | 357k | else if(compare(i, t->key) < 0) { |
134 | 345k | node->smaller = t->smaller; |
135 | 345k | node->larger = t; |
136 | 345k | t->smaller = NULL; |
137 | 345k | } |
138 | 11.6k | else { |
139 | 11.6k | node->larger = t->larger; |
140 | 11.6k | node->smaller = t; |
141 | 11.6k | t->larger = NULL; |
142 | 11.6k | } |
143 | 575k | node->key = i; |
144 | | |
145 | | /* no identical nodes (yet), we are the only one in the list of nodes */ |
146 | 575k | node->samen = node; |
147 | 575k | node->samep = node; |
148 | 575k | return node; |
149 | 575k | } |
150 | | |
151 | | /* Finds and deletes the best-fit node from the tree. Return a pointer to the |
152 | | resulting tree. best-fit means the smallest node if it is not larger than |
153 | | the key */ |
154 | | struct Curl_tree *Curl_splaygetbest(struct curltime i, |
155 | | struct Curl_tree *t, |
156 | | struct Curl_tree **removed) |
157 | 20.2M | { |
158 | 20.2M | static const struct curltime tv_zero = { 0, 0 }; |
159 | 20.2M | struct Curl_tree *x; |
160 | | |
161 | 20.2M | if(!t) { |
162 | 156k | *removed = NULL; /* none removed since there was no root */ |
163 | 156k | return NULL; |
164 | 156k | } |
165 | | |
166 | | /* find smallest */ |
167 | 20.0M | t = Curl_splay(tv_zero, t); |
168 | 20.0M | DEBUGASSERT(t); |
169 | 20.0M | if(compare(i, t->key) < 0) { |
170 | | /* even the smallest is too big */ |
171 | 20.0M | *removed = NULL; |
172 | 20.0M | return t; |
173 | 20.0M | } |
174 | | |
175 | | /* FIRST! Check if there is a list with identical keys */ |
176 | 2.15k | x = t->samen; |
177 | 2.15k | if(x != t) { |
178 | | /* there is, pick one from the list */ |
179 | | |
180 | | /* 'x' is the new root node */ |
181 | |
|
182 | 0 | x->key = t->key; |
183 | 0 | x->larger = t->larger; |
184 | 0 | x->smaller = t->smaller; |
185 | 0 | x->samep = t->samep; |
186 | 0 | t->samep->samen = x; |
187 | |
|
188 | 0 | *removed = t; |
189 | 0 | return x; /* new root */ |
190 | 0 | } |
191 | | |
192 | | /* we splayed the tree to the smallest element, there is no smaller */ |
193 | 2.15k | x = t->larger; |
194 | 2.15k | *removed = t; |
195 | | |
196 | 2.15k | return x; |
197 | 2.15k | } |
198 | | |
199 | | /* Deletes the node we point out from the tree if it is there. Stores a |
200 | | * pointer to the new resulting tree in 'newroot'. |
201 | | * |
202 | | * Returns zero on success and non-zero on errors! |
203 | | * When returning error, it does not touch the 'newroot' pointer. |
204 | | * |
205 | | * NOTE: when the last node of the tree is removed, there is no tree left so |
206 | | * 'newroot' will be made to point to NULL. |
207 | | * |
208 | | * @unittest: 1309 |
209 | | */ |
210 | | int Curl_splayremove(struct Curl_tree *t, |
211 | | struct Curl_tree *removenode, |
212 | | struct Curl_tree **newroot) |
213 | 573k | { |
214 | 573k | struct Curl_tree *x; |
215 | | |
216 | 573k | if(!t) |
217 | 0 | return 1; |
218 | | |
219 | 573k | DEBUGASSERT(removenode); |
220 | | |
221 | 573k | if(compare(SPLAY_SUBNODE, removenode->key) == 0) { |
222 | | /* It is a subnode within a 'same' linked list and thus we can unlink it |
223 | | easily. */ |
224 | 3 | DEBUGASSERT(removenode->samen != removenode); |
225 | 3 | if(removenode->samen == removenode) |
226 | | /* A non-subnode should never be set to SPLAY_SUBNODE */ |
227 | 0 | return 3; |
228 | | |
229 | 3 | removenode->samep->samen = removenode->samen; |
230 | 3 | removenode->samen->samep = removenode->samep; |
231 | | |
232 | | /* Ensures that double-remove gets caught. */ |
233 | 3 | removenode->samen = removenode; |
234 | | |
235 | 3 | *newroot = t; /* return the same root */ |
236 | 3 | return 0; |
237 | 3 | } |
238 | | |
239 | 573k | t = Curl_splay(removenode->key, t); |
240 | 573k | DEBUGASSERT(t); |
241 | | |
242 | | /* First make sure that we got the same root node as the one we want |
243 | | to remove, as otherwise we might be trying to remove a node that |
244 | | is not actually in the tree. |
245 | | |
246 | | We cannot just compare the keys here as a double remove in quick |
247 | | succession of a node with key != SPLAY_SUBNODE && same != NULL |
248 | | could return the same key but a different node. */ |
249 | 573k | DEBUGASSERT(t == removenode); |
250 | 573k | if(t != removenode) |
251 | 0 | return 2; |
252 | | |
253 | | /* Check if there is a list with identical sizes, as then we are trying to |
254 | | remove the root node of a list of nodes with identical keys. */ |
255 | 573k | x = t->samen; |
256 | 573k | if(x != t) { |
257 | | /* 'x' is the new root node, we just make it use the root node's |
258 | | smaller/larger links */ |
259 | |
|
260 | 0 | x->key = t->key; |
261 | 0 | x->larger = t->larger; |
262 | 0 | x->smaller = t->smaller; |
263 | 0 | x->samep = t->samep; |
264 | 0 | t->samep->samen = x; |
265 | 0 | } |
266 | 573k | else { |
267 | | /* Remove the root node */ |
268 | 573k | if(!t->smaller) |
269 | 561k | x = t->larger; |
270 | 11.9k | else { |
271 | 11.9k | x = Curl_splay(removenode->key, t->smaller); |
272 | 11.9k | DEBUGASSERT(x); |
273 | 11.9k | x->larger = t->larger; |
274 | 11.9k | } |
275 | 573k | } |
276 | | |
277 | 573k | *newroot = x; /* store new root pointer */ |
278 | | |
279 | 573k | return 0; |
280 | 573k | } |
281 | | |
282 | | /* set and get the custom payload for this tree node */ |
283 | | void Curl_splayset(struct Curl_tree *node, void *payload) |
284 | 573k | { |
285 | 573k | DEBUGASSERT(node); |
286 | 573k | node->ptr = payload; |
287 | 573k | } |
288 | | |
289 | | void *Curl_splayget(struct Curl_tree *node) |
290 | 2.15k | { |
291 | 2.15k | DEBUGASSERT(node); |
292 | 2.15k | return node->ptr; |
293 | 2.15k | } |