]> git.ozlabs.org Git - ccan/blob - ccan/list/list.h
list: implement list_check_node to check list from node rather than head
[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 head of 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  * See also: list_check_node()
55  *
56  * Example:
57  *      static void dump_parent(struct parent *p)
58  *      {
59  *              struct child *c;
60  *
61  *              printf("%s (%u children):\n", p->name, p->num_children);
62  *              list_check(&p->children, "bad child list");
63  *              list_for_each(&p->children, c, list)
64  *                      printf(" -> %s\n", c->name);
65  *      }
66  */
67 struct list_head *list_check(const struct list_head *h, const char *abortstr);
68
69 /**
70  * list_check_node - check node of a list for consistency
71  * @n: the list_node
72  * @abortstr: the location to print on aborting, or NULL.
73  *
74  * Check consistency of the list node is in (it must be in one).
75  *
76  * See also: list_check()
77  *
78  * Example:
79  *      static void dump_child(const struct child *c)
80  *      {
81  *              list_check_node(&c->list, "bad child list");
82  *              printf("%s\n", c->name);
83  *      }
84  */
85 struct list_node *list_check_node(const struct list_node *n,
86                                   const char *abortstr);
87
88 #ifdef CCAN_LIST_DEBUG
89 #define debug_list(h) list_check((h), __func__)
90 #else
91 #define debug_list(h) (h)
92 #endif
93
94 /**
95  * LIST_HEAD - define and initalize an empty list_head
96  * @name: the name of the list.
97  *
98  * The LIST_HEAD macro defines a list_head and initializes it to an empty
99  * list.  It can be prepended by "static" to define a static list_head.
100  *
101  * Example:
102  *      static LIST_HEAD(my_global_list);
103  */
104 #define LIST_HEAD(name) \
105         struct list_head name = { { &name.n, &name.n } }
106
107 /**
108  * list_head_init - initialize a list_head
109  * @h: the list_head to set to the empty list
110  *
111  * Example:
112  *      ...
113  *      struct parent *parent = malloc(sizeof(*parent));
114  *
115  *      list_head_init(&parent->children);
116  *      parent->num_children = 0;
117  */
118 static inline void list_head_init(struct list_head *h)
119 {
120         h->n.next = h->n.prev = &h->n;
121 }
122
123 /**
124  * list_add - add an entry at the start of a linked list.
125  * @h: the list_head to add the node to
126  * @n: the list_node to add to the list.
127  *
128  * The list_node does not need to be initialized; it will be overwritten.
129  * Example:
130  *      struct child *child = malloc(sizeof(*child));
131  *
132  *      child->name = "marvin";
133  *      list_add(&parent->children, &child->list);
134  *      parent->num_children++;
135  */
136 static inline void list_add(struct list_head *h, struct list_node *n)
137 {
138         n->next = h->n.next;
139         n->prev = &h->n;
140         h->n.next->prev = n;
141         h->n.next = n;
142         (void)debug_list(h);
143 }
144
145 /**
146  * list_add_tail - add an entry at the end of a linked list.
147  * @h: the list_head to add the node to
148  * @n: the list_node to add to the list.
149  *
150  * The list_node does not need to be initialized; it will be overwritten.
151  * Example:
152  *      list_add_tail(&parent->children, &child->list);
153  *      parent->num_children++;
154  */
155 static inline void list_add_tail(struct list_head *h, struct list_node *n)
156 {
157         n->next = &h->n;
158         n->prev = h->n.prev;
159         h->n.prev->next = n;
160         h->n.prev = n;
161         (void)debug_list(h);
162 }
163
164 /**
165  * list_del - delete an entry from a linked list.
166  * @n: the list_node to delete from the list.
167  *
168  * Example:
169  *      list_del(&child->list);
170  *      parent->num_children--;
171  */
172 static inline void list_del(struct list_node *n)
173 {
174         n->next->prev = n->prev;
175         n->prev->next = n->next;
176         (void)debug_list(n->next);
177 #ifdef CCAN_LIST_DEBUG
178         /* Catch use-after-del. */
179         n->next = n->prev = NULL;
180 #endif
181 }
182
183 /**
184  * list_empty - is a list empty?
185  * @h: the list_head
186  *
187  * If the list is empty, returns true.
188  *
189  * Example:
190  *      assert(list_empty(&parent->children) == (parent->num_children == 0));
191  */
192 static inline bool list_empty(const struct list_head *h)
193 {
194         (void)debug_list(h);
195         return h->n.next == &h->n;
196 }
197
198 /**
199  * list_entry - convert a list_node back into the structure containing it.
200  * @n: the list_node
201  * @type: the type of the entry
202  * @member: the list_node member of the type
203  *
204  * Example:
205  *      // First list entry is children.next; convert back to child.
206  *      child = list_entry(parent->children.n.next, struct child, list);
207  *
208  * See Also:
209  *      list_top(), list_for_each()
210  */
211 #define list_entry(n, type, member) container_of(n, type, member)
212
213 /**
214  * list_top - get the first entry in a list
215  * @h: the list_head
216  * @type: the type of the entry
217  * @member: the list_node member of the type
218  *
219  * If the list is empty, returns NULL.
220  *
221  * Example:
222  *      struct child *first;
223  *      first = list_top(&parent->children, struct child, list);
224  */
225 #define list_top(h, type, member) \
226         (list_empty(h) ? NULL : list_entry((h)->n.next, type, member))
227
228 /**
229  * list_tail - get the last entry in a list
230  * @h: the list_head
231  * @type: the type of the entry
232  * @member: the list_node member of the type
233  *
234  * If the list is empty, returns NULL.
235  *
236  * Example:
237  *      struct child *last;
238  *      last = list_tail(&parent->children, struct child, list);
239  */
240 #define list_tail(h, type, member) \
241         (list_empty(h) ? NULL : list_entry((h)->n.prev, type, member))
242
243 /**
244  * list_for_each - iterate through a list.
245  * @h: the list_head
246  * @i: the structure containing the list_node
247  * @member: the list_node member of the structure
248  *
249  * This is a convenient wrapper to iterate @i over the entire list.  It's
250  * a for loop, so you can break and continue as normal.
251  *
252  * Example:
253  *      list_for_each(&parent->children, child, list)
254  *              printf("Name: %s\n", child->name);
255  */
256 #define list_for_each(h, i, member)                                     \
257         for (i = container_of_var(debug_list(h)->n.next, i, member);    \
258              &i->member != &(h)->n;                                     \
259              i = container_of_var(i->member.next, i, member))
260
261 /**
262  * list_for_each_safe - iterate through a list, maybe during deletion
263  * @h: the list_head
264  * @i: the structure containing the list_node
265  * @nxt: the structure containing the list_node
266  * @member: the list_node member of the structure
267  *
268  * This is a convenient wrapper to iterate @i over the entire list.  It's
269  * a for loop, so you can break and continue as normal.  The extra variable
270  * @nxt is used to hold the next element, so you can delete @i from the list.
271  *
272  * Example:
273  *      struct child *next;
274  *      list_for_each_safe(&parent->children, child, next, list) {
275  *              list_del(&child->list);
276  *              parent->num_children--;
277  *      }
278  */
279 #define list_for_each_safe(h, i, nxt, member)                           \
280         for (i = container_of_var(debug_list(h)->n.next, i, member),    \
281                 nxt = container_of_var(i->member.next, i, member);      \
282              &i->member != &(h)->n;                                     \
283              i = nxt, nxt = container_of_var(i->member.next, i, member))
284 #endif /* CCAN_LIST_H */