]> git.ozlabs.org Git - ccan/blob - ccan/list/list.h
tdb2: unify tdb1_traverse into tdb_traverse
[ccan] / ccan / list / list.h
1 /* Licensed under LGPLv2.1+ - see LICENSE file for details */
2 #ifndef CCAN_LIST_H
3 #define CCAN_LIST_H
4 #include <stdbool.h>
5 #include <assert.h>
6 #include <ccan/container_of/container_of.h>
7
8 /**
9  * struct list_node - an entry in a doubly-linked list
10  * @next: next entry (self if empty)
11  * @prev: previous entry (self if empty)
12  *
13  * This is used as an entry in a linked list.
14  * Example:
15  *      struct child {
16  *              const char *name;
17  *              // Linked list of all us children.
18  *              struct list_node list;
19  *      };
20  */
21 struct list_node
22 {
23         struct list_node *next, *prev;
24 };
25
26 /**
27  * struct list_head - the head of a doubly-linked list
28  * @h: the list_head (containing next and prev pointers)
29  *
30  * This is used as the head of a linked list.
31  * Example:
32  *      struct parent {
33  *              const char *name;
34  *              struct list_head children;
35  *              unsigned int num_children;
36  *      };
37  */
38 struct list_head
39 {
40         struct list_node n;
41 };
42
43 /**
44  * list_check - check head of a list for consistency
45  * @h: the list_head
46  * @abortstr: the location to print on aborting, or NULL.
47  *
48  * Because list_nodes have redundant information, consistency checking between
49  * the back and forward links can be done.  This is useful as a debugging check.
50  * If @abortstr is non-NULL, that will be printed in a diagnostic if the list
51  * is inconsistent, and the function will abort.
52  *
53  * Returns the list head if the list is consistent, NULL if not (it
54  * can never return NULL if @abortstr is set).
55  *
56  * See also: list_check_node()
57  *
58  * Example:
59  *      static void dump_parent(struct parent *p)
60  *      {
61  *              struct child *c;
62  *
63  *              printf("%s (%u children):\n", p->name, p->num_children);
64  *              list_check(&p->children, "bad child list");
65  *              list_for_each(&p->children, c, list)
66  *                      printf(" -> %s\n", c->name);
67  *      }
68  */
69 struct list_head *list_check(const struct list_head *h, const char *abortstr);
70
71 /**
72  * list_check_node - check node of a list for consistency
73  * @n: the list_node
74  * @abortstr: the location to print on aborting, or NULL.
75  *
76  * Check consistency of the list node is in (it must be in one).
77  *
78  * See also: list_check()
79  *
80  * Example:
81  *      static void dump_child(const struct child *c)
82  *      {
83  *              list_check_node(&c->list, "bad child list");
84  *              printf("%s\n", c->name);
85  *      }
86  */
87 struct list_node *list_check_node(const struct list_node *n,
88                                   const char *abortstr);
89
90 #ifdef CCAN_LIST_DEBUG
91 #define list_debug(h) list_check((h), __func__)
92 #define list_debug_node(n) list_check_node((n), __func__)
93 #else
94 #define list_debug(h) (h)
95 #define list_debug_node(n) (n)
96 #endif
97
98 /**
99  * LIST_HEAD_INIT - initializer for an empty list_head
100  * @name: the name of the list.
101  *
102  * Explicit initializer for an empty list.
103  *
104  * See also:
105  *      LIST_HEAD, list_head_init()
106  *
107  * Example:
108  *      static struct list_head my_list = LIST_HEAD_INIT(my_list);
109  */
110 #define LIST_HEAD_INIT(name) { { &name.n, &name.n } }
111
112 /**
113  * LIST_HEAD - define and initialize an empty list_head
114  * @name: the name of the list.
115  *
116  * The LIST_HEAD macro defines a list_head and initializes it to an empty
117  * list.  It can be prepended by "static" to define a static list_head.
118  *
119  * See also:
120  *      LIST_HEAD_INIT, list_head_init()
121  *
122  * Example:
123  *      static LIST_HEAD(my_global_list);
124  */
125 #define LIST_HEAD(name) \
126         struct list_head name = LIST_HEAD_INIT(name)
127
128 /**
129  * list_head_init - initialize a list_head
130  * @h: the list_head to set to the empty list
131  *
132  * Example:
133  *      ...
134  *      struct parent *parent = malloc(sizeof(*parent));
135  *
136  *      list_head_init(&parent->children);
137  *      parent->num_children = 0;
138  */
139 static inline void list_head_init(struct list_head *h)
140 {
141         h->n.next = h->n.prev = &h->n;
142 }
143
144 /**
145  * list_add - add an entry at the start of a linked list.
146  * @h: the list_head to add the node to
147  * @n: the list_node to add to the list.
148  *
149  * The list_node does not need to be initialized; it will be overwritten.
150  * Example:
151  *      struct child *child = malloc(sizeof(*child));
152  *
153  *      child->name = "marvin";
154  *      list_add(&parent->children, &child->list);
155  *      parent->num_children++;
156  */
157 static inline void list_add(struct list_head *h, struct list_node *n)
158 {
159         n->next = h->n.next;
160         n->prev = &h->n;
161         h->n.next->prev = n;
162         h->n.next = n;
163         (void)list_debug(h);
164 }
165
166 /**
167  * list_add_tail - add an entry at the end of a linked list.
168  * @h: the list_head to add the node to
169  * @n: the list_node to add to the list.
170  *
171  * The list_node does not need to be initialized; it will be overwritten.
172  * Example:
173  *      list_add_tail(&parent->children, &child->list);
174  *      parent->num_children++;
175  */
176 static inline void list_add_tail(struct list_head *h, struct list_node *n)
177 {
178         n->next = &h->n;
179         n->prev = h->n.prev;
180         h->n.prev->next = n;
181         h->n.prev = n;
182         (void)list_debug(h);
183 }
184
185 /**
186  * list_empty - is a list empty?
187  * @h: the list_head
188  *
189  * If the list is empty, returns true.
190  *
191  * Example:
192  *      assert(list_empty(&parent->children) == (parent->num_children == 0));
193  */
194 static inline bool list_empty(const struct list_head *h)
195 {
196         (void)list_debug(h);
197         return h->n.next == &h->n;
198 }
199
200 /**
201  * list_del - delete an entry from an (unknown) linked list.
202  * @n: the list_node to delete from the list.
203  *
204  * Note that this leaves @n in an undefined state; it can be added to
205  * another list, but not deleted again.
206  *
207  * See also:
208  *      list_del_from()
209  *
210  * Example:
211  *      list_del(&child->list);
212  *      parent->num_children--;
213  */
214 static inline void list_del(struct list_node *n)
215 {
216         (void)list_debug_node(n);
217         n->next->prev = n->prev;
218         n->prev->next = n->next;
219 #ifdef CCAN_LIST_DEBUG
220         /* Catch use-after-del. */
221         n->next = n->prev = NULL;
222 #endif
223 }
224
225 /**
226  * list_del_from - delete an entry from a known linked list.
227  * @h: the list_head the node is in.
228  * @n: the list_node to delete from the list.
229  *
230  * This explicitly indicates which list a node is expected to be in,
231  * which is better documentation and can catch more bugs.
232  *
233  * See also: list_del()
234  *
235  * Example:
236  *      list_del_from(&parent->children, &child->list);
237  *      parent->num_children--;
238  */
239 static inline void list_del_from(struct list_head *h, struct list_node *n)
240 {
241 #ifdef CCAN_LIST_DEBUG
242         {
243                 /* Thorough check: make sure it was in list! */
244                 struct list_node *i;
245                 for (i = h->n.next; i != n; i = i->next)
246                         assert(i != &h->n);
247         }
248 #endif /* CCAN_LIST_DEBUG */
249
250         /* Quick test that catches a surprising number of bugs. */
251         assert(!list_empty(h));
252         list_del(n);
253 }
254
255 /**
256  * list_entry - convert a list_node back into the structure containing it.
257  * @n: the list_node
258  * @type: the type of the entry
259  * @member: the list_node member of the type
260  *
261  * Example:
262  *      // First list entry is children.next; convert back to child.
263  *      child = list_entry(parent->children.n.next, struct child, list);
264  *
265  * See Also:
266  *      list_top(), list_for_each()
267  */
268 #define list_entry(n, type, member) container_of(n, type, member)
269
270 /**
271  * list_top - get the first entry in a list
272  * @h: the list_head
273  * @type: the type of the entry
274  * @member: the list_node member of the type
275  *
276  * If the list is empty, returns NULL.
277  *
278  * Example:
279  *      struct child *first;
280  *      first = list_top(&parent->children, struct child, list);
281  */
282 #define list_top(h, type, member) \
283         (list_empty(h) ? NULL : list_entry((h)->n.next, type, member))
284
285 /**
286  * list_tail - get the last entry in a list
287  * @h: the list_head
288  * @type: the type of the entry
289  * @member: the list_node member of the type
290  *
291  * If the list is empty, returns NULL.
292  *
293  * Example:
294  *      struct child *last;
295  *      last = list_tail(&parent->children, struct child, list);
296  */
297 #define list_tail(h, type, member) \
298         (list_empty(h) ? NULL : list_entry((h)->n.prev, type, member))
299
300 /**
301  * list_for_each - iterate through a list.
302  * @h: the list_head
303  * @i: the structure containing the list_node
304  * @member: the list_node member of the structure
305  *
306  * This is a convenient wrapper to iterate @i over the entire list.  It's
307  * a for loop, so you can break and continue as normal.
308  *
309  * Example:
310  *      list_for_each(&parent->children, child, list)
311  *              printf("Name: %s\n", child->name);
312  */
313 #define list_for_each(h, i, member)                                     \
314         for (i = container_of_var(list_debug(h)->n.next, i, member);    \
315              &i->member != &(h)->n;                                     \
316              i = container_of_var(i->member.next, i, member))
317
318 /**
319  * list_for_each_safe - iterate through a list, maybe during deletion
320  * @h: the list_head
321  * @i: the structure containing the list_node
322  * @nxt: the structure containing the list_node
323  * @member: the list_node member of the structure
324  *
325  * This is a convenient wrapper to iterate @i over the entire list.  It's
326  * a for loop, so you can break and continue as normal.  The extra variable
327  * @nxt is used to hold the next element, so you can delete @i from the list.
328  *
329  * Example:
330  *      struct child *next;
331  *      list_for_each_safe(&parent->children, child, next, list) {
332  *              list_del(&child->list);
333  *              parent->num_children--;
334  *      }
335  */
336 #define list_for_each_safe(h, i, nxt, member)                           \
337         for (i = container_of_var(list_debug(h)->n.next, i, member),    \
338                 nxt = container_of_var(i->member.next, i, member);      \
339              &i->member != &(h)->n;                                     \
340              i = nxt, nxt = container_of_var(i->member.next, i, member))
341 #endif /* CCAN_LIST_H */