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);
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
149 list_move(struct list_head *list, struct list_head *head)
152 list_add(list, head);
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
161 list_move_tail(struct list_head *list, struct list_head *head)
164 list_add_tail(list, head);
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.
173 list_splice_tail(struct list_head *list, struct list_head *head)
175 if (!list_empty(list))
176 __list_splice(list, head->prev, head);
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.
185 #define list_entry(ptr, type, member) \
186 container_of(ptr, type, member)
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.
194 * Note, that list is expected to be not empty.
196 #define list_first_entry(ptr, type, member) \
197 list_entry((ptr)->next, type, member)
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.
205 * Note, that list is expected to be not empty.
207 #define list_last_entry(ptr, type, member) \
208 list_entry((ptr)->prev, type, member)
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.
215 #define list_next_entry(pos, member) \
216 list_entry((pos)->member.next, typeof(*(pos)), member)
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.
223 #define list_prev_entry(pos, member) \
224 list_entry((pos)->member.prev, typeof(*(pos)), member)
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.
232 #define list_for_each(pos, head) \
233 for (pos = (head)->next; pos != (head); pos = pos->next)
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.
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))
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.
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))
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.
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))
271 * Double linked lists with a single pointer list head.
272 * Mostly useful for hash tables where the two pointer list head is
274 * You lose the ability to access the tail in O(1).
278 struct hlist_node *first;
282 struct hlist_node *next;
283 struct hlist_node **pprev;
287 INIT_HLIST_HEAD(struct hlist_head *h)
293 hlist_unhashed(const struct hlist_node *h)
299 hlist_empty(const struct hlist_head *h)
305 hlist_del(struct hlist_node *n)
307 struct hlist_node *next = n->next;
308 struct hlist_node **pprev = n->pprev;
315 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
317 struct hlist_node *first = h->first;
320 first->pprev = &n->next;
322 n->pprev = &h->first;
325 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
327 #define hlist_entry_safe(ptr, type, member) \
328 ({ typeof(ptr) ____ptr = (ptr); \
329 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
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.
338 #define hlist_for_each_entry(pos, head, member) \
339 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
341 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
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.
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))
355 #endif /* _WIMLIB_LIST_H */