]> wimlib.net Git - wimlib/blob - include/wimlib/list.h
f59e113c994c52d217f3c9807af4b20c474446ec
[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 /* Move the entire list @old to the list @new, overwriting it. */
144 static inline void
145 list_transfer(struct list_head *old, struct list_head *new)
146 {
147         struct list_head *prev, *next;
148
149         if (list_empty(old)) {
150                 INIT_LIST_HEAD(new);
151         } else {
152                 prev = old->prev;
153                 next = old->next;
154                 new->next = next;
155                 new->prev = prev;
156                 prev->next = new;
157                 next->prev = new;
158         }
159 }
160
161 /**
162  * list_move - delete from one list and add as another's head
163  * @list: the entry to move
164  * @head: the head that will precede our entry
165  */
166 static inline void
167 list_move(struct list_head *list, struct list_head *head)
168 {
169         list_del(list);
170         list_add(list, head);
171 }
172
173 /**
174  * list_move_tail - delete from one list and add as another's tail
175  * @list: the entry to move
176  * @head: the head that will follow our entry
177  */
178 static inline void
179 list_move_tail(struct list_head *list, struct list_head *head)
180 {
181         list_del(list);
182         list_add_tail(list, head);
183 }
184
185 /**
186  * list_splice_tail - join two lists, each list being a queue
187  * @list: the new list to add.
188  * @head: the place to add it in the first list.
189  */
190 static inline void
191 list_splice_tail(struct list_head *list, struct list_head *head)
192 {
193         if (!list_empty(list))
194                 __list_splice(list, head->prev, head);
195 }
196
197 /**
198  * list_entry - get the struct for this entry
199  * @ptr:        the &struct list_head pointer.
200  * @type:       the type of the struct this is embedded in.
201  * @member:     the name of the list_struct within the struct.
202  */
203 #define list_entry(ptr, type, member) \
204         container_of(ptr, type, member)
205
206 /**
207  * list_first_entry - get the first element from a list
208  * @ptr:        the list head to take the element from.
209  * @type:       the type of the struct this is embedded in.
210  * @member:     the name of the list_struct within the struct.
211  *
212  * Note, that list is expected to be not empty.
213  */
214 #define list_first_entry(ptr, type, member) \
215         list_entry((ptr)->next, type, member)
216
217 /**
218  * list_last_entry - get the last element from a list
219  * @ptr:        the list head to take the element from.
220  * @type:       the type of the struct this is embedded in.
221  * @member:     the name of the list_struct within the struct.
222  *
223  * Note, that list is expected to be not empty.
224  */
225 #define list_last_entry(ptr, type, member) \
226         list_entry((ptr)->prev, type, member)
227
228 /**
229  * list_next_entry - get the next element in list
230  * @pos:        the type * to cursor
231  * @member:     the name of the list_struct within the struct.
232  */
233 #define list_next_entry(pos, member) \
234         list_entry((pos)->member.next, typeof(*(pos)), member)
235
236 /**
237  * list_prev_entry - get the prev element in list
238  * @pos:        the type * to cursor
239  * @member:     the name of the list_struct within the struct.
240  */
241 #define list_prev_entry(pos, member) \
242         list_entry((pos)->member.prev, typeof(*(pos)), member)
243
244
245 /**
246  * list_for_each        -       iterate over a list
247  * @pos:        the &struct list_head to use as a loop cursor.
248  * @head:       the head for your list.
249  */
250 #define list_for_each(pos, head) \
251         for (pos = (head)->next; pos != (head); pos = pos->next)
252
253 /**
254  * list_for_each_entry  -       iterate over list of given type
255  * @pos:        the type * to use as a loop cursor.
256  * @head:       the head for your list.
257  * @member:     the name of the list_struct within the struct.
258  */
259 #define list_for_each_entry(pos, head, member)                          \
260         for (pos = list_first_entry(head, typeof(*pos), member);        \
261              &pos->member != (head);                                    \
262              pos = list_next_entry(pos, member))
263
264 /**
265  * list_for_each_entry_reverse - iterate backwards over list of given type.
266  * @pos:        the type * to use as a loop cursor.
267  * @head:       the head for your list.
268  * @member:     the name of the list_struct within the struct.
269  */
270 #define list_for_each_entry_reverse(pos, head, member)                  \
271         for (pos = list_last_entry(head, typeof(*pos), member);         \
272              &pos->member != (head);                                    \
273              pos = list_prev_entry(pos, member))
274
275 /**
276  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
277  * @pos:        the type * to use as a loop cursor.
278  * @n:          another type * to use as temporary storage
279  * @head:       the head for your list.
280  * @member:     the name of the list_struct within the struct.
281  */
282 #define list_for_each_entry_safe(pos, n, head, member)                  \
283         for (pos = list_entry((head)->next, typeof(*pos), member),      \
284                 n = list_entry(pos->member.next, typeof(*pos), member); \
285              &pos->member != (head);                                    \
286              pos = n, n = list_entry(n->member.next, typeof(*n), member))
287
288 /*
289  * Double linked lists with a single pointer list head.
290  * Mostly useful for hash tables where the two pointer list head is
291  * too wasteful.
292  * You lose the ability to access the tail in O(1).
293  */
294
295 struct hlist_head {
296         struct hlist_node *first;
297 };
298
299 struct hlist_node {
300         struct hlist_node *next;
301         struct hlist_node **pprev;
302 };
303
304 static inline void
305 INIT_HLIST_HEAD(struct hlist_head *h)
306 {
307         h->first = NULL;
308 }
309
310 static inline bool
311 hlist_unhashed(const struct hlist_node *h)
312 {
313         return !h->pprev;
314 }
315
316 static inline bool
317 hlist_empty(const struct hlist_head *h)
318 {
319         return !h->first;
320 }
321
322 static inline void
323 hlist_del(struct hlist_node *n)
324 {
325         struct hlist_node *next = n->next;
326         struct hlist_node **pprev = n->pprev;
327         *pprev = next;
328         if (next)
329                 next->pprev = pprev;
330 }
331
332 static inline void
333 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
334 {
335         struct hlist_node *first = h->first;
336         n->next = first;
337         if (first)
338                 first->pprev = &n->next;
339         h->first = n;
340         n->pprev = &h->first;
341 }
342
343 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
344
345 #define hlist_entry_safe(ptr, type, member) \
346         ({ typeof(ptr) ____ptr = (ptr); \
347            ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
348         })
349
350 /**
351  * hlist_for_each_entry - iterate over list of given type
352  * @pos:        the type * to use as a loop cursor.
353  * @head:       the head for your list.
354  * @member:     the name of the hlist_node within the struct.
355  */
356 #define hlist_for_each_entry(pos, head, member)                         \
357         for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
358              pos;                                                       \
359              pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
360
361 /**
362  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
363  * @pos:        the type * to use as a loop cursor.
364  * @n:          another &struct hlist_node to use as temporary storage
365  * @head:       the head for your list.
366  * @member:     the name of the hlist_node within the struct.
367  */
368 #define hlist_for_each_entry_safe(pos, n, head, member)                 \
369         for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
370              pos && ({ n = pos->member.next; 1; });                     \
371              pos = hlist_entry_safe(n, typeof(*pos), member))
372
373 #endif /* _WIMLIB_LIST_H */