]> git.ozlabs.org Git - ccan/blob - ccan/list/list.h
list: fix uninitialized variable in list_add example.
[ccan] / ccan / list / list.h
1 #ifndef CCAN_LIST_H
2 #define CCAN_LIST_H
3 #include <stdbool.h>
4 #include <ccan/container_of/container_of.h>
5
6 /**
7  * struct list_node - an entry in a doubly-linked list
8  * @next: next entry (self if empty)
9  * @prev: previous entry (self if empty)
10  *
11  * This is used as an entry in a linked list.
12  * Example:
13  *      struct child {
14  *              const char *name;
15  *              // Linked list of all us children.
16  *              struct list_node list;
17  *      };
18  */
19 struct list_node
20 {
21         struct list_node *next, *prev;
22 };
23
24 /**
25  * struct list_head - the head of a doubly-linked list
26  * @h: the list_head (containing next and prev pointers)
27  *
28  * This is used as the head of a linked list.
29  * Example:
30  *      struct parent {
31  *              const char *name;
32  *              struct list_head children;
33  *              unsigned int num_children;
34  *      };
35  */
36 struct list_head
37 {
38         struct list_node n;
39 };
40
41 /**
42  * list_check - check a list for consistency
43  * @h: the list_head
44  * @abortstr: the location to print on aborting, or NULL.
45  *
46  * Because list_nodes have redundant information, consistency checking between
47  * the back and forward links can be done.  This is useful as a debugging check.
48  * If @abortstr is non-NULL, that will be printed in a diagnostic if the list
49  * is inconsistent, and the function will abort.
50  *
51  * Returns the list head if the list is consistent, NULL if not (it
52  * can never return NULL if @abortstr is set).
53  *
54  * Example:
55  *      static void dump_parent(struct parent *p)
56  *      {
57  *              struct child *c;
58  *
59  *              printf("%s (%u children):\n", p->name, p->num_children);
60  *              list_check(&p->children, "bad child list");
61  *              list_for_each(&p->children, c, list)
62  *                      printf(" -> %s\n", c->name);
63  *      }
64  */
65 struct list_head *list_check(const struct list_head *h, const char *abortstr);
66
67 #ifdef CCAN_LIST_DEBUG
68 #define debug_list(h) list_check((h), __func__)
69 #else
70 #define debug_list(h) (h)
71 #endif
72
73 /**
74  * LIST_HEAD - define and initalize an empty list_head
75  * @name: the name of the list.
76  *
77  * The LIST_HEAD macro defines a list_head and initializes it to an empty
78  * list.  It can be prepended by "static" to define a static list_head.
79  *
80  * Example:
81  *      static LIST_HEAD(my_global_list);
82  */
83 #define LIST_HEAD(name) \
84         struct list_head name = { { &name.n, &name.n } }
85
86 /**
87  * list_head_init - initialize a list_head
88  * @h: the list_head to set to the empty list
89  *
90  * Example:
91  *      ...
92  *      struct parent *parent = malloc(sizeof(*parent));
93  *
94  *      list_head_init(&parent->children);
95  *      parent->num_children = 0;
96  */
97 static inline void list_head_init(struct list_head *h)
98 {
99         h->n.next = h->n.prev = &h->n;
100 }
101
102 /**
103  * list_add - add an entry at the start of a linked list.
104  * @h: the list_head to add the node to
105  * @n: the list_node to add to the list.
106  *
107  * The list_node does not need to be initialized; it will be overwritten.
108  * Example:
109  *      struct child *child = malloc(sizeof(*child));
110  *
111  *      child->name = "marvin";
112  *      list_add(&parent->children, &child->list);
113  *      parent->num_children++;
114  */
115 static inline void list_add(struct list_head *h, struct list_node *n)
116 {
117         n->next = h->n.next;
118         n->prev = &h->n;
119         h->n.next->prev = n;
120         h->n.next = n;
121         (void)debug_list(h);
122 }
123
124 /**
125  * list_add_tail - add an entry at the end of a linked list.
126  * @h: the list_head to add the node to
127  * @n: the list_node to add to the list.
128  *
129  * The list_node does not need to be initialized; it will be overwritten.
130  * Example:
131  *      list_add_tail(&parent->children, &child->list);
132  *      parent->num_children++;
133  */
134 static inline void list_add_tail(struct list_head *h, struct list_node *n)
135 {
136         n->next = &h->n;
137         n->prev = h->n.prev;
138         h->n.prev->next = n;
139         h->n.prev = n;
140         (void)debug_list(h);
141 }
142
143 /**
144  * list_del - delete an entry from a linked list.
145  * @n: the list_node to delete from the list.
146  *
147  * Example:
148  *      list_del(&child->list);
149  *      parent->num_children--;
150  */
151 static inline void list_del(struct list_node *n)
152 {
153         n->next->prev = n->prev;
154         n->prev->next = n->next;
155         (void)debug_list(n->next);
156 #ifdef CCAN_LIST_DEBUG
157         /* Catch use-after-del. */
158         n->next = n->prev = NULL;
159 #endif
160 }
161
162 /**
163  * list_empty - is a list empty?
164  * @h: the list_head
165  *
166  * If the list is empty, returns true.
167  *
168  * Example:
169  *      assert(list_empty(&parent->children) == (parent->num_children == 0));
170  */
171 static inline bool list_empty(const struct list_head *h)
172 {
173         (void)debug_list(h);
174         return h->n.next == &h->n;
175 }
176
177 /**
178  * list_entry - convert a list_node back into the structure containing it.
179  * @n: the list_node
180  * @type: the type of the entry
181  * @member: the list_node member of the type
182  *
183  * Example:
184  *      // First list entry is children.next; convert back to child.
185  *      child = list_entry(parent->children.n.next, struct child, list);
186  *
187  * See Also:
188  *      list_top(), list_for_each()
189  */
190 #define list_entry(n, type, member) container_of(n, type, member)
191
192 /**
193  * list_top - get the first entry in a list
194  * @h: the list_head
195  * @type: the type of the entry
196  * @member: the list_node member of the type
197  *
198  * If the list is empty, returns NULL.
199  *
200  * Example:
201  *      struct child *first;
202  *      first = list_top(&parent->children, struct child, list);
203  */
204 #define list_top(h, type, member) \
205         (list_empty(h) ? NULL : list_entry((h)->n.next, type, member))
206
207 /**
208  * list_tail - get the last entry in a list
209  * @h: the list_head
210  * @type: the type of the entry
211  * @member: the list_node member of the type
212  *
213  * If the list is empty, returns NULL.
214  *
215  * Example:
216  *      struct child *last;
217  *      last = list_tail(&parent->children, struct child, list);
218  */
219 #define list_tail(h, type, member) \
220         (list_empty(h) ? NULL : list_entry((h)->n.prev, type, member))
221
222 /**
223  * list_for_each - iterate through a list.
224  * @h: the list_head
225  * @i: the structure containing the list_node
226  * @member: the list_node member of the structure
227  *
228  * This is a convenient wrapper to iterate @i over the entire list.  It's
229  * a for loop, so you can break and continue as normal.
230  *
231  * Example:
232  *      list_for_each(&parent->children, child, list)
233  *              printf("Name: %s\n", child->name);
234  */
235 #define list_for_each(h, i, member)                                     \
236         for (i = container_of_var(debug_list(h)->n.next, i, member);    \
237              &i->member != &(h)->n;                                     \
238              i = container_of_var(i->member.next, i, member))
239
240 /**
241  * list_for_each_safe - iterate through a list, maybe during deletion
242  * @h: the list_head
243  * @i: the structure containing the list_node
244  * @nxt: the structure containing the list_node
245  * @member: the list_node member of the structure
246  *
247  * This is a convenient wrapper to iterate @i over the entire list.  It's
248  * a for loop, so you can break and continue as normal.  The extra variable
249  * @nxt is used to hold the next element, so you can delete @i from the list.
250  *
251  * Example:
252  *      struct child *next;
253  *      list_for_each_safe(&parent->children, child, next, list) {
254  *              list_del(&child->list);
255  *              parent->num_children--;
256  *      }
257  */
258 #define list_for_each_safe(h, i, nxt, member)                           \
259         for (i = container_of_var(debug_list(h)->n.next, i, member),    \
260                 nxt = container_of_var(i->member.next, i, member);      \
261              &i->member != &(h)->n;                                     \
262              i = nxt, nxt = container_of_var(i->member.next, i, member))
263 #endif /* CCAN_LIST_H */