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