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 * Delete a list entry by making the prev/next entries
91 * point to each other.
93 * This is only for internal list manipulation where we know
94 * the prev/next entries already!
96 static inline void __list_del(struct list_head * prev, struct list_head * next)
103 * list_del - deletes entry from list.
104 * @entry: the element to delete from the list.
105 * Note: list_empty() on entry does not return true after this, the entry is
106 * in an undefined state.
108 static inline void list_del(struct list_head *entry)
110 __list_del(entry->prev, entry->next);
114 * list_del_init - deletes entry from list and reinitialize it.
115 * @entry: the element to delete from the list.
117 static inline void list_del_init(struct list_head *entry)
120 INIT_LIST_HEAD(entry);
124 * list_empty - tests whether a list is empty
125 * @head: the list to test.
127 static inline int list_empty(const struct list_head *head)
129 return head->next == head;
132 static inline void __list_splice(const struct list_head *list,
133 struct list_head *prev,
134 struct list_head *next)
136 struct list_head *first = list->next;
137 struct list_head *last = list->prev;
147 * list_splice - join two lists, this is designed for stacks
148 * @list: the new list to add.
149 * @head: the place to add it in the first list.
151 static inline void list_splice(const struct list_head *list,
152 struct list_head *head)
154 if (!list_empty(list))
155 __list_splice(list, head, head->next);
158 /* Move the entire list @old to the list @new, overwriting it. */
159 static inline void list_transfer(struct list_head *old,
160 struct list_head *new)
162 struct list_head *prev, *next;
164 if (list_empty(old)) {
177 * list_move - delete from one list and add as another's head
178 * @list: the entry to move
179 * @head: the head that will precede our entry
181 static inline void list_move(struct list_head *list, struct list_head *head)
184 list_add(list, head);
188 * list_move_tail - delete from one list and add as another's tail
189 * @list: the entry to move
190 * @head: the head that will follow our entry
192 static inline void list_move_tail(struct list_head *list,
193 struct list_head *head)
196 list_add_tail(list, head);
200 * list_splice_tail - join two lists, each list being a queue
201 * @list: the new list to add.
202 * @head: the place to add it in the first list.
204 static inline void list_splice_tail(struct list_head *list,
205 struct list_head *head)
207 if (!list_empty(list))
208 __list_splice(list, head->prev, head);
212 * list_entry - get the struct for this entry
213 * @ptr: the &struct list_head pointer.
214 * @type: the type of the struct this is embedded in.
215 * @member: the name of the list_struct within the struct.
217 #define list_entry(ptr, type, member) \
218 container_of(ptr, type, member)
221 * list_for_each - iterate over a list
222 * @pos: the &struct list_head to use as a loop cursor.
223 * @head: the head for your list.
225 #define list_for_each(pos, head) \
226 for (pos = (head)->next; pos != (head); pos = pos->next)
229 * list_for_each_safe - iterate over a list safe against removal of list entry
230 * @pos: the &struct list_head to use as a loop cursor.
231 * @n: another &struct list_head to use as temporary storage
232 * @head: the head for your list.
234 #define list_for_each_safe(pos, n, head) \
235 for (pos = (head)->next, n = pos->next; pos != (head); \
236 pos = n, n = pos->next)
239 * list_for_each_entry - iterate over list of given type
240 * @pos: the type * to use as a loop cursor.
241 * @head: the head for your list.
242 * @member: the name of the list_struct within the struct.
244 #define list_for_each_entry(pos, head, member) \
245 for (pos = list_entry((head)->next, typeof(*pos), member); \
246 &pos->member != (head); \
247 pos = list_entry(pos->member.next, typeof(*pos), member))
250 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
251 * @pos: the type * to use as a loop cursor.
252 * @n: another type * to use as temporary storage
253 * @head: the head for your list.
254 * @member: the name of the list_struct within the struct.
256 #define list_for_each_entry_safe(pos, n, head, member) \
257 for (pos = list_entry((head)->next, typeof(*pos), member), \
258 n = list_entry(pos->member.next, typeof(*pos), member); \
259 &pos->member != (head); \
260 pos = n, n = list_entry(n->member.next, typeof(*n), member))
263 * Double linked lists with a single pointer list head.
264 * Mostly useful for hash tables where the two pointer list head is
266 * You lose the ability to access the tail in O(1).
269 #define HLIST_HEAD_INIT { .first = NULL }
270 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
271 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
272 static inline void INIT_HLIST_NODE(struct hlist_node *h)
278 static inline void hlist_del(struct hlist_node *n)
280 struct hlist_node *next = n->next;
281 struct hlist_node **pprev = n->pprev;
287 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
289 struct hlist_node *first = h->first;
292 first->pprev = &n->next;
294 n->pprev = &h->first;
297 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
299 #define hlist_for_each(pos, head) \
300 for (pos = (head)->first; pos ; pos = pos->next)
302 #define hlist_for_each_safe(pos, n, head) \
303 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
307 * hlist_for_each_entry - iterate over list of given type
308 * @tpos: the type * to use as a loop cursor.
309 * @pos: the &struct hlist_node to use as a loop cursor.
310 * @head: the head for your list.
311 * @member: the name of the hlist_node within the struct.
313 #define hlist_for_each_entry(tpos, pos, head, member) \
314 for (pos = (head)->first; \
316 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
320 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
321 * @tpos: the type * to use as a loop cursor.
322 * @pos: the &struct hlist_node to use as a loop cursor.
323 * @n: another &struct hlist_node to use as temporary storage
324 * @head: the head for your list.
325 * @member: the name of the hlist_node within the struct.
327 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
328 for (pos = (head)->first; \
329 pos && ({ n = pos->next; 1; }) && \
330 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \