Coverage Report

Created: 2025-12-04 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/splay.c
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
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
0
  }
138
0
  else {
139
0
    node->larger = t->larger;
140
0
    node->smaller = t;
141
0
    t->larger = NULL;
142
0
  }
143
0
  node->key = i;
144
145
  /* no identical nodes (yet), we are the only one in the list of nodes */
146
0
  node->samen = node;
147
0
  node->samep = node;
148
0
  return node;
149
0
}
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
0
{
158
0
  static const struct curltime tv_zero = { 0, 0 };
159
0
  struct Curl_tree *x;
160
161
0
  if(!t) {
162
0
    *removed = NULL; /* none removed since there was no root */
163
0
    return NULL;
164
0
  }
165
166
  /* find smallest */
167
0
  t = Curl_splay(tv_zero, t);
168
0
  DEBUGASSERT(t);
169
0
  if(compare(i, t->key) < 0) {
170
    /* even the smallest is too big */
171
0
    *removed = NULL;
172
0
    return t;
173
0
  }
174
175
  /* FIRST! Check if there is a list with identical keys */
176
0
  x = t->samen;
177
0
  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
0
  x = t->larger;
194
0
  *removed = t;
195
196
0
  return x;
197
0
}
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
0
{
214
0
  struct Curl_tree *x;
215
216
0
  if(!t)
217
0
    return 1;
218
219
0
  DEBUGASSERT(removenode);
220
221
0
  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
0
    DEBUGASSERT(removenode->samen != removenode);
225
0
    if(removenode->samen == removenode)
226
      /* A non-subnode should never be set to SPLAY_SUBNODE */
227
0
      return 3;
228
229
0
    removenode->samep->samen = removenode->samen;
230
0
    removenode->samen->samep = removenode->samep;
231
232
    /* Ensures that double-remove gets caught. */
233
0
    removenode->samen = removenode;
234
235
0
    *newroot = t; /* return the same root */
236
0
    return 0;
237
0
  }
238
239
0
  t = Curl_splay(removenode->key, t);
240
0
  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
0
  DEBUGASSERT(t == removenode);
250
0
  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
0
  x = t->samen;
256
0
  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
0
  else {
267
    /* Remove the root node */
268
0
    if(!t->smaller)
269
0
      x = t->larger;
270
0
    else {
271
0
      x = Curl_splay(removenode->key, t->smaller);
272
0
      DEBUGASSERT(x);
273
0
      x->larger = t->larger;
274
0
    }
275
0
  }
276
277
0
  *newroot = x; /* store new root pointer */
278
279
0
  return 0;
280
0
}
281
282
/* set and get the custom payload for this tree node */
283
void Curl_splayset(struct Curl_tree *node, void *payload)
284
0
{
285
0
  DEBUGASSERT(node);
286
0
  node->ptr = payload;
287
0
}
288
289
void *Curl_splayget(struct Curl_tree *node)
290
0
{
291
0
  DEBUGASSERT(node);
292
0
  return node->ptr;
293
0
}