3 * This file is based on include/linux/list.h in the Linux kernel source code.
12 struct list_head *next, *prev;
15 struct hlist_node *first;
19 struct hlist_node *next, **pprev;
23 * Simple doubly linked list implementation.
25 * Some of the internal functions ("__xxx") are useful when
26 * manipulating whole lists rather than single entries, as
27 * sometimes we already know the next/prev entries and we can
28 * generate better code by using them directly rather than
29 * using the generic single-entry routines.
32 #define LIST_HEAD_INIT(name) { &(name), &(name) }
34 #ifdef LIST_HEAD /* BSD sys/queue.h defines this... */
38 #define LIST_HEAD(name) \
39 struct list_head name = LIST_HEAD_INIT(name)
41 static inline void INIT_LIST_HEAD(struct list_head *list)
48 * Insert a new entry between two known consecutive entries.
50 * This is only for internal list manipulation where we know
51 * the prev/next entries already!
53 static inline void __list_add(struct list_head *new,
54 struct list_head *prev,
55 struct list_head *next)
64 * list_add - add a new entry
65 * @new: new entry to be added
66 * @head: list head to add it after
68 * Insert a new entry after the specified head.
69 * This is good for implementing stacks.
71 static inline void list_add(struct list_head *new, struct list_head *head)
73 __list_add(new, head, head->next);
77 * list_add_tail - add a new entry
78 * @new: new entry to be added
79 * @head: list head to add it before
81 * Insert a new entry before the specified head.
82 * This is useful for implementing queues.
84 static inline void list_add_tail(struct list_head *new, struct list_head *head)
86 __list_add(new, head->prev, head);
90 * list_replace - replace old entry by new one
91 * @old : the element to be replaced
92 * @new : the new element to insert
94 * If @old was empty, it will be overwritten.
96 static inline void list_replace(struct list_head *old,
97 struct list_head *new)
99 new->next = old->next;
100 new->next->prev = new;
101 new->prev = old->prev;
102 new->prev->next = new;
105 static inline void list_replace_init(struct list_head *old,
106 struct list_head *new)
108 list_replace(old, new);
113 * Delete a list entry by making the prev/next entries
114 * point to each other.
116 * This is only for internal list manipulation where we know
117 * the prev/next entries already!
119 static inline void __list_del(struct list_head * prev, struct list_head * next)
126 * list_del - deletes entry from list.
127 * @entry: the element to delete from the list.
128 * Note: list_empty() on entry does not return true after this, the entry is
129 * in an undefined state.
131 static inline void list_del(struct list_head *entry)
133 __list_del(entry->prev, entry->next);
137 * list_del_init - deletes entry from list and reinitialize it.
138 * @entry: the element to delete from the list.
140 static inline void list_del_init(struct list_head *entry)
143 INIT_LIST_HEAD(entry);
147 * list_empty - tests whether a list is empty
148 * @head: the list to test.
150 static inline int list_empty(const struct list_head *head)
152 return head->next == head;
156 * list_is_singular - tests whether a list has just one entry.
157 * @head: the list to test.
159 static inline int list_is_singular(const struct list_head *head)
161 return !list_empty(head) && (head->next == head->prev);
164 static inline void __list_splice(const struct list_head *list,
165 struct list_head *prev,
166 struct list_head *next)
168 struct list_head *first = list->next;
169 struct list_head *last = list->prev;
179 * list_splice - join two lists, this is designed for stacks
180 * @list: the new list to add.
181 * @head: the place to add it in the first list.
183 static inline void list_splice(const struct list_head *list,
184 struct list_head *head)
186 if (!list_empty(list))
187 __list_splice(list, head, head->next);
190 /* Move the entire list @old to the list @new, overwriting it. */
191 static inline void list_transfer(struct list_head *old,
192 struct list_head *new)
194 struct list_head *prev, *next;
196 if (list_empty(old)) {
209 * list_move - delete from one list and add as another's head
210 * @list: the entry to move
211 * @head: the head that will precede our entry
213 static inline void list_move(struct list_head *list, struct list_head *head)
216 list_add(list, head);
220 * list_move_tail - delete from one list and add as another's tail
221 * @list: the entry to move
222 * @head: the head that will follow our entry
224 static inline void list_move_tail(struct list_head *list,
225 struct list_head *head)
228 list_add_tail(list, head);
232 * list_splice_tail - join two lists, each list being a queue
233 * @list: the new list to add.
234 * @head: the place to add it in the first list.
236 static inline void list_splice_tail(struct list_head *list,
237 struct list_head *head)
239 if (!list_empty(list))
240 __list_splice(list, head->prev, head);
244 * list_entry - get the struct for this entry
245 * @ptr: the &struct list_head pointer.
246 * @type: the type of the struct this is embedded in.
247 * @member: the name of the list_struct within the struct.
249 #define list_entry(ptr, type, member) \
250 container_of(ptr, type, member)
253 * list_for_each - iterate over a list
254 * @pos: the &struct list_head to use as a loop cursor.
255 * @head: the head for your list.
257 #define list_for_each(pos, head) \
258 for (pos = (head)->next; pos != (head); pos = pos->next)
261 * list_for_each_safe - iterate over a list safe against removal of list entry
262 * @pos: the &struct list_head to use as a loop cursor.
263 * @n: another &struct list_head to use as temporary storage
264 * @head: the head for your list.
266 #define list_for_each_safe(pos, n, head) \
267 for (pos = (head)->next, n = pos->next; pos != (head); \
268 pos = n, n = pos->next)
271 * list_for_each_entry - iterate over list of given type
272 * @pos: the type * to use as a loop cursor.
273 * @head: the head for your list.
274 * @member: the name of the list_struct within the struct.
276 #define list_for_each_entry(pos, head, member) \
277 for (pos = list_entry((head)->next, typeof(*pos), member); \
278 &pos->member != (head); \
279 pos = list_entry(pos->member.next, typeof(*pos), member))
282 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
283 * @pos: the type * to use as a loop cursor.
284 * @n: another type * to use as temporary storage
285 * @head: the head for your list.
286 * @member: the name of the list_struct within the struct.
288 #define list_for_each_entry_safe(pos, n, head, member) \
289 for (pos = list_entry((head)->next, typeof(*pos), member), \
290 n = list_entry(pos->member.next, typeof(*pos), member); \
291 &pos->member != (head); \
292 pos = n, n = list_entry(n->member.next, typeof(*n), member))
295 * Double linked lists with a single pointer list head.
296 * Mostly useful for hash tables where the two pointer list head is
298 * You lose the ability to access the tail in O(1).
301 #define HLIST_HEAD_INIT { .first = NULL }
302 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
303 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
304 static inline void INIT_HLIST_NODE(struct hlist_node *h)
310 static inline int hlist_unhashed(const struct hlist_node *h)
315 static inline int hlist_empty(const struct hlist_head *h)
320 static inline void __hlist_del(struct hlist_node *n)
322 struct hlist_node *next = n->next;
323 struct hlist_node **pprev = n->pprev;
329 static inline void hlist_del(struct hlist_node *n)
333 n->next = LIST_POISON1;
334 n->pprev = LIST_POISON2;
338 static inline void hlist_del_init(struct hlist_node *n)
340 if (!hlist_unhashed(n)) {
346 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
348 struct hlist_node *first = h->first;
351 first->pprev = &n->next;
353 n->pprev = &h->first;
356 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
358 #define hlist_for_each(pos, head) \
359 for (pos = (head)->first; pos ; pos = pos->next)
361 #define hlist_for_each_safe(pos, n, head) \
362 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
366 * hlist_for_each_entry - iterate over list of given type
367 * @tpos: the type * to use as a loop cursor.
368 * @pos: the &struct hlist_node to use as a loop cursor.
369 * @head: the head for your list.
370 * @member: the name of the hlist_node within the struct.
372 #define hlist_for_each_entry(tpos, pos, head, member) \
373 for (pos = (head)->first; \
375 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
379 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
380 * @tpos: the type * to use as a loop cursor.
381 * @pos: the &struct hlist_node to use as a loop cursor.
382 * @n: another &struct hlist_node to use as temporary storage
383 * @head: the head for your list.
384 * @member: the name of the hlist_node within the struct.
386 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
387 for (pos = (head)->first; \
388 pos && ({ n = pos->next; 1; }) && \
389 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \