llist.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2017, 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.haxx.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  ***************************************************************************/
22 
23 #include "curl_setup.h"
24 
25 #include <curl/curl.h>
26 
27 #include "llist.h"
28 #include "curl_memory.h"
29 
30 /* this must be the last include file */
31 #include "memdebug.h"
32 
33 /*
34  * @unittest: 1300
35  */
36 void
38 {
39  l->size = 0;
40  l->dtor = dtor;
41  l->head = NULL;
42  l->tail = NULL;
43 }
44 
45 /*
46  * Curl_llist_insert_next()
47  *
48  * Inserts a new list element after the given one 'e'. If the given existing
49  * entry is NULL and the list already has elements, the new one will be
50  * inserted first in the list.
51  *
52  * The 'ne' argument should be a pointer into the object to store.
53  *
54  * @unittest: 1300
55  */
56 void
58  const void *p,
59  struct curl_llist_element *ne)
60 {
61  ne->ptr = (void *) p;
62  if(list->size == 0) {
63  list->head = ne;
64  list->head->prev = NULL;
65  list->head->next = NULL;
66  list->tail = ne;
67  }
68  else {
69  /* if 'e' is NULL here, we insert the new element first in the list */
70  ne->next = e?e->next:list->head;
71  ne->prev = e;
72  if(!e) {
73  list->head->prev = ne;
74  list->head = ne;
75  }
76  else if(e->next) {
77  e->next->prev = ne;
78  }
79  else {
80  list->tail = ne;
81  }
82  if(e)
83  e->next = ne;
84  }
85 
86  ++list->size;
87 }
88 
89 /*
90  * @unittest: 1300
91  */
92 void
94  void *user)
95 {
96  void *ptr;
97  if(e == NULL || list->size == 0)
98  return;
99 
100  if(e == list->head) {
101  list->head = e->next;
102 
103  if(list->head == NULL)
104  list->tail = NULL;
105  else
106  e->next->prev = NULL;
107  }
108  else {
109  e->prev->next = e->next;
110  if(!e->next)
111  list->tail = e->prev;
112  else
113  e->next->prev = e->prev;
114  }
115 
116  ptr = e->ptr;
117 
118  e->ptr = NULL;
119  e->prev = NULL;
120  e->next = NULL;
121 
122  --list->size;
123 
124  /* call the dtor() last for when it actually frees the 'e' memory itself */
125  if(list->dtor)
126  list->dtor(user, ptr);
127 }
128 
129 void
130 Curl_llist_destroy(struct curl_llist *list, void *user)
131 {
132  if(list) {
133  while(list->size > 0)
134  Curl_llist_remove(list, list->tail, user);
135  }
136 }
137 
138 size_t
140 {
141  return list->size;
142 }
143 
144 /*
145  * @unittest: 1300
146  */
147 void Curl_llist_move(struct curl_llist *list, struct curl_llist_element *e,
148  struct curl_llist *to_list,
149  struct curl_llist_element *to_e)
150 {
151  /* Remove element from list */
152  if(e == NULL || list->size == 0)
153  return;
154 
155  if(e == list->head) {
156  list->head = e->next;
157 
158  if(list->head == NULL)
159  list->tail = NULL;
160  else
161  e->next->prev = NULL;
162  }
163  else {
164  e->prev->next = e->next;
165  if(!e->next)
166  list->tail = e->prev;
167  else
168  e->next->prev = e->prev;
169  }
170 
171  --list->size;
172 
173  /* Add element to to_list after to_e */
174  if(to_list->size == 0) {
175  to_list->head = e;
176  to_list->head->prev = NULL;
177  to_list->head->next = NULL;
178  to_list->tail = e;
179  }
180  else {
181  e->next = to_e->next;
182  e->prev = to_e;
183  if(to_e->next) {
184  to_e->next->prev = e;
185  }
186  else {
187  to_list->tail = e;
188  }
189  to_e->next = e;
190  }
191 
192  ++to_list->size;
193 }
void(* curl_llist_dtor)(void *, void *)
Definition: llist.h:28
struct curl_llist_element * tail
Definition: llist.h:38
UNITTEST_START char * ptr
Definition: unit1330.c:38
void Curl_llist_init(struct curl_llist *l, curl_llist_dtor dtor)
Definition: llist.c:37
void Curl_llist_move(struct curl_llist *list, struct curl_llist_element *e, struct curl_llist *to_list, struct curl_llist_element *to_e)
Definition: llist.c:147
curl_llist_dtor dtor
Definition: llist.h:39
const char ** p
Definition: unit1394.c:76
size_t size
Definition: llist.h:40
size_t Curl_llist_count(struct curl_llist *list)
Definition: llist.c:139
void * ptr
Definition: llist.h:31
struct curl_llist_element * head
Definition: llist.h:37
void Curl_llist_remove(struct curl_llist *list, struct curl_llist_element *e, void *user)
Definition: llist.c:93
void Curl_llist_destroy(struct curl_llist *list, void *user)
Definition: llist.c:130
void Curl_llist_insert_next(struct curl_llist *list, struct curl_llist_element *e, const void *p, struct curl_llist_element *ne)
Definition: llist.c:57
struct curl_llist_element * prev
Definition: llist.h:32
struct curl_llist_element * next
Definition: llist.h:33


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:15