]> git.ozlabs.org Git - ccan/blob - ccan/list/list.h
5b4f81c965725d473f7cfc6df52812c21fc7cfc4
[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 list_debug(h) list_check((h), __func__)
90 #define list_debug_node(n) list_check_node((n), __func__)
91 #else
92 #define list_debug(h) (h)
93 #define list_debug_node(n) (n)
94 #endif
95
96 /**
97  * LIST_HEAD - define and initalize an empty list_head
98  * @name: the name of the list.
99  *
100  * The LIST_HEAD macro defines a list_head and initializes it to an empty
101  * list.  It can be prepended by "static" to define a static list_head.
102  *
103  * Example:
104  *      static LIST_HEAD(my_global_list);
105  */
106 #define LIST_HEAD(name) \
107         struct list_head name = { { &name.n, &name.n } }
108
109 /**
110  * list_head_init - initialize a list_head
111  * @h: the list_head to set to the empty list
112  *
113  * Example:
114  *      ...
115  *      struct parent *parent = malloc(sizeof(*parent));
116  *
117  *      list_head_init(&parent->children);
118  *      parent->num_children = 0;
119  */
120 static inline void list_head_init(struct list_head *h)
121 {
122         h->n.next = h->n.prev = &h->n;
123 }
124
125 /**
126  * list_add - add an entry at the start of a linked list.
127  * @h: the list_head to add the node to
128  * @n: the list_node to add to the list.
129  *
130  * The list_node does not need to be initialized; it will be overwritten.
131  * Example:
132  *      struct child *child = malloc(sizeof(*child));
133  *
134  *      child->name = "marvin";
135  *      list_add(&parent->children, &child->list);
136  *      parent->num_children++;
137  */
138 static inline void list_add(struct list_head *h, struct list_node *n)
139 {
140         n->next = h->n.next;
141         n->prev = &h->n;
142         h->n.next->prev = n;
143         h->n.next = n;
144         (void)list_debug(h);
145 }
146
147 /**
148  * list_add_tail - add an entry at the end of a linked list.
149  * @h: the list_head to add the node to
150  * @n: the list_node to add to the list.
151  *
152  * The list_node does not need to be initialized; it will be overwritten.
153  * Example:
154  *      list_add_tail(&parent->children, &child->list);
155  *      parent->num_children++;
156  */
157 static inline void list_add_tail(struct list_head *h, struct list_node *n)
158 {
159         n->next = &h->n;
160         n->prev = h->n.prev;
161         h->n.prev->next = n;
162         h->n.prev = n;
163         (void)list_debug(h);
164 }
165
166 /**
167  * list_del - delete an entry from a linked list.
168  * @n: the list_node to delete from the list.
169  *
170  * Example:
171  *      list_del(&child->list);
172  *      parent->num_children--;
173  */
174 static inline void list_del(struct list_node *n)
175 {
176         (void)list_debug_node(n);
177         n->next->prev = n->prev;
178         n->prev->next = n->next;
179 #ifdef CCAN_LIST_DEBUG
180         /* Catch use-after-del. */
181         n->next = n->prev = NULL;
182 #endif
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_entry - convert a list_node back into the structure containing it.
202  * @n: the list_node
203  * @type: the type of the entry
204  * @member: the list_node member of the type
205  *
206  * Example:
207  *      // First list entry is children.next; convert back to child.
208  *      child = list_entry(parent->children.n.next, struct child, list);
209  *
210  * See Also:
211  *      list_top(), list_for_each()
212  */
213 #define list_entry(n, type, member) container_of(n, type, member)
214
215 /**
216  * list_top - get the first entry in a list
217  * @h: the list_head
218  * @type: the type of the entry
219  * @member: the list_node member of the type
220  *
221  * If the list is empty, returns NULL.
222  *
223  * Example:
224  *      struct child *first;
225  *      first = list_top(&parent->children, struct child, list);
226  */
227 #define list_top(h, type, member) \
228         (list_empty(h) ? NULL : list_entry((h)->n.next, type, member))
229
230 /**
231  * list_tail - get the last entry in a list
232  * @h: the list_head
233  * @type: the type of the entry
234  * @member: the list_node member of the type
235  *
236  * If the list is empty, returns NULL.
237  *
238  * Example:
239  *      struct child *last;
240  *      last = list_tail(&parent->children, struct child, list);
241  */
242 #define list_tail(h, type, member) \
243         (list_empty(h) ? NULL : list_entry((h)->n.prev, type, member))
244
245 /**
246  * list_for_each - iterate through a list.
247  * @h: the list_head
248  * @i: the structure containing the list_node
249  * @member: the list_node member of the structure
250  *
251  * This is a convenient wrapper to iterate @i over the entire list.  It's
252  * a for loop, so you can break and continue as normal.
253  *
254  * Example:
255  *      list_for_each(&parent->children, child, list)
256  *              printf("Name: %s\n", child->name);
257  */
258 #define list_for_each(h, i, member)                                     \
259         for (i = container_of_var(list_debug(h)->n.next, i, member);    \
260              &i->member != &(h)->n;                                     \
261              i = container_of_var(i->member.next, i, member))
262
263 /**
264  * list_for_each_safe - iterate through a list, maybe during deletion
265  * @h: the list_head
266  * @i: the structure containing the list_node
267  * @nxt: the structure containing the list_node
268  * @member: the list_node member of the structure
269  *
270  * This is a convenient wrapper to iterate @i over the entire list.  It's
271  * a for loop, so you can break and continue as normal.  The extra variable
272  * @nxt is used to hold the next element, so you can delete @i from the list.
273  *
274  * Example:
275  *      struct child *next;
276  *      list_for_each_safe(&parent->children, child, next, list) {
277  *              list_del(&child->list);
278  *              parent->num_children--;
279  *      }
280  */
281 #define list_for_each_safe(h, i, nxt, member)                           \
282         for (i = container_of_var(list_debug(h)->n.next, i, member),    \
283                 nxt = container_of_var(i->member.next, i, member);      \
284              &i->member != &(h)->n;                                     \
285              i = nxt, nxt = container_of_var(i->member.next, i, member))
286 #endif /* CCAN_LIST_H */