]> wimlib.net Git - wimlib/blob - include/wimlib/list.h
d6bd55c11013b54dadb2d0e615882058ace32e64
[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  * Delete a list entry by making the prev/next entries
91  * point to each other.
92  *
93  * This is only for internal list manipulation where we know
94  * the prev/next entries already!
95  */
96 static inline void __list_del(struct list_head * prev, struct list_head * next)
97 {
98         next->prev = prev;
99         prev->next = next;
100 }
101
102 /**
103  * list_del - deletes entry from list.
104  * @entry: the element to delete from the list.
105  * Note: list_empty() on entry does not return true after this, the entry is
106  * in an undefined state.
107  */
108 static inline void list_del(struct list_head *entry)
109 {
110         __list_del(entry->prev, entry->next);
111 }
112
113 /**
114  * list_del_init - deletes entry from list and reinitialize it.
115  * @entry: the element to delete from the list.
116  */
117 static inline void list_del_init(struct list_head *entry)
118 {
119         list_del(entry);
120         INIT_LIST_HEAD(entry);
121 }
122
123 /**
124  * list_empty - tests whether a list is empty
125  * @head: the list to test.
126  */
127 static inline int list_empty(const struct list_head *head)
128 {
129         return head->next == head;
130 }
131
132 /**
133  * list_is_singular - tests whether a list has just one entry.
134  * @head: the list to test.
135  */
136 static inline int list_is_singular(const struct list_head *head)
137 {
138         return !list_empty(head) && (head->next == head->prev);
139 }
140
141 static inline void __list_splice(const struct list_head *list,
142                                  struct list_head *prev,
143                                  struct list_head *next)
144 {
145         struct list_head *first = list->next;
146         struct list_head *last = list->prev;
147
148         first->prev = prev;
149         prev->next = first;
150
151         last->next = next;
152         next->prev = last;
153 }
154
155 /**
156  * list_splice - join two lists, this is designed for stacks
157  * @list: the new list to add.
158  * @head: the place to add it in the first list.
159  */
160 static inline void list_splice(const struct list_head *list,
161                                 struct list_head *head)
162 {
163         if (!list_empty(list))
164                 __list_splice(list, head, head->next);
165 }
166
167 /* Move the entire list @old to the list @new, overwriting it. */
168 static inline void list_transfer(struct list_head *old,
169                                  struct list_head *new)
170 {
171         struct list_head *prev, *next;
172
173         if (list_empty(old)) {
174                 INIT_LIST_HEAD(new);
175         } else {
176                 prev = old->prev;
177                 next = old->next;
178                 new->next = next;
179                 new->prev = prev;
180                 prev->next = new;
181                 next->prev = new;
182         }
183 }
184
185 /**
186  * list_move - delete from one list and add as another's head
187  * @list: the entry to move
188  * @head: the head that will precede our entry
189  */
190 static inline void list_move(struct list_head *list, struct list_head *head)
191 {
192         list_del(list);
193         list_add(list, head);
194 }
195
196 /**
197  * list_move_tail - delete from one list and add as another's tail
198  * @list: the entry to move
199  * @head: the head that will follow our entry
200  */
201 static inline void list_move_tail(struct list_head *list,
202                                   struct list_head *head)
203 {
204         list_del(list);
205         list_add_tail(list, head);
206 }
207
208 /**
209  * list_splice_tail - join two lists, each list being a queue
210  * @list: the new list to add.
211  * @head: the place to add it in the first list.
212  */
213 static inline void list_splice_tail(struct list_head *list,
214                                 struct list_head *head)
215 {
216         if (!list_empty(list))
217                 __list_splice(list, head->prev, head);
218 }
219
220 /**
221  * list_entry - get the struct for this entry
222  * @ptr:        the &struct list_head pointer.
223  * @type:       the type of the struct this is embedded in.
224  * @member:     the name of the list_struct within the struct.
225  */
226 #define list_entry(ptr, type, member) \
227         container_of(ptr, type, member)
228
229 /**
230  * list_for_each        -       iterate over a list
231  * @pos:        the &struct list_head to use as a loop cursor.
232  * @head:       the head for your list.
233  */
234 #define list_for_each(pos, head) \
235         for (pos = (head)->next; pos != (head); pos = pos->next)
236
237 /**
238  * list_for_each_safe - iterate over a list safe against removal of list entry
239  * @pos:        the &struct list_head to use as a loop cursor.
240  * @n:          another &struct list_head to use as temporary storage
241  * @head:       the head for your list.
242  */
243 #define list_for_each_safe(pos, n, head) \
244         for (pos = (head)->next, n = pos->next; pos != (head); \
245                 pos = n, n = pos->next)
246
247 /**
248  * list_for_each_entry  -       iterate over list of given type
249  * @pos:        the type * to use as a loop cursor.
250  * @head:       the head for your list.
251  * @member:     the name of the list_struct within the struct.
252  */
253 #define list_for_each_entry(pos, head, member)                          \
254         for (pos = list_entry((head)->next, typeof(*pos), member);      \
255              &pos->member != (head);    \
256              pos = list_entry(pos->member.next, typeof(*pos), member))
257
258 /**
259  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
260  * @pos:        the type * to use as a loop cursor.
261  * @n:          another type * to use as temporary storage
262  * @head:       the head for your list.
263  * @member:     the name of the list_struct within the struct.
264  */
265 #define list_for_each_entry_safe(pos, n, head, member)                  \
266         for (pos = list_entry((head)->next, typeof(*pos), member),      \
267                 n = list_entry(pos->member.next, typeof(*pos), member); \
268              &pos->member != (head);                                    \
269              pos = n, n = list_entry(n->member.next, typeof(*n), member))
270
271 /*
272  * Double linked lists with a single pointer list head.
273  * Mostly useful for hash tables where the two pointer list head is
274  * too wasteful.
275  * You lose the ability to access the tail in O(1).
276  */
277
278 #define HLIST_HEAD_INIT { .first = NULL }
279 #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
280 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
281 static inline void INIT_HLIST_NODE(struct hlist_node *h)
282 {
283         h->next = NULL;
284         h->pprev = NULL;
285 }
286
287 static inline int hlist_unhashed(const struct hlist_node *h)
288 {
289         return !h->pprev;
290 }
291
292 static inline int hlist_empty(const struct hlist_head *h)
293 {
294         return !h->first;
295 }
296
297 static inline void __hlist_del(struct hlist_node *n)
298 {
299         struct hlist_node *next = n->next;
300         struct hlist_node **pprev = n->pprev;
301         *pprev = next;
302         if (next)
303                 next->pprev = pprev;
304 }
305
306 static inline void hlist_del(struct hlist_node *n)
307 {
308         __hlist_del(n);
309 #if 0
310         n->next = LIST_POISON1;
311         n->pprev = LIST_POISON2;
312 #endif
313 }
314
315 static inline void hlist_del_init(struct hlist_node *n)
316 {
317         if (!hlist_unhashed(n)) {
318                 __hlist_del(n);
319                 INIT_HLIST_NODE(n);
320         }
321 }
322
323 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
324 {
325         struct hlist_node *first = h->first;
326         n->next = first;
327         if (first)
328                 first->pprev = &n->next;
329         h->first = n;
330         n->pprev = &h->first;
331 }
332
333 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
334
335 #define hlist_for_each(pos, head) \
336         for (pos = (head)->first; pos ; pos = pos->next)
337
338 #define hlist_for_each_safe(pos, n, head) \
339         for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
340              pos = n)
341
342 /**
343  * hlist_for_each_entry - iterate over list of given type
344  * @tpos:       the type * to use as a loop cursor.
345  * @pos:        the &struct hlist_node to use as a loop cursor.
346  * @head:       the head for your list.
347  * @member:     the name of the hlist_node within the struct.
348  */
349 #define hlist_for_each_entry(tpos, pos, head, member)                    \
350         for (pos = (head)->first;                                        \
351              pos &&                                                      \
352                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
353              pos = pos->next)
354
355 /**
356  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
357  * @tpos:       the type * to use as a loop cursor.
358  * @pos:        the &struct hlist_node to use as a loop cursor.
359  * @n:          another &struct hlist_node to use as temporary storage
360  * @head:       the head for your list.
361  * @member:     the name of the hlist_node within the struct.
362  */
363 #define hlist_for_each_entry_safe(tpos, pos, n, head, member)            \
364         for (pos = (head)->first;                                        \
365              pos && ({ n = pos->next; 1; }) &&                           \
366                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
367              pos = n)
368
369 #endif