Coverage Report

Created: 2026-03-12 06:35

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