1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 1997 - 2017, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 ***************************************************************************/
23 #include "curl_setup.h"
28 * This macro compares two node keys i and j and returns:
30 * negative value: when i is smaller than j
31 * zero : when i is equal to j
32 * positive when : when i is larger than j
34 #define compare(i,j) Curl_splaycomparekeys((i),(j))
37 * Splay using the key i (which may or may not be in the tree.) The starting
40 struct Curl_tree *Curl_splay(struct curltime i,
43 struct Curl_tree N, *l, *r, *y;
48 N.smaller = N.larger = NULL;
52 comp = compare(i, t->key);
54 if(t->smaller == NULL)
56 if(compare(i, t->smaller->key) < 0) {
57 y = t->smaller; /* rotate smaller */
58 t->smaller = y->larger;
61 if(t->smaller == NULL)
64 r->smaller = t; /* link smaller */
71 if(compare(i, t->larger->key) > 0) {
72 y = t->larger; /* rotate larger */
73 t->larger = y->smaller;
79 l->larger = t; /* link larger */
87 l->larger = t->smaller; /* assemble */
88 r->smaller = t->larger;
89 t->smaller = N.larger;
90 t->larger = N.smaller;
95 /* Insert key i into the tree t. Return a pointer to the resulting tree or
96 * NULL if something went wrong.
100 struct Curl_tree *Curl_splayinsert(struct curltime i,
102 struct Curl_tree *node)
104 static const struct curltime KEY_NOTUSED = {
105 (time_t)-1, (unsigned int)-1
106 }; /* will *NEVER* appear */
112 t = Curl_splay(i, t);
113 if(compare(i, t->key) == 0) {
114 /* There already exists a node in the tree with the very same key. Build
115 a doubly-linked circular list of nodes. We add the new 'node' struct
116 to the end of this list. */
118 node->key = KEY_NOTUSED; /* we set the key in the sub node to NOTUSED
119 to quickly identify this node as a subnode */
121 node->samep = t->samep;
122 t->samep->samen = node;
125 return t; /* the root node always stays the same */
130 node->smaller = node->larger = NULL;
132 else if(compare(i, t->key) < 0) {
133 node->smaller = t->smaller;
139 node->larger = t->larger;
145 /* no identical nodes (yet), we are the only one in the list of nodes */
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
154 struct Curl_tree *Curl_splaygetbest(struct curltime i,
156 struct Curl_tree **removed)
158 static struct curltime tv_zero = {0, 0};
162 *removed = NULL; /* none removed since there was no root */
167 t = Curl_splay(tv_zero, t);
168 if(compare(i, t->key) < 0) {
169 /* even the smallest is too big */
174 /* FIRST! Check if there is a list with identical keys */
177 /* there is, pick one from the list */
179 /* 'x' is the new root node */
182 x->larger = t->larger;
183 x->smaller = t->smaller;
188 return x; /* new root */
191 /* we splayed the tree to the smallest element, there is no smaller */
199 /* Deletes the very node we point out from the tree if it's there. Stores a
200 * pointer to the new resulting tree in 'newroot'.
202 * Returns zero on success and non-zero on errors! TODO: document error codes.
203 * When returning error, it does not touch the 'newroot' pointer.
205 * NOTE: when the last node of the tree is removed, there's no tree left so
206 * 'newroot' will be made to point to NULL.
210 int Curl_splayremovebyaddr(struct Curl_tree *t,
211 struct Curl_tree *removenode,
212 struct Curl_tree **newroot)
214 static const struct curltime KEY_NOTUSED = {
215 (time_t)-1, (unsigned int)-1
216 }; /* will *NEVER* appear */
219 if(!t || !removenode)
222 if(compare(KEY_NOTUSED, removenode->key) == 0) {
223 /* Key set to NOTUSED means it is a subnode within a 'same' linked list
224 and thus we can unlink it easily. */
225 if(removenode->samen == removenode)
226 /* A non-subnode should never be set to KEY_NOTUSED */
229 removenode->samep->samen = removenode->samen;
230 removenode->samen->samep = removenode->samep;
232 /* Ensures that double-remove gets caught. */
233 removenode->samen = removenode;
235 *newroot = t; /* return the same root */
239 t = Curl_splay(removenode->key, t);
241 /* First make sure that we got the same root node as the one we want
242 to remove, as otherwise we might be trying to remove a node that
243 isn't actually in the tree.
245 We cannot just compare the keys here as a double remove in quick
246 succession of a node with key != KEY_NOTUSED && same != NULL
247 could return the same key but a different node. */
251 /* Check if there is a list with identical sizes, as then we're trying to
252 remove the root node of a list of nodes with identical keys. */
255 /* 'x' is the new root node, we just make it use the root node's
256 smaller/larger links */
259 x->larger = t->larger;
260 x->smaller = t->smaller;
265 /* Remove the root node */
266 if(t->smaller == NULL)
269 x = Curl_splay(removenode->key, t->smaller);
270 x->larger = t->larger;
274 *newroot = x; /* store new root pointer */