3 * This file is based on include/linux/list.h in the Linux kernel source code.
12 /* Simple doubly linked list implementation. */
15 struct list_head *next;
16 struct list_head *prev;
20 #define LIST_HEAD_INIT(name) { &(name), &(name) }
22 #undef LIST_HEAD /* BSD sys/queue.h defines this... */
23 #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
26 INIT_LIST_HEAD(struct list_head *list)
33 * Insert a new entry between two known consecutive entries.
35 * This is only for internal list manipulation where we know
36 * the prev/next entries already!
39 __list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
48 * list_add - add a new entry
49 * @new: new entry to be added
50 * @head: list head to add it after
52 * Insert a new entry after the specified head.
53 * This is good for implementing stacks.
56 list_add(struct list_head *new, struct list_head *head)
58 __list_add(new, head, head->next);
62 * list_add_tail - add a new entry
63 * @new: new entry to be added
64 * @head: list head to add it before
66 * Insert a new entry before the specified head.
67 * This is useful for implementing queues.
70 list_add_tail(struct list_head *new, struct list_head *head)
72 __list_add(new, head->prev, head);
76 * list_replace - replace old entry by new one
77 * @old : the element to be replaced
78 * @new : the new element to insert
80 * If @old was empty, it will be overwritten.
83 list_replace(struct list_head *old, struct list_head *new)
85 new->next = old->next;
86 new->next->prev = new;
87 new->prev = old->prev;
88 new->prev->next = new;
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.
98 list_del(struct list_head *entry)
100 struct list_head *prev = entry->prev;
101 struct list_head *next = entry->next;
108 * list_empty - tests whether a list is empty
109 * @head: the list to test.
112 list_empty(const struct list_head *head)
114 return head->next == head;
118 __list_splice(const struct list_head *list,
119 struct list_head *prev, struct list_head *next)
121 struct list_head *first = list->next;
122 struct list_head *last = list->prev;
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.
137 list_splice(const struct list_head *list, struct list_head *head)
139 if (!list_empty(list))
140 __list_splice(list, head, head->next);
143 /* Move the entire list @old to the list @new, overwriting it. */
145 list_transfer(struct list_head *old, struct list_head *new)
147 struct list_head *prev, *next;
149 if (list_empty(old)) {
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
167 list_move(struct list_head *list, struct list_head *head)
170 list_add(list, head);
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
179 list_move_tail(struct list_head *list, struct list_head *head)
182 list_add_tail(list, head);
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.
191 list_splice_tail(struct list_head *list, struct list_head *head)
193 if (!list_empty(list))
194 __list_splice(list, head->prev, head);
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.
203 #define list_entry(ptr, type, member) \
204 container_of(ptr, type, member)
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.
212 * Note, that list is expected to be not empty.
214 #define list_first_entry(ptr, type, member) \
215 list_entry((ptr)->next, type, member)
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.
223 * Note, that list is expected to be not empty.
225 #define list_last_entry(ptr, type, member) \
226 list_entry((ptr)->prev, type, member)
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.
233 #define list_next_entry(pos, member) \
234 list_entry((pos)->member.next, typeof(*(pos)), member)
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.
241 #define list_prev_entry(pos, member) \
242 list_entry((pos)->member.prev, typeof(*(pos)), member)
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.
250 #define list_for_each(pos, head) \
251 for (pos = (head)->next; pos != (head); pos = pos->next)
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.
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))
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.
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))
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.
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))
289 * Double linked lists with a single pointer list head.
290 * Mostly useful for hash tables where the two pointer list head is
292 * You lose the ability to access the tail in O(1).
296 struct hlist_node *first;
300 struct hlist_node *next;
301 struct hlist_node **pprev;
305 INIT_HLIST_HEAD(struct hlist_head *h)
311 hlist_unhashed(const struct hlist_node *h)
317 hlist_empty(const struct hlist_head *h)
323 hlist_del(struct hlist_node *n)
325 struct hlist_node *next = n->next;
326 struct hlist_node **pprev = n->pprev;
333 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
335 struct hlist_node *first = h->first;
338 first->pprev = &n->next;
340 n->pprev = &h->first;
343 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
345 #define hlist_entry_safe(ptr, type, member) \
346 ({ typeof(ptr) ____ptr = (ptr); \
347 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
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.
356 #define hlist_for_each_entry(pos, head, member) \
357 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
359 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
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.
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))
373 #endif /* _WIMLIB_LIST_H */