]> wimlib.net Git - wimlib/blob - include/wimlib/list.h
wimcapture.1: 7-Zip 15.12 supports LZMS decompression
[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 _WIMLIB_LIST_H
7 #define _WIMLIB_LIST_H
8
9 #include <stdbool.h>
10 #include <stddef.h>
11
12 /* Simple doubly linked list implementation.  */
13
14 struct list_head {
15         struct list_head *next;
16         struct list_head *prev;
17 };
18
19
20 #define LIST_HEAD_INIT(name) { &(name), &(name) }
21
22 #undef LIST_HEAD /* BSD sys/queue.h defines this... */
23 #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
24
25 static inline void
26 INIT_LIST_HEAD(struct list_head *list)
27 {
28         list->next = list;
29         list->prev = list;
30 }
31
32 /*
33  * Insert a new entry between two known consecutive entries.
34  *
35  * This is only for internal list manipulation where we know
36  * the prev/next entries already!
37  */
38 static inline void
39 __list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
40 {
41         next->prev = new;
42         new->next = next;
43         new->prev = prev;
44         prev->next = new;
45 }
46
47 /**
48  * list_add - add a new entry
49  * @new: new entry to be added
50  * @head: list head to add it after
51  *
52  * Insert a new entry after the specified head.
53  * This is good for implementing stacks.
54  */
55 static inline void
56 list_add(struct list_head *new, struct list_head *head)
57 {
58         __list_add(new, head, head->next);
59 }
60
61 /**
62  * list_add_tail - add a new entry
63  * @new: new entry to be added
64  * @head: list head to add it before
65  *
66  * Insert a new entry before the specified head.
67  * This is useful for implementing queues.
68  */
69 static inline void
70 list_add_tail(struct list_head *new, struct list_head *head)
71 {
72         __list_add(new, head->prev, head);
73 }
74
75 /**
76  * list_replace - replace old entry by new one
77  * @old : the element to be replaced
78  * @new : the new element to insert
79  *
80  * If @old was empty, it will be overwritten.
81  */
82 static inline void
83 list_replace(struct list_head *old, struct list_head *new)
84 {
85         new->next = old->next;
86         new->next->prev = new;
87         new->prev = old->prev;
88         new->prev->next = new;
89 }
90
91 /**
92  * list_del - deletes entry from list.
93  * @entry: the element to delete from the list.
94  * Note: list_empty() on entry does not return true after this, the entry is
95  * in an undefined state.
96  */
97 static inline void
98 list_del(struct list_head *entry)
99 {
100         struct list_head *prev = entry->prev;
101         struct list_head *next = entry->next;
102
103         prev->next = next;
104         next->prev = prev;
105 }
106
107 /**
108  * list_empty - tests whether a list is empty
109  * @head: the list to test.
110  */
111 static inline bool
112 list_empty(const struct list_head *head)
113 {
114         return head->next == head;
115 }
116
117 static inline void
118 __list_splice(const struct list_head *list,
119               struct list_head *prev, struct list_head *next)
120 {
121         struct list_head *first = list->next;
122         struct list_head *last = list->prev;
123
124         first->prev = prev;
125         prev->next = first;
126
127         last->next = next;
128         next->prev = last;
129 }
130
131 /**
132  * list_splice - join two lists, this is designed for stacks
133  * @list: the new list to add.
134  * @head: the place to add it in the first list.
135  */
136 static inline void
137 list_splice(const struct list_head *list, struct list_head *head)
138 {
139         if (!list_empty(list))
140                 __list_splice(list, head, head->next);
141 }
142
143 /**
144  * list_move - delete from one list and add as another's head
145  * @list: the entry to move
146  * @head: the head that will precede our entry
147  */
148 static inline void
149 list_move(struct list_head *list, struct list_head *head)
150 {
151         list_del(list);
152         list_add(list, head);
153 }
154
155 /**
156  * list_move_tail - delete from one list and add as another's tail
157  * @list: the entry to move
158  * @head: the head that will follow our entry
159  */
160 static inline void
161 list_move_tail(struct list_head *list, struct list_head *head)
162 {
163         list_del(list);
164         list_add_tail(list, head);
165 }
166
167 /**
168  * list_splice_tail - join two lists, each list being a queue
169  * @list: the new list to add.
170  * @head: the place to add it in the first list.
171  */
172 static inline void
173 list_splice_tail(struct list_head *list, struct list_head *head)
174 {
175         if (!list_empty(list))
176                 __list_splice(list, head->prev, head);
177 }
178
179 /**
180  * list_entry - get the struct for this entry
181  * @ptr:        the &struct list_head pointer.
182  * @type:       the type of the struct this is embedded in.
183  * @member:     the name of the list_struct within the struct.
184  */
185 #define list_entry(ptr, type, member) \
186         container_of(ptr, type, member)
187
188 /**
189  * list_first_entry - get the first element from a list
190  * @ptr:        the list head to take the element from.
191  * @type:       the type of the struct this is embedded in.
192  * @member:     the name of the list_struct within the struct.
193  *
194  * Note, that list is expected to be not empty.
195  */
196 #define list_first_entry(ptr, type, member) \
197         list_entry((ptr)->next, type, member)
198
199 /**
200  * list_last_entry - get the last element from a list
201  * @ptr:        the list head to take the element from.
202  * @type:       the type of the struct this is embedded in.
203  * @member:     the name of the list_struct within the struct.
204  *
205  * Note, that list is expected to be not empty.
206  */
207 #define list_last_entry(ptr, type, member) \
208         list_entry((ptr)->prev, type, member)
209
210 /**
211  * list_next_entry - get the next element in list
212  * @pos:        the type * to cursor
213  * @member:     the name of the list_struct within the struct.
214  */
215 #define list_next_entry(pos, member) \
216         list_entry((pos)->member.next, typeof(*(pos)), member)
217
218 /**
219  * list_prev_entry - get the prev element in list
220  * @pos:        the type * to cursor
221  * @member:     the name of the list_struct within the struct.
222  */
223 #define list_prev_entry(pos, member) \
224         list_entry((pos)->member.prev, typeof(*(pos)), member)
225
226
227 /**
228  * list_for_each        -       iterate over a list
229  * @pos:        the &struct list_head to use as a loop cursor.
230  * @head:       the head for your list.
231  */
232 #define list_for_each(pos, head) \
233         for (pos = (head)->next; pos != (head); pos = pos->next)
234
235 /**
236  * list_for_each_entry  -       iterate over list of given type
237  * @pos:        the type * to use as a loop cursor.
238  * @head:       the head for your list.
239  * @member:     the name of the list_struct within the struct.
240  */
241 #define list_for_each_entry(pos, head, member)                          \
242         for (pos = list_first_entry(head, typeof(*pos), member);        \
243              &pos->member != (head);                                    \
244              pos = list_next_entry(pos, member))
245
246 /**
247  * list_for_each_entry_reverse - iterate backwards over list of given type.
248  * @pos:        the type * to use as a loop cursor.
249  * @head:       the head for your list.
250  * @member:     the name of the list_struct within the struct.
251  */
252 #define list_for_each_entry_reverse(pos, head, member)                  \
253         for (pos = list_last_entry(head, typeof(*pos), member);         \
254              &pos->member != (head);                                    \
255              pos = list_prev_entry(pos, member))
256
257 /**
258  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
259  * @pos:        the type * to use as a loop cursor.
260  * @n:          another type * to use as temporary storage
261  * @head:       the head for your list.
262  * @member:     the name of the list_struct within the struct.
263  */
264 #define list_for_each_entry_safe(pos, n, head, member)                  \
265         for (pos = list_entry((head)->next, typeof(*pos), member),      \
266                 n = list_entry(pos->member.next, typeof(*pos), member); \
267              &pos->member != (head);                                    \
268              pos = n, n = list_entry(n->member.next, typeof(*n), member))
269
270 /*
271  * Double linked lists with a single pointer list head.
272  * Mostly useful for hash tables where the two pointer list head is
273  * too wasteful.
274  * You lose the ability to access the tail in O(1).
275  */
276
277 struct hlist_head {
278         struct hlist_node *first;
279 };
280
281 struct hlist_node {
282         struct hlist_node *next;
283         struct hlist_node **pprev;
284 };
285
286 static inline void
287 INIT_HLIST_HEAD(struct hlist_head *h)
288 {
289         h->first = NULL;
290 }
291
292 static inline bool
293 hlist_unhashed(const struct hlist_node *h)
294 {
295         return !h->pprev;
296 }
297
298 static inline bool
299 hlist_empty(const struct hlist_head *h)
300 {
301         return !h->first;
302 }
303
304 static inline void
305 hlist_del(struct hlist_node *n)
306 {
307         struct hlist_node *next = n->next;
308         struct hlist_node **pprev = n->pprev;
309         *pprev = next;
310         if (next)
311                 next->pprev = pprev;
312 }
313
314 static inline void
315 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
316 {
317         struct hlist_node *first = h->first;
318         n->next = first;
319         if (first)
320                 first->pprev = &n->next;
321         h->first = n;
322         n->pprev = &h->first;
323 }
324
325 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
326
327 #define hlist_entry_safe(ptr, type, member) \
328         ({ typeof(ptr) ____ptr = (ptr); \
329            ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
330         })
331
332 /**
333  * hlist_for_each_entry - iterate over list of given type
334  * @pos:        the type * to use as a loop cursor.
335  * @head:       the head for your list.
336  * @member:     the name of the hlist_node within the struct.
337  */
338 #define hlist_for_each_entry(pos, head, member)                         \
339         for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
340              pos;                                                       \
341              pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
342
343 /**
344  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
345  * @pos:        the type * to use as a loop cursor.
346  * @n:          another &struct hlist_node to use as temporary storage
347  * @head:       the head for your list.
348  * @member:     the name of the hlist_node within the struct.
349  */
350 #define hlist_for_each_entry_safe(pos, n, head, member)                 \
351         for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
352              pos && ({ n = pos->member.next; 1; });                     \
353              pos = hlist_entry_safe(n, typeof(*pos), member))
354
355 #endif /* _WIMLIB_LIST_H */