Coverage Report

Created: 2026-06-30 08:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/curl/lib/llist.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 "llist.h"
27
28
#ifdef DEBUGBUILD
29
#define LLISTINIT 0x100cc001 /* random pattern */
30
#define NODEINIT  0x12344321 /* random pattern */
31
#define NODEREM   0x54321012 /* random pattern */
32
33
#define VERIFYNODE(x) verifynode(x)
34
static struct Curl_llist_node *verifynode(struct Curl_llist_node *n)
35
{
36
  DEBUGASSERT(!n || (n->_init == NODEINIT));
37
  return n;
38
}
39
#else
40
6.62M
#define VERIFYNODE(x) x
41
#endif
42
/*
43
 * @unittest 1300
44
 */
45
void Curl_llist_init(struct Curl_llist *l, Curl_llist_dtor dtor)
46
3.02M
{
47
3.02M
  l->_size = 0;
48
3.02M
  l->_dtor = dtor;
49
3.02M
  l->_head = NULL;
50
3.02M
  l->_tail = NULL;
51
#ifdef DEBUGBUILD
52
  l->_init = LLISTINIT;
53
#endif
54
3.02M
}
55
56
/*
57
 * Curl_llist_insert_next()
58
 *
59
 * Inserts a new list element after the given one 'e'. If the given existing
60
 * entry is NULL and the list already has elements, the new one will be
61
 * inserted first in the list.
62
 *
63
 * The 'ne' argument should be a pointer into the object to store.
64
 *
65
 * @unittest 1300
66
 */
67
void Curl_llist_insert_next(struct Curl_llist *list,
68
                            struct Curl_llist_node *e, /* may be NULL */
69
                            const void *p,
70
                            struct Curl_llist_node *ne)
71
1.82M
{
72
1.82M
  DEBUGASSERT(list);
73
1.82M
  DEBUGASSERT(list->_init == LLISTINIT);
74
1.82M
  DEBUGASSERT(ne);
75
76
#ifdef DEBUGBUILD
77
  ne->_init = NODEINIT;
78
#endif
79
1.82M
  ne->_ptr = CURL_UNCONST(p);
80
1.82M
  ne->_list = list;
81
1.82M
  if(list->_size == 0) {
82
1.13M
    list->_head = ne;
83
1.13M
    list->_head->_prev = NULL;
84
1.13M
    list->_head->_next = NULL;
85
1.13M
    list->_tail = ne;
86
1.13M
  }
87
690k
  else {
88
    /* if 'e' is NULL here, we insert the new element first in the list */
89
690k
    ne->_next = e ? e->_next : list->_head;
90
690k
    ne->_prev = e;
91
690k
    if(!e) {
92
28.4k
      list->_head->_prev = ne;
93
28.4k
      list->_head = ne;
94
28.4k
    }
95
661k
    else if(e->_next) {
96
21.6k
      e->_next->_prev = ne;
97
21.6k
    }
98
640k
    else {
99
640k
      list->_tail = ne;
100
640k
    }
101
690k
    if(e)
102
661k
      e->_next = ne;
103
690k
  }
104
105
1.82M
  ++list->_size;
106
1.82M
}
107
108
/*
109
 * Curl_llist_append()
110
 *
111
 * Adds a new list element to the end of the list.
112
 *
113
 * The 'ne' argument should be a pointer into the object to store.
114
 *
115
 * @unittest 1300
116
 */
117
void Curl_llist_append(struct Curl_llist *list, const void *p,
118
                       struct Curl_llist_node *ne)
119
993k
{
120
993k
  DEBUGASSERT(list);
121
993k
  DEBUGASSERT(list->_init == LLISTINIT);
122
993k
  DEBUGASSERT(ne);
123
993k
  Curl_llist_insert_next(list, list->_tail, p, ne);
124
993k
}
125
126
void *Curl_node_take_elem(struct Curl_llist_node *e)
127
1.74M
{
128
1.74M
  void *ptr;
129
1.74M
  struct Curl_llist *list;
130
1.74M
  if(!e)
131
0
    return NULL;
132
133
1.74M
  list = e->_list;
134
1.74M
  DEBUGASSERT(list);
135
1.74M
  DEBUGASSERT(list->_init == LLISTINIT);
136
1.74M
  DEBUGASSERT(list->_size);
137
1.74M
  DEBUGASSERT(e->_init == NODEINIT);
138
1.74M
  if(list) {
139
1.74M
    if(e == list->_head) {
140
1.26M
      list->_head = e->_next;
141
142
1.26M
      if(!list->_head)
143
1.12M
        list->_tail = NULL;
144
141k
      else
145
141k
        e->_next->_prev = NULL;
146
1.26M
    }
147
478k
    else {
148
478k
      if(e->_prev)
149
478k
        e->_prev->_next = e->_next;
150
151
478k
      if(!e->_next)
152
456k
        list->_tail = e->_prev;
153
21.6k
      else
154
21.6k
        e->_next->_prev = e->_prev;
155
478k
    }
156
1.74M
    --list->_size;
157
1.74M
  }
158
1.74M
  ptr = e->_ptr;
159
160
1.74M
  e->_list = NULL;
161
1.74M
  e->_ptr  = NULL;
162
1.74M
  e->_prev = NULL;
163
1.74M
  e->_next = NULL;
164
#ifdef DEBUGBUILD
165
  e->_init = NODEREM; /* specific pattern on remove - not zero */
166
#endif
167
168
1.74M
  return ptr;
169
1.74M
}
170
171
static void node_uremove(struct Curl_llist_node *e, void *user)
172
1.55M
{
173
1.55M
  struct Curl_llist *list;
174
1.55M
  void *ptr;
175
1.55M
  if(!e)
176
0
    return;
177
178
1.55M
  list = e->_list;
179
1.55M
  DEBUGASSERT(list);
180
1.55M
  if(list) {
181
1.55M
    ptr = Curl_node_take_elem(e);
182
1.55M
    if(list->_dtor)
183
12.3k
      list->_dtor(user, ptr);
184
1.55M
  }
185
1.55M
}
186
187
void Curl_node_remove(struct Curl_llist_node *e)
188
788k
{
189
788k
  node_uremove(e, NULL);
190
788k
}
191
192
void Curl_llist_destroy(struct Curl_llist *list, void *user)
193
977k
{
194
977k
  if(list) {
195
977k
    DEBUGASSERT(list->_init == LLISTINIT);
196
1.74M
    while(list->_size > 0)
197
763k
      node_uremove(list->_tail, user);
198
977k
  }
199
977k
}
200
201
/* Curl_llist_head() returns the first 'struct Curl_llist_node *', which
202
   might be NULL */
203
struct Curl_llist_node *Curl_llist_head(const struct Curl_llist *list)
204
5.24M
{
205
5.24M
  DEBUGASSERT(list);
206
5.24M
  DEBUGASSERT(list->_init == LLISTINIT);
207
5.24M
  return VERIFYNODE(list->_head);
208
5.24M
}
209
210
#ifdef UNITTESTS
211
/* llist_tail() returns the last 'struct Curl_llist_node *', which might be
212
   NULL
213
214
   @unittest 1300
215
*/
216
UNITTEST struct Curl_llist_node *llist_tail(const struct Curl_llist *list);
217
UNITTEST struct Curl_llist_node *llist_tail(const struct Curl_llist *list)
218
{
219
  DEBUGASSERT(list);
220
  DEBUGASSERT(list->_init == LLISTINIT);
221
  return VERIFYNODE(list->_tail);
222
}
223
#endif
224
225
/* Curl_llist_count() returns a size_t the number of nodes in the list */
226
size_t Curl_llist_count(const struct Curl_llist *list)
227
2.47M
{
228
2.47M
  DEBUGASSERT(list);
229
2.47M
  DEBUGASSERT(list->_init == LLISTINIT);
230
2.47M
  return list->_size;
231
2.47M
}
232
233
/* Curl_node_elem() returns the custom data from a Curl_llist_node */
234
void *Curl_node_elem(const struct Curl_llist_node *n)
235
1.98M
{
236
1.98M
  DEBUGASSERT(n);
237
1.98M
  DEBUGASSERT(n->_init == NODEINIT);
238
1.98M
  return n->_ptr;
239
1.98M
}
240
241
/* Curl_node_next() returns the next element in a list from a given
242
   Curl_llist_node */
243
struct Curl_llist_node *Curl_node_next(const struct Curl_llist_node *n)
244
1.37M
{
245
1.37M
  DEBUGASSERT(n);
246
1.37M
  DEBUGASSERT(n->_init == NODEINIT);
247
1.37M
  return VERIFYNODE(n->_next);
248
1.37M
}
249
250
#ifdef UNITTESTS
251
/* llist_node_prev() returns the previous element in a list from a given
252
   Curl_llist_node
253
254
   @unittest 1300
255
*/
256
UNITTEST struct Curl_llist_node *llist_node_prev(
257
  const struct Curl_llist_node *n);
258
UNITTEST struct Curl_llist_node *llist_node_prev(
259
  const struct Curl_llist_node *n)
260
{
261
  DEBUGASSERT(n);
262
  DEBUGASSERT(n->_init == NODEINIT);
263
  return VERIFYNODE(n->_prev);
264
}
265
#endif
266
267
struct Curl_llist *Curl_node_llist(const struct Curl_llist_node *n)
268
135k
{
269
135k
  DEBUGASSERT(n);
270
135k
  DEBUGASSERT(!n->_list || n->_init == NODEINIT);
271
135k
  return n->_list;
272
135k
}