]> wimlib.net Git - wimlib/blob - include/wimlib/list.h
6073c23b8088b317baa31cd7649a5860e25c7790
[wimlib] / include / wimlib / list.h
1
2 /*
3  * This file is based on include/linux/list.h in the Linux kernel source code.
4  */
5
6 #ifndef _LINUX_LIST_H
7 #define _LINUX_LIST_H
8
9 #include <stddef.h>
10
11 struct list_head {
12         struct list_head *next, *prev;
13 };
14 struct hlist_head {
15         struct hlist_node *first;
16 };
17
18 struct hlist_node {
19         struct hlist_node *next, **pprev;
20 };
21
22 /*
23  * Simple doubly linked list implementation.
24  *
25  * Some of the internal functions ("__xxx") are useful when
26  * manipulating whole lists rather than single entries, as
27  * sometimes we already know the next/prev entries and we can
28  * generate better code by using them directly rather than
29  * using the generic single-entry routines.
30  */
31
32 #define LIST_HEAD_INIT(name) { &(name), &(name) }
33
34 #ifdef LIST_HEAD /* BSD sys/queue.h defines this... */
35 #  undef LIST_HEAD
36 #endif
37
38 #define LIST_HEAD(name) \
39         struct list_head name = LIST_HEAD_INIT(name)
40
41 static inline void INIT_LIST_HEAD(struct list_head *list)
42 {
43         list->next = list;
44         list->prev = list;
45 }
46
47 /*
48  * Insert a new entry between two known consecutive entries.
49  *
50  * This is only for internal list manipulation where we know
51  * the prev/next entries already!
52  */
53 static inline void __list_add(struct list_head *new,
54                               struct list_head *prev,
55                               struct list_head *next)
56 {
57         next->prev = new;
58         new->next = next;
59         new->prev = prev;
60         prev->next = new;
61 }
62
63 /**
64  * list_add - add a new entry
65  * @new: new entry to be added
66  * @head: list head to add it after
67  *
68  * Insert a new entry after the specified head.
69  * This is good for implementing stacks.
70  */
71 static inline void list_add(struct list_head *new, struct list_head *head)
72 {
73         __list_add(new, head, head->next);
74 }
75
76 /**
77  * list_add_tail - add a new entry
78  * @new: new entry to be added
79  * @head: list head to add it before
80  *
81  * Insert a new entry before the specified head.
82  * This is useful for implementing queues.
83  */
84 static inline void list_add_tail(struct list_head *new, struct list_head *head)
85 {
86         __list_add(new, head->prev, head);
87 }
88
89 /**
90  * list_replace - replace old entry by new one
91  * @old : the element to be replaced
92  * @new : the new element to insert
93  *
94  * If @old was empty, it will be overwritten.
95  */
96 static inline void list_replace(struct list_head *old,
97                                 struct list_head *new)
98 {
99         new->next = old->next;
100         new->next->prev = new;
101         new->prev = old->prev;
102         new->prev->next = new;
103 }
104
105 static inline void list_replace_init(struct list_head *old,
106                                         struct list_head *new)
107 {
108         list_replace(old, new);
109         INIT_LIST_HEAD(old);
110 }
111
112 /*
113  * Delete a list entry by making the prev/next entries
114  * point to each other.
115  *
116  * This is only for internal list manipulation where we know
117  * the prev/next entries already!
118  */
119 static inline void __list_del(struct list_head * prev, struct list_head * next)
120 {
121         next->prev = prev;
122         prev->next = next;
123 }
124
125 /**
126  * list_del - deletes entry from list.
127  * @entry: the element to delete from the list.
128  * Note: list_empty() on entry does not return true after this, the entry is
129  * in an undefined state.
130  */
131 static inline void list_del(struct list_head *entry)
132 {
133         __list_del(entry->prev, entry->next);
134 }
135
136 /**
137  * list_del_init - deletes entry from list and reinitialize it.
138  * @entry: the element to delete from the list.
139  */
140 static inline void list_del_init(struct list_head *entry)
141 {
142         list_del(entry);
143         INIT_LIST_HEAD(entry);
144 }
145
146 /**
147  * list_empty - tests whether a list is empty
148  * @head: the list to test.
149  */
150 static inline int list_empty(const struct list_head *head)
151 {
152         return head->next == head;
153 }
154
155 /**
156  * list_is_singular - tests whether a list has just one entry.
157  * @head: the list to test.
158  */
159 static inline int list_is_singular(const struct list_head *head)
160 {
161         return !list_empty(head) && (head->next == head->prev);
162 }
163
164 static inline void __list_splice(const struct list_head *list,
165                                  struct list_head *prev,
166                                  struct list_head *next)
167 {
168         struct list_head *first = list->next;
169         struct list_head *last = list->prev;
170
171         first->prev = prev;
172         prev->next = first;
173
174         last->next = next;
175         next->prev = last;
176 }
177
178 /**
179  * list_splice - join two lists, this is designed for stacks
180  * @list: the new list to add.
181  * @head: the place to add it in the first list.
182  */
183 static inline void list_splice(const struct list_head *list,
184                                 struct list_head *head)
185 {
186         if (!list_empty(list))
187                 __list_splice(list, head, head->next);
188 }
189
190 /* Move the entire list @old to the list @new, overwriting it. */
191 static inline void list_transfer(struct list_head *old,
192                                  struct list_head *new)
193 {
194         struct list_head *prev, *next;
195
196         if (list_empty(old)) {
197                 INIT_LIST_HEAD(new);
198         } else {
199                 prev = old->prev;
200                 next = old->next;
201                 new->next = next;
202                 new->prev = prev;
203                 prev->next = new;
204                 next->prev = new;
205         }
206 }
207
208 /**
209  * list_move - delete from one list and add as another's head
210  * @list: the entry to move
211  * @head: the head that will precede our entry
212  */
213 static inline void list_move(struct list_head *list, struct list_head *head)
214 {
215         list_del(list);
216         list_add(list, head);
217 }
218
219 /**
220  * list_move_tail - delete from one list and add as another's tail
221  * @list: the entry to move
222  * @head: the head that will follow our entry
223  */
224 static inline void list_move_tail(struct list_head *list,
225                                   struct list_head *head)
226 {
227         list_del(list);
228         list_add_tail(list, head);
229 }
230
231 /**
232  * list_splice_tail - join two lists, each list being a queue
233  * @list: the new list to add.
234  * @head: the place to add it in the first list.
235  */
236 static inline void list_splice_tail(struct list_head *list,
237                                 struct list_head *head)
238 {
239         if (!list_empty(list))
240                 __list_splice(list, head->prev, head);
241 }
242
243 /**
244  * list_entry - get the struct for this entry
245  * @ptr:        the &struct list_head pointer.
246  * @type:       the type of the struct this is embedded in.
247  * @member:     the name of the list_struct within the struct.
248  */
249 #define list_entry(ptr, type, member) \
250         container_of(ptr, type, member)
251
252 /**
253  * list_first_entry - get the first element from a list
254  * @ptr:        the list head to take the element from.
255  * @type:       the type of the struct this is embedded in.
256  * @member:     the name of the list_struct within the struct.
257  *
258  * Note, that list is expected to be not empty.
259  */
260 #define list_first_entry(ptr, type, member) \
261         list_entry((ptr)->next, type, member)
262
263 /**
264  * list_last_entry - get the last element from a list
265  * @ptr:        the list head to take the element from.
266  * @type:       the type of the struct this is embedded in.
267  * @member:     the name of the list_struct within the struct.
268  *
269  * Note, that list is expected to be not empty.
270  */
271 #define list_last_entry(ptr, type, member) \
272         list_entry((ptr)->prev, type, member)
273
274 /**
275  * list_first_entry_or_null - get the first element from a list
276  * @ptr:        the list head to take the element from.
277  * @type:       the type of the struct this is embedded in.
278  * @member:     the name of the list_struct within the struct.
279  *
280  * Note that if the list is empty, it returns NULL.
281  */
282 #define list_first_entry_or_null(ptr, type, member) \
283         (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
284
285 /**
286  * list_next_entry - get the next element in list
287  * @pos:        the type * to cursor
288  * @member:     the name of the list_struct within the struct.
289  */
290 #define list_next_entry(pos, member) \
291         list_entry((pos)->member.next, typeof(*(pos)), member)
292
293 /**
294  * list_prev_entry - get the prev element in list
295  * @pos:        the type * to cursor
296  * @member:     the name of the list_struct within the struct.
297  */
298 #define list_prev_entry(pos, member) \
299         list_entry((pos)->member.prev, typeof(*(pos)), member)
300
301
302 /**
303  * list_for_each        -       iterate over a list
304  * @pos:        the &struct list_head to use as a loop cursor.
305  * @head:       the head for your list.
306  */
307 #define list_for_each(pos, head) \
308         for (pos = (head)->next; pos != (head); pos = pos->next)
309
310 /**
311  * list_for_each_safe - iterate over a list safe against removal of list entry
312  * @pos:        the &struct list_head to use as a loop cursor.
313  * @n:          another &struct list_head to use as temporary storage
314  * @head:       the head for your list.
315  */
316 #define list_for_each_safe(pos, n, head) \
317         for (pos = (head)->next, n = pos->next; pos != (head); \
318                 pos = n, n = pos->next)
319
320 /**
321  * list_for_each_entry  -       iterate over list of given type
322  * @pos:        the type * to use as a loop cursor.
323  * @head:       the head for your list.
324  * @member:     the name of the list_struct within the struct.
325  */
326 #define list_for_each_entry(pos, head, member)                          \
327         for (pos = list_first_entry(head, typeof(*pos), member);        \
328              &pos->member != (head);                                    \
329              pos = list_next_entry(pos, member))
330
331 /**
332  * list_for_each_entry_reverse - iterate backwards over list of given type.
333  * @pos:        the type * to use as a loop cursor.
334  * @head:       the head for your list.
335  * @member:     the name of the list_struct within the struct.
336  */
337 #define list_for_each_entry_reverse(pos, head, member)                  \
338         for (pos = list_last_entry(head, typeof(*pos), member);         \
339              &pos->member != (head);                                    \
340              pos = list_prev_entry(pos, member))
341
342 /**
343  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
344  * @pos:        the type * to use as a loop cursor.
345  * @n:          another type * to use as temporary storage
346  * @head:       the head for your list.
347  * @member:     the name of the list_struct within the struct.
348  */
349 #define list_for_each_entry_safe(pos, n, head, member)                  \
350         for (pos = list_entry((head)->next, typeof(*pos), member),      \
351                 n = list_entry(pos->member.next, typeof(*pos), member); \
352              &pos->member != (head);                                    \
353              pos = n, n = list_entry(n->member.next, typeof(*n), member))
354
355 /*
356  * Double linked lists with a single pointer list head.
357  * Mostly useful for hash tables where the two pointer list head is
358  * too wasteful.
359  * You lose the ability to access the tail in O(1).
360  */
361
362 #define HLIST_HEAD_INIT { .first = NULL }
363 #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
364 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
365 static inline void INIT_HLIST_NODE(struct hlist_node *h)
366 {
367         h->next = NULL;
368         h->pprev = NULL;
369 }
370
371 static inline int hlist_unhashed(const struct hlist_node *h)
372 {
373         return !h->pprev;
374 }
375
376 static inline int hlist_empty(const struct hlist_head *h)
377 {
378         return !h->first;
379 }
380
381 static inline void __hlist_del(struct hlist_node *n)
382 {
383         struct hlist_node *next = n->next;
384         struct hlist_node **pprev = n->pprev;
385         *pprev = next;
386         if (next)
387                 next->pprev = pprev;
388 }
389
390 static inline void hlist_del(struct hlist_node *n)
391 {
392         __hlist_del(n);
393 #if 0
394         n->next = LIST_POISON1;
395         n->pprev = LIST_POISON2;
396 #endif
397 }
398
399 static inline void hlist_del_init(struct hlist_node *n)
400 {
401         if (!hlist_unhashed(n)) {
402                 __hlist_del(n);
403                 INIT_HLIST_NODE(n);
404         }
405 }
406
407 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
408 {
409         struct hlist_node *first = h->first;
410         n->next = first;
411         if (first)
412                 first->pprev = &n->next;
413         h->first = n;
414         n->pprev = &h->first;
415 }
416
417 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
418
419 #define hlist_for_each(pos, head) \
420         for (pos = (head)->first; pos ; pos = pos->next)
421
422 #define hlist_for_each_safe(pos, n, head) \
423         for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
424              pos = n)
425
426 /**
427  * hlist_for_each_entry - iterate over list of given type
428  * @tpos:       the type * to use as a loop cursor.
429  * @pos:        the &struct hlist_node to use as a loop cursor.
430  * @head:       the head for your list.
431  * @member:     the name of the hlist_node within the struct.
432  */
433 #define hlist_for_each_entry(tpos, pos, head, member)                    \
434         for (pos = (head)->first;                                        \
435              pos &&                                                      \
436                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
437              pos = pos->next)
438
439 /**
440  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
441  * @tpos:       the type * to use as a loop cursor.
442  * @pos:        the &struct hlist_node to use as a loop cursor.
443  * @n:          another &struct hlist_node to use as temporary storage
444  * @head:       the head for your list.
445  * @member:     the name of the hlist_node within the struct.
446  */
447 #define hlist_for_each_entry_safe(tpos, pos, n, head, member)            \
448         for (pos = (head)->first;                                        \
449              pos && ({ n = pos->next; 1; }) &&                           \
450                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
451              pos = n)
452
453 #endif