xref: /curl/lib/llist.h (revision fa0ccd9f)
1 #ifndef HEADER_CURL_LLIST_H
2 #define HEADER_CURL_LLIST_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11  *
12  * This software is licensed as described in the file COPYING, which
13  * you should have received as part of this distribution. The terms
14  * are also available at https://curl.se/docs/copyright.html.
15  *
16  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17  * copies of the Software, and permit persons to whom the Software is
18  * furnished to do so, under the terms of the COPYING file.
19  *
20  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21  * KIND, either express or implied.
22  *
23  * SPDX-License-Identifier: curl
24  *
25  ***************************************************************************/
26 
27 #include "curl_setup.h"
28 #include <stddef.h>
29 
30 typedef void (*Curl_llist_dtor)(void *user, void *elem);
31 
32 /* none of these struct members should be referenced directly, use the
33    dedicated functions */
34 
35 struct Curl_llist {
36   struct Curl_llist_node *_head;
37   struct Curl_llist_node *_tail;
38   Curl_llist_dtor _dtor;
39   size_t _size;
40 #ifdef DEBUGBUILD
41   int _init;      /* detect API usage mistakes */
42 #endif
43 };
44 
45 struct Curl_llist_node {
46   struct Curl_llist *_list; /* the list where this belongs */
47   void *_ptr;
48   struct Curl_llist_node *_prev;
49   struct Curl_llist_node *_next;
50 #ifdef DEBUGBUILD
51   int _init;      /* detect API usage mistakes */
52 #endif
53 };
54 
55 void Curl_llist_init(struct Curl_llist *, Curl_llist_dtor);
56 void Curl_llist_insert_next(struct Curl_llist *, struct Curl_llist_node *,
57                             const void *, struct Curl_llist_node *node);
58 void Curl_llist_append(struct Curl_llist *,
59                        const void *, struct Curl_llist_node *node);
60 void Curl_node_uremove(struct Curl_llist_node *, void *);
61 void Curl_node_remove(struct Curl_llist_node *);
62 void Curl_llist_destroy(struct Curl_llist *, void *);
63 
64 /* Curl_llist_head() returns the first 'struct Curl_llist_node *', which
65    might be NULL */
66 struct Curl_llist_node *Curl_llist_head(struct Curl_llist *list);
67 
68 /* Curl_llist_tail() returns the last 'struct Curl_llist_node *', which
69    might be NULL */
70 struct Curl_llist_node *Curl_llist_tail(struct Curl_llist *list);
71 
72 /* Curl_llist_count() returns a size_t the number of nodes in the list */
73 size_t Curl_llist_count(struct Curl_llist *list);
74 
75 /* Curl_node_elem() returns the custom data from a Curl_llist_node */
76 void *Curl_node_elem(struct Curl_llist_node *n);
77 
78 /* Remove the node from the list and return the custom data
79  * from a Curl_llist_node. Will NOT incoke a registered `dtor`. */
80 void *Curl_node_take_elem(struct Curl_llist_node *);
81 
82 /* Curl_node_next() returns the next element in a list from a given
83    Curl_llist_node */
84 struct Curl_llist_node *Curl_node_next(struct Curl_llist_node *n);
85 
86 /* Curl_node_prev() returns the previous element in a list from a given
87    Curl_llist_node */
88 struct Curl_llist_node *Curl_node_prev(struct Curl_llist_node *n);
89 
90 /* Curl_node_llist() return the list the node is in or NULL. */
91 struct Curl_llist *Curl_node_llist(struct Curl_llist_node *n);
92 
93 #endif /* HEADER_CURL_LLIST_H */
94