Coverage Report

Created: 2022-09-28 06:21

/src/curl/lib/llist.c
Line
Count
Source (jump to first uncovered line)
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) 1998 - 2022, 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 <curl/curl.h>
28
29
#include "llist.h"
30
#include "curl_memory.h"
31
32
/* this must be the last include file */
33
#include "memdebug.h"
34
35
/*
36
 * @unittest: 1300
37
 */
38
void
39
Curl_llist_init(struct Curl_llist *l, Curl_llist_dtor dtor)
40
11.7M
{
41
11.7M
  l->size = 0;
42
11.7M
  l->dtor = dtor;
43
11.7M
  l->head = NULL;
44
11.7M
  l->tail = NULL;
45
11.7M
}
46
47
/*
48
 * Curl_llist_insert_next()
49
 *
50
 * Inserts a new list element after the given one 'e'. If the given existing
51
 * entry is NULL and the list already has elements, the new one will be
52
 * inserted first in the list.
53
 *
54
 * The 'ne' argument should be a pointer into the object to store.
55
 *
56
 * @unittest: 1300
57
 */
58
void
59
Curl_llist_insert_next(struct Curl_llist *list, struct Curl_llist_element *e,
60
                       const void *p,
61
                       struct Curl_llist_element *ne)
62
872k
{
63
872k
  ne->ptr = (void *) p;
64
872k
  if(list->size == 0) {
65
556k
    list->head = ne;
66
556k
    list->head->prev = NULL;
67
556k
    list->head->next = NULL;
68
556k
    list->tail = ne;
69
556k
  }
70
315k
  else {
71
    /* if 'e' is NULL here, we insert the new element first in the list */
72
315k
    ne->next = e?e->next:list->head;
73
315k
    ne->prev = e;
74
315k
    if(!e) {
75
6.27k
      list->head->prev = ne;
76
6.27k
      list->head = ne;
77
6.27k
    }
78
309k
    else if(e->next) {
79
78
      e->next->prev = ne;
80
78
    }
81
309k
    else {
82
309k
      list->tail = ne;
83
309k
    }
84
315k
    if(e)
85
309k
      e->next = ne;
86
315k
  }
87
88
872k
  ++list->size;
89
872k
}
90
91
/*
92
 * @unittest: 1300
93
 */
94
void
95
Curl_llist_remove(struct Curl_llist *list, struct Curl_llist_element *e,
96
                  void *user)
97
718k
{
98
718k
  void *ptr;
99
718k
  if(!e || list->size == 0)
100
0
    return;
101
102
718k
  if(e == list->head) {
103
590k
    list->head = e->next;
104
105
590k
    if(!list->head)
106
549k
      list->tail = NULL;
107
40.7k
    else
108
40.7k
      e->next->prev = NULL;
109
590k
  }
110
127k
  else {
111
127k
    if(e->prev)
112
127k
      e->prev->next = e->next;
113
114
127k
    if(!e->next)
115
124k
      list->tail = e->prev;
116
2.77k
    else
117
2.77k
      e->next->prev = e->prev;
118
127k
  }
119
120
718k
  ptr = e->ptr;
121
122
718k
  e->ptr  = NULL;
123
718k
  e->prev = NULL;
124
718k
  e->next = NULL;
125
126
718k
  --list->size;
127
128
  /* call the dtor() last for when it actually frees the 'e' memory itself */
129
718k
  if(list->dtor)
130
202k
    list->dtor(user, ptr);
131
718k
}
132
133
void
134
Curl_llist_destroy(struct Curl_llist *list, void *user)
135
11.9M
{
136
11.9M
  if(list) {
137
12.0M
    while(list->size > 0)
138
65.1k
      Curl_llist_remove(list, list->tail, user);
139
11.9M
  }
140
11.9M
}
141
142
size_t
143
Curl_llist_count(struct Curl_llist *list)
144
258k
{
145
258k
  return list->size;
146
258k
}