/src/PROJ/curl/lib/llist.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 <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 | 0 | { |
41 | 0 | l->size = 0; |
42 | 0 | l->dtor = dtor; |
43 | 0 | l->head = NULL; |
44 | 0 | l->tail = NULL; |
45 | 0 | } |
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 | 0 | { |
63 | 0 | ne->ptr = (void *) p; |
64 | 0 | if(list->size == 0) { |
65 | 0 | list->head = ne; |
66 | 0 | list->head->prev = NULL; |
67 | 0 | list->head->next = NULL; |
68 | 0 | list->tail = ne; |
69 | 0 | } |
70 | 0 | else { |
71 | | /* if 'e' is NULL here, we insert the new element first in the list */ |
72 | 0 | ne->next = e?e->next:list->head; |
73 | 0 | ne->prev = e; |
74 | 0 | if(!e) { |
75 | 0 | list->head->prev = ne; |
76 | 0 | list->head = ne; |
77 | 0 | } |
78 | 0 | else if(e->next) { |
79 | 0 | e->next->prev = ne; |
80 | 0 | } |
81 | 0 | else { |
82 | 0 | list->tail = ne; |
83 | 0 | } |
84 | 0 | if(e) |
85 | 0 | e->next = ne; |
86 | 0 | } |
87 | |
|
88 | 0 | ++list->size; |
89 | 0 | } |
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 | 0 | { |
98 | 0 | void *ptr; |
99 | 0 | if(!e || list->size == 0) |
100 | 0 | return; |
101 | | |
102 | 0 | if(e == list->head) { |
103 | 0 | list->head = e->next; |
104 | |
|
105 | 0 | if(!list->head) |
106 | 0 | list->tail = NULL; |
107 | 0 | else |
108 | 0 | e->next->prev = NULL; |
109 | 0 | } |
110 | 0 | else { |
111 | 0 | if(e->prev) |
112 | 0 | e->prev->next = e->next; |
113 | |
|
114 | 0 | if(!e->next) |
115 | 0 | list->tail = e->prev; |
116 | 0 | else |
117 | 0 | e->next->prev = e->prev; |
118 | 0 | } |
119 | |
|
120 | 0 | ptr = e->ptr; |
121 | |
|
122 | 0 | e->ptr = NULL; |
123 | 0 | e->prev = NULL; |
124 | 0 | e->next = NULL; |
125 | |
|
126 | 0 | --list->size; |
127 | | |
128 | | /* call the dtor() last for when it actually frees the 'e' memory itself */ |
129 | 0 | if(list->dtor) |
130 | 0 | list->dtor(user, ptr); |
131 | 0 | } |
132 | | |
133 | | void |
134 | | Curl_llist_destroy(struct Curl_llist *list, void *user) |
135 | 0 | { |
136 | 0 | if(list) { |
137 | 0 | while(list->size > 0) |
138 | 0 | Curl_llist_remove(list, list->tail, user); |
139 | 0 | } |
140 | 0 | } |
141 | | |
142 | | size_t |
143 | | Curl_llist_count(struct Curl_llist *list) |
144 | 0 | { |
145 | 0 | return list->size; |
146 | 0 | } |