3 * This file is based on include/linux/list.h in the Linux kernel source code.
13 struct list_head *next, *prev;
16 struct hlist_node *first;
20 struct hlist_node *next, **pprev;
24 * Simple doubly linked list implementation.
26 * Some of the internal functions ("__xxx") are useful when
27 * manipulating whole lists rather than single entries, as
28 * sometimes we already know the next/prev entries and we can
29 * generate better code by using them directly rather than
30 * using the generic single-entry routines.
33 #define LIST_HEAD_INIT(name) { &(name), &(name) }
35 #ifdef LIST_HEAD /* BSD sys/queue.h defines this... */
39 #define LIST_HEAD(name) \
40 struct list_head name = LIST_HEAD_INIT(name)
43 INIT_LIST_HEAD(struct list_head *list)
50 * Insert a new entry between two known consecutive entries.
52 * This is only for internal list manipulation where we know
53 * the prev/next entries already!
56 __list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
65 * list_add - add a new entry
66 * @new: new entry to be added
67 * @head: list head to add it after
69 * Insert a new entry after the specified head.
70 * This is good for implementing stacks.
73 list_add(struct list_head *new, struct list_head *head)
75 __list_add(new, head, head->next);
79 * list_add_tail - add a new entry
80 * @new: new entry to be added
81 * @head: list head to add it before
83 * Insert a new entry before the specified head.
84 * This is useful for implementing queues.
87 list_add_tail(struct list_head *new, struct list_head *head)
89 __list_add(new, head->prev, head);
93 * list_replace - replace old entry by new one
94 * @old : the element to be replaced
95 * @new : the new element to insert
97 * If @old was empty, it will be overwritten.
100 list_replace(struct list_head *old, struct list_head *new)
102 new->next = old->next;
103 new->next->prev = new;
104 new->prev = old->prev;
105 new->prev->next = new;
109 * list_del - deletes entry from list.
110 * @entry: the element to delete from the list.
111 * Note: list_empty() on entry does not return true after this, the entry is
112 * in an undefined state.
115 list_del(struct list_head *entry)
117 struct list_head *prev = entry->prev;
118 struct list_head *next = entry->next;
125 * list_empty - tests whether a list is empty
126 * @head: the list to test.
129 list_empty(const struct list_head *head)
131 return head->next == head;
135 __list_splice(const struct list_head *list,
136 struct list_head *prev,
137 struct list_head *next)
139 struct list_head *first = list->next;
140 struct list_head *last = list->prev;
150 * list_splice - join two lists, this is designed for stacks
151 * @list: the new list to add.
152 * @head: the place to add it in the first list.
155 list_splice(const struct list_head *list, struct list_head *head)
157 if (!list_empty(list))
158 __list_splice(list, head, head->next);
161 /* Move the entire list @old to the list @new, overwriting it. */
163 list_transfer(struct list_head *old, struct list_head *new)
165 struct list_head *prev, *next;
167 if (list_empty(old)) {
180 * list_move - delete from one list and add as another's head
181 * @list: the entry to move
182 * @head: the head that will precede our entry
185 list_move(struct list_head *list, struct list_head *head)
188 list_add(list, head);
192 * list_move_tail - delete from one list and add as another's tail
193 * @list: the entry to move
194 * @head: the head that will follow our entry
197 list_move_tail(struct list_head *list, struct list_head *head)
200 list_add_tail(list, head);
204 * list_splice_tail - join two lists, each list being a queue
205 * @list: the new list to add.
206 * @head: the place to add it in the first list.
209 list_splice_tail(struct list_head *list, struct list_head *head)
211 if (!list_empty(list))
212 __list_splice(list, head->prev, head);
216 * list_entry - get the struct for this entry
217 * @ptr: the &struct list_head pointer.
218 * @type: the type of the struct this is embedded in.
219 * @member: the name of the list_struct within the struct.
221 #define list_entry(ptr, type, member) \
222 container_of(ptr, type, member)
225 * list_first_entry - get the first element from a list
226 * @ptr: the list head to take the element from.
227 * @type: the type of the struct this is embedded in.
228 * @member: the name of the list_struct within the struct.
230 * Note, that list is expected to be not empty.
232 #define list_first_entry(ptr, type, member) \
233 list_entry((ptr)->next, type, member)
236 * list_last_entry - get the last element from a list
237 * @ptr: the list head to take the element from.
238 * @type: the type of the struct this is embedded in.
239 * @member: the name of the list_struct within the struct.
241 * Note, that list is expected to be not empty.
243 #define list_last_entry(ptr, type, member) \
244 list_entry((ptr)->prev, type, member)
247 * list_next_entry - get the next element in list
248 * @pos: the type * to cursor
249 * @member: the name of the list_struct within the struct.
251 #define list_next_entry(pos, member) \
252 list_entry((pos)->member.next, typeof(*(pos)), member)
255 * list_prev_entry - get the prev element in list
256 * @pos: the type * to cursor
257 * @member: the name of the list_struct within the struct.
259 #define list_prev_entry(pos, member) \
260 list_entry((pos)->member.prev, typeof(*(pos)), member)
264 * list_for_each - iterate over a list
265 * @pos: the &struct list_head to use as a loop cursor.
266 * @head: the head for your list.
268 #define list_for_each(pos, head) \
269 for (pos = (head)->next; pos != (head); pos = pos->next)
272 * list_for_each_entry - iterate over list of given type
273 * @pos: the type * to use as a loop cursor.
274 * @head: the head for your list.
275 * @member: the name of the list_struct within the struct.
277 #define list_for_each_entry(pos, head, member) \
278 for (pos = list_first_entry(head, typeof(*pos), member); \
279 &pos->member != (head); \
280 pos = list_next_entry(pos, member))
283 * list_for_each_entry_reverse - iterate backwards over list of given type.
284 * @pos: the type * to use as a loop cursor.
285 * @head: the head for your list.
286 * @member: the name of the list_struct within the struct.
288 #define list_for_each_entry_reverse(pos, head, member) \
289 for (pos = list_last_entry(head, typeof(*pos), member); \
290 &pos->member != (head); \
291 pos = list_prev_entry(pos, member))
294 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
295 * @pos: the type * to use as a loop cursor.
296 * @n: another type * to use as temporary storage
297 * @head: the head for your list.
298 * @member: the name of the list_struct within the struct.
300 #define list_for_each_entry_safe(pos, n, head, member) \
301 for (pos = list_entry((head)->next, typeof(*pos), member), \
302 n = list_entry(pos->member.next, typeof(*pos), member); \
303 &pos->member != (head); \
304 pos = n, n = list_entry(n->member.next, typeof(*n), member))
307 * Double linked lists with a single pointer list head.
308 * Mostly useful for hash tables where the two pointer list head is
310 * You lose the ability to access the tail in O(1).
313 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
316 hlist_unhashed(const struct hlist_node *h)
322 hlist_empty(const struct hlist_head *h)
328 hlist_del(struct hlist_node *n)
330 struct hlist_node *next = n->next;
331 struct hlist_node **pprev = n->pprev;
338 hlist_add_head(struct hlist_node *n, struct hlist_head *h)
340 struct hlist_node *first = h->first;
343 first->pprev = &n->next;
345 n->pprev = &h->first;
348 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
351 * hlist_for_each_entry - iterate over list of given type
352 * @tpos: the type * to use as a loop cursor.
353 * @pos: the &struct hlist_node to use as a loop cursor.
354 * @head: the head for your list.
355 * @member: the name of the hlist_node within the struct.
357 #define hlist_for_each_entry(tpos, pos, head, member) \
358 for (pos = (head)->first; \
360 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
364 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
365 * @tpos: the type * to use as a loop cursor.
366 * @pos: the &struct hlist_node to use as a loop cursor.
367 * @n: another &struct hlist_node to use as temporary storage
368 * @head: the head for your list.
369 * @member: the name of the hlist_node within the struct.
371 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
372 for (pos = (head)->first; \
373 pos && ({ n = pos->next; 1; }) && \
374 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
377 #endif /* _WIMLIB_LIST_H */