Coverage Report

Created: 2025-08-26 07:09

/src/curl/lib/splay.c
Line
Count
Source (jump to first uncovered line)
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
0
#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
0
{
46
0
  struct Curl_tree N, *l, *r, *y;
47
48
0
  if(!t)
49
0
    return NULL;
50
0
  N.smaller = N.larger = NULL;
51
0
  l = r = &N;
52
53
0
  for(;;) {
54
0
    timediff_t comp = compare(i, t->key);
55
0
    if(comp < 0) {
56
0
      if(!t->smaller)
57
0
        break;
58
0
      if(compare(i, t->smaller->key) < 0) {
59
0
        y = t->smaller;                           /* rotate smaller */
60
0
        t->smaller = y->larger;
61
0
        y->larger = t;
62
0
        t = y;
63
0
        if(!t->smaller)
64
0
          break;
65
0
      }
66
0
      r->smaller = t;                               /* link smaller */
67
0
      r = t;
68
0
      t = t->smaller;
69
0
    }
70
0
    else if(comp > 0) {
71
0
      if(!t->larger)
72
0
        break;
73
0
      if(compare(i, t->larger->key) > 0) {
74
0
        y = t->larger;                          /* rotate larger */
75
0
        t->larger = y->smaller;
76
0
        y->smaller = t;
77
0
        t = y;
78
0
        if(!t->larger)
79
0
          break;
80
0
      }
81
0
      l->larger = t;                              /* link larger */
82
0
      l = t;
83
0
      t = t->larger;
84
0
    }
85
0
    else
86
0
      break;
87
0
  }
88
89
0
  l->larger = t->smaller;                                /* assemble */
90
0
  r->smaller = t->larger;
91
0
  t->smaller = N.larger;
92
0
  t->larger = N.smaller;
93
94
0
  return t;
95
0
}
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
0
{
110
0
  DEBUGASSERT(node);
111
112
0
  if(t) {
113
0
    t = Curl_splay(i, t);
114
0
    DEBUGASSERT(t);
115
0
    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
0
      node->key = SPLAY_SUBNODE; /* identify this node as a subnode */
121
0
      node->samen = t;
122
0
      node->samep = t->samep;
123
0
      t->samep->samen = node;
124
0
      t->samep = node;
125
126
0
      return t; /* the root node always stays the same */
127
0
    }
128
0
  }
129
130
0
  if(!t) {
131
0
    node->smaller = node->larger = NULL;
132
0
  }
133
0
  else if(compare(i, t->key) < 0) {
134
0
    node->smaller = t->smaller;
135
0
    node->larger = t;
136
0
    t->smaller = NULL;
137
138
0
  }
139
0
  else {
140
0
    node->larger = t->larger;
141
0
    node->smaller = t;
142
0
    t->larger = NULL;
143
0
  }
144
0
  node->key = i;
145
146
  /* no identical nodes (yet), we are the only one in the list of nodes */
147
0
  node->samen = node;
148
0
  node->samep = node;
149
0
  return node;
150
0
}
151
152
/* Finds and deletes the best-fit node from the tree. Return a pointer to the
153
   resulting tree. best-fit means the smallest node if it is not larger than
154
   the key */
155
struct Curl_tree *Curl_splaygetbest(struct curltime i,
156
                                    struct Curl_tree *t,
157
                                    struct Curl_tree **removed)
158
0
{
159
0
  static const struct curltime tv_zero = {0, 0};
160
0
  struct Curl_tree *x;
161
162
0
  if(!t) {
163
0
    *removed = NULL; /* none removed since there was no root */
164
0
    return NULL;
165
0
  }
166
167
  /* find smallest */
168
0
  t = Curl_splay(tv_zero, t);
169
0
  DEBUGASSERT(t);
170
0
  if(compare(i, t->key) < 0) {
171
    /* even the smallest is too big */
172
0
    *removed = NULL;
173
0
    return t;
174
0
  }
175
176
  /* FIRST! Check if there is a list with identical keys */
177
0
  x = t->samen;
178
0
  if(x != t) {
179
    /* there is, pick one from the list */
180
181
    /* 'x' is the new root node */
182
183
0
    x->key = t->key;
184
0
    x->larger = t->larger;
185
0
    x->smaller = t->smaller;
186
0
    x->samep = t->samep;
187
0
    t->samep->samen = x;
188
189
0
    *removed = t;
190
0
    return x; /* new root */
191
0
  }
192
193
  /* we splayed the tree to the smallest element, there is no smaller */
194
0
  x = t->larger;
195
0
  *removed = t;
196
197
0
  return x;
198
0
}
199
200
201
/* Deletes the node we point out from the tree if it is there. Stores a
202
 * pointer to the new resulting tree in 'newroot'.
203
 *
204
 * Returns zero on success and non-zero on errors!
205
 * When returning error, it does not touch the 'newroot' pointer.
206
 *
207
 * NOTE: when the last node of the tree is removed, there is no tree left so
208
 * 'newroot' will be made to point to NULL.
209
 *
210
 * @unittest: 1309
211
 */
212
int Curl_splayremove(struct Curl_tree *t,
213
                     struct Curl_tree *removenode,
214
                     struct Curl_tree **newroot)
215
0
{
216
0
  struct Curl_tree *x;
217
218
0
  if(!t)
219
0
    return 1;
220
221
0
  DEBUGASSERT(removenode);
222
223
0
  if(compare(SPLAY_SUBNODE, removenode->key) == 0) {
224
    /* It is a subnode within a 'same' linked list and thus we can unlink it
225
       easily. */
226
0
    DEBUGASSERT(removenode->samen != removenode);
227
0
    if(removenode->samen == removenode)
228
      /* A non-subnode should never be set to SPLAY_SUBNODE */
229
0
      return 3;
230
231
0
    removenode->samep->samen = removenode->samen;
232
0
    removenode->samen->samep = removenode->samep;
233
234
    /* Ensures that double-remove gets caught. */
235
0
    removenode->samen = removenode;
236
237
0
    *newroot = t; /* return the same root */
238
0
    return 0;
239
0
  }
240
241
0
  t = Curl_splay(removenode->key, t);
242
0
  DEBUGASSERT(t);
243
244
  /* First make sure that we got the same root node as the one we want
245
     to remove, as otherwise we might be trying to remove a node that
246
     is not actually in the tree.
247
248
     We cannot just compare the keys here as a double remove in quick
249
     succession of a node with key != SPLAY_SUBNODE && same != NULL
250
     could return the same key but a different node. */
251
0
  DEBUGASSERT(t == removenode);
252
0
  if(t != removenode)
253
0
    return 2;
254
255
  /* Check if there is a list with identical sizes, as then we are trying to
256
     remove the root node of a list of nodes with identical keys. */
257
0
  x = t->samen;
258
0
  if(x != t) {
259
    /* 'x' is the new root node, we just make it use the root node's
260
       smaller/larger links */
261
262
0
    x->key = t->key;
263
0
    x->larger = t->larger;
264
0
    x->smaller = t->smaller;
265
0
    x->samep = t->samep;
266
0
    t->samep->samen = x;
267
0
  }
268
0
  else {
269
    /* Remove the root node */
270
0
    if(!t->smaller)
271
0
      x = t->larger;
272
0
    else {
273
0
      x = Curl_splay(removenode->key, t->smaller);
274
0
      DEBUGASSERT(x);
275
0
      x->larger = t->larger;
276
0
    }
277
0
  }
278
279
0
  *newroot = x; /* store new root pointer */
280
281
0
  return 0;
282
0
}
283
284
/* set and get the custom payload for this tree node */
285
void Curl_splayset(struct Curl_tree *node, void *payload)
286
0
{
287
0
  DEBUGASSERT(node);
288
0
  node->ptr = payload;
289
0
}
290
291
void *Curl_splayget(struct Curl_tree *node)
292
0
{
293
0
  DEBUGASSERT(node);
294
0
  return node->ptr;
295
0
}