4 Copyright (C) Cast of dozens, comes from the Linux kernel
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of version 2 of the GNU General Public License as
8 published by the Free Software Foundation.
14 * These are non-NULL pointers that will result in page faults
15 * under normal circumstances, used to verify that nobody uses
16 * non-initialized list entries.
18 #define LIST_POISON1 ((void *)0x00100100)
19 #define LIST_POISON2 ((void *)0x00200200)
22 * container_of - cast a member of a structure out to the containing structure
23 * @ptr: the pointer to the member.
24 * @type: the type of the container struct this is embedded in.
25 * @member: the name of the member within the struct.
28 #define container_of(ptr, type, member) ({ \
29 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
30 (type *)( (char *)__mptr - offsetof(type,member) );})
33 * Simple doubly linked list implementation.
35 * Some of the internal functions ("__xxx") are useful when
36 * manipulating whole lists rather than single entries, as
37 * sometimes we already know the next/prev entries and we can
38 * generate better code by using them directly rather than
39 * using the generic single-entry routines.
43 struct list_head
*next
, *prev
;
46 #define LIST_HEAD_INIT(name) { &(name), &(name) }
48 #define LIST_HEAD(name) \
49 struct list_head name = LIST_HEAD_INIT(name)
51 static inline void INIT_LIST_HEAD(struct list_head
*list
)
58 * Insert a new entry between two known consecutive entries.
60 * This is only for internal list manipulation where we know
61 * the prev/next entries already!
63 static inline void __list_add(struct list_head
*new,
64 struct list_head
*prev
,
65 struct list_head
*next
)
74 * list_add - add a new entry
75 * @new: new entry to be added
76 * @head: list head to add it after
78 * Insert a new entry after the specified head.
79 * This is good for implementing stacks.
81 static inline void list_add(struct list_head
*new, struct list_head
*head
)
83 __list_add(new, head
, head
->next
);
87 * list_add_tail - add a new entry
88 * @new: new entry to be added
89 * @head: list head to add it before
91 * Insert a new entry before the specified head.
92 * This is useful for implementing queues.
94 static inline void list_add_tail(struct list_head
*new, struct list_head
*head
)
96 __list_add(new, head
->prev
, head
);
100 * Delete a list entry by making the prev/next entries
101 * point to each other.
103 * This is only for internal list manipulation where we know
104 * the prev/next entries already!
106 static inline void __list_del(struct list_head
* prev
, struct list_head
* next
)
113 * list_del - deletes entry from list.
114 * @entry: the element to delete from the list.
115 * Note: list_empty on entry does not return true after this, the entry is
116 * in an undefined state.
118 static inline void list_del(struct list_head
*entry
)
120 __list_del(entry
->prev
, entry
->next
);
121 entry
->next
= LIST_POISON1
;
122 entry
->prev
= LIST_POISON2
;
126 * list_del_range - deletes range of entries from list.
127 * @beging: first element in the range to delete from the list.
128 * @beging: first element in the range to delete from the list.
129 * Note: list_empty on the range of entries does not return true after this,
130 * the entries is in an undefined state.
132 static inline void list_del_range(struct list_head
*begin
,
133 struct list_head
*end
)
135 begin
->prev
->next
= end
->next
;
136 end
->next
->prev
= begin
->prev
;
140 * list_replace - replace old entry by new one
141 * @old : the element to be replaced
142 * @new : the new element to insert
143 * Note: if 'old' was empty, it will be overwritten.
145 static inline void list_replace(struct list_head
*old
,
146 struct list_head
*new)
148 new->next
= old
->next
;
149 new->next
->prev
= new;
150 new->prev
= old
->prev
;
151 new->prev
->next
= new;
154 static inline void list_replace_init(struct list_head
*old
,
155 struct list_head
*new)
157 list_replace(old
, new);
162 * list_del_init - deletes entry from list and reinitialize it.
163 * @entry: the element to delete from the list.
165 static inline void list_del_init(struct list_head
*entry
)
167 __list_del(entry
->prev
, entry
->next
);
168 INIT_LIST_HEAD(entry
);
172 * list_move - delete from one list and add as another's head
173 * @list: the entry to move
174 * @head: the head that will precede our entry
176 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
178 __list_del(list
->prev
, list
->next
);
179 list_add(list
, head
);
183 * list_move_tail - delete from one list and add as another's tail
184 * @list: the entry to move
185 * @head: the head that will follow our entry
187 static inline void list_move_tail(struct list_head
*list
,
188 struct list_head
*head
)
190 __list_del(list
->prev
, list
->next
);
191 list_add_tail(list
, head
);
195 * list_is_last - tests whether @list is the last entry in list @head
196 * @list: the entry to test
197 * @head: the head of the list
199 static inline int list_is_last(const struct list_head
*list
,
200 const struct list_head
*head
)
202 return list
->next
== head
;
206 * list_empty - tests whether a list is empty
207 * @head: the list to test.
209 static inline int list_empty(const struct list_head
*head
)
211 return head
->next
== head
;
215 * list_empty_careful - tests whether a list is empty and not being modified
216 * @head: the list to test
219 * tests whether a list is empty _and_ checks that no other CPU might be
220 * in the process of modifying either member (next or prev)
222 * NOTE: using list_empty_careful() without synchronization
223 * can only be safe if the only activity that can happen
224 * to the list entry is list_del_init(). Eg. it cannot be used
225 * if another CPU could re-list_add() it.
227 static inline int list_empty_careful(const struct list_head
*head
)
229 struct list_head
*next
= head
->next
;
230 return (next
== head
) && (next
== head
->prev
);
233 static inline void __list_splice(struct list_head
*list
,
234 struct list_head
*head
)
236 struct list_head
*first
= list
->next
;
237 struct list_head
*last
= list
->prev
;
238 struct list_head
*at
= head
->next
;
248 * list_splice - join two lists
249 * @list: the new list to add.
250 * @head: the place to add it in the first list.
252 static inline void list_splice(struct list_head
*list
, struct list_head
*head
)
254 if (!list_empty(list
))
255 __list_splice(list
, head
);
259 * list_splice_init - join two lists and reinitialise the emptied list.
260 * @list: the new list to add.
261 * @head: the place to add it in the first list.
263 * The list at @list is reinitialised
265 static inline void list_splice_init(struct list_head
*list
,
266 struct list_head
*head
)
268 if (!list_empty(list
)) {
269 __list_splice(list
, head
);
270 INIT_LIST_HEAD(list
);
275 * list_entry - get the struct for this entry
276 * @ptr: the &struct list_head pointer.
277 * @type: the type of the struct this is embedded in.
278 * @member: the name of the list_struct within the struct.
280 #define list_entry(ptr, type, member) \
281 container_of(ptr, type, member)
284 * list_first_entry - get the first element from a list
285 * @ptr: the list head to take the element from.
286 * @type: the type of the struct this is embedded in.
287 * @member: the name of the list_struct within the struct.
289 * Note, that list is expected to be not empty.
291 #define list_first_entry(ptr, type, member) \
292 list_entry((ptr)->next, type, member)
295 * list_for_each - iterate over a list
296 * @pos: the &struct list_head to use as a loop cursor.
297 * @head: the head for your list.
299 #define list_for_each(pos, head) \
300 for (pos = (head)->next; pos != (head); \
304 * __list_for_each - iterate over a list
305 * @pos: the &struct list_head to use as a loop cursor.
306 * @head: the head for your list.
308 * This variant differs from list_for_each() in that it's the
309 * simplest possible list iteration code, no prefetching is done.
310 * Use this for code that knows the list to be very short (empty
311 * or 1 entry) most of the time.
313 #define __list_for_each(pos, head) \
314 for (pos = (head)->next; pos != (head); pos = pos->next)
317 * list_for_each_prev - iterate over a list backwards
318 * @pos: the &struct list_head to use as a loop cursor.
319 * @head: the head for your list.
321 #define list_for_each_prev(pos, head) \
322 for (pos = (head)->prev; pos != (head); \
326 * list_for_each_safe - iterate over a list safe against removal of list entry
327 * @pos: the &struct list_head to use as a loop cursor.
328 * @n: another &struct list_head to use as temporary storage
329 * @head: the head for your list.
331 #define list_for_each_safe(pos, n, head) \
332 for (pos = (head)->next, n = pos->next; pos != (head); \
333 pos = n, n = pos->next)
336 * list_for_each_entry - iterate over list of given type
337 * @pos: the type * to use as a loop cursor.
338 * @head: the head for your list.
339 * @member: the name of the list_struct within the struct.
341 #define list_for_each_entry(pos, head, member) \
342 for (pos = list_entry((head)->next, typeof(*pos), member); \
343 &pos->member != (head); \
344 pos = list_entry(pos->member.next, typeof(*pos), member))
347 * list_for_each_entry_reverse - iterate backwards over list of given type.
348 * @pos: the type * to use as a loop cursor.
349 * @head: the head for your list.
350 * @member: the name of the list_struct within the struct.
352 #define list_for_each_entry_reverse(pos, head, member) \
353 for (pos = list_entry((head)->prev, typeof(*pos), member); \
354 &pos->member != (head); \
355 pos = list_entry(pos->member.prev, typeof(*pos), member))
358 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue
359 * @pos: the type * to use as a start point
360 * @head: the head of the list
361 * @member: the name of the list_struct within the struct.
363 * Prepares a pos entry for use as a start point in list_for_each_entry_continue.
365 #define list_prepare_entry(pos, head, member) \
366 ((pos) ? : list_entry(head, typeof(*pos), member))
369 * list_for_each_entry_continue - continue iteration over list of given type
370 * @pos: the type * to use as a loop cursor.
371 * @head: the head for your list.
372 * @member: the name of the list_struct within the struct.
374 * Continue to iterate over list of given type, continuing after
375 * the current position.
377 #define list_for_each_entry_continue(pos, head, member) \
378 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
379 &pos->member != (head); \
380 pos = list_entry(pos->member.next, typeof(*pos), member))
383 * list_for_each_entry_from - iterate over list of given type from the current point
384 * @pos: the type * to use as a loop cursor.
385 * @head: the head for your list.
386 * @member: the name of the list_struct within the struct.
388 * Iterate over list of given type, continuing from current position.
390 #define list_for_each_entry_from(pos, head, member) \
391 for (; &pos->member != (head); \
392 pos = list_entry(pos->member.next, typeof(*pos), member))
395 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
396 * @pos: the type * to use as a loop cursor.
397 * @n: another type * to use as temporary storage
398 * @head: the head for your list.
399 * @member: the name of the list_struct within the struct.
401 #define list_for_each_entry_safe(pos, n, head, member) \
402 for (pos = list_entry((head)->next, typeof(*pos), member), \
403 n = list_entry(pos->member.next, typeof(*pos), member); \
404 &pos->member != (head); \
405 pos = n, n = list_entry(n->member.next, typeof(*n), member))
408 * list_for_each_entry_safe_continue
409 * @pos: the type * to use as a loop cursor.
410 * @n: another type * to use as temporary storage
411 * @head: the head for your list.
412 * @member: the name of the list_struct within the struct.
414 * Iterate over list of given type, continuing after current point,
415 * safe against removal of list entry.
417 #define list_for_each_entry_safe_continue(pos, n, head, member) \
418 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
419 n = list_entry(pos->member.next, typeof(*pos), member); \
420 &pos->member != (head); \
421 pos = n, n = list_entry(n->member.next, typeof(*n), member))
424 * list_for_each_entry_safe_from
425 * @pos: the type * to use as a loop cursor.
426 * @n: another type * to use as temporary storage
427 * @head: the head for your list.
428 * @member: the name of the list_struct within the struct.
430 * Iterate over list of given type from current point, safe against
431 * removal of list entry.
433 #define list_for_each_entry_safe_from(pos, n, head, member) \
434 for (n = list_entry(pos->member.next, typeof(*pos), member); \
435 &pos->member != (head); \
436 pos = n, n = list_entry(n->member.next, typeof(*n), member))
439 * list_for_each_entry_safe_reverse
440 * @pos: the type * to use as a loop cursor.
441 * @n: another type * to use as temporary storage
442 * @head: the head for your list.
443 * @member: the name of the list_struct within the struct.
445 * Iterate backwards over list of given type, safe against removal
448 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
449 for (pos = list_entry((head)->prev, typeof(*pos), member), \
450 n = list_entry(pos->member.prev, typeof(*pos), member); \
451 &pos->member != (head); \
452 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
455 * Double linked lists with a single pointer list head.
456 * Mostly useful for hash tables where the two pointer list head is
458 * You lose the ability to access the tail in O(1).
462 struct hlist_node
*first
;
466 struct hlist_node
*next
, **pprev
;
469 #define HLIST_HEAD_INIT { .first = NULL }
470 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
471 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
472 static inline void INIT_HLIST_NODE(struct hlist_node
*h
)
478 static inline int hlist_unhashed(const struct hlist_node
*h
)
483 static inline int hlist_empty(const struct hlist_head
*h
)
488 static inline void __hlist_del(struct hlist_node
*n
)
490 struct hlist_node
*next
= n
->next
;
491 struct hlist_node
**pprev
= n
->pprev
;
497 static inline void hlist_del(struct hlist_node
*n
)
500 n
->next
= LIST_POISON1
;
501 n
->pprev
= LIST_POISON2
;
504 static inline void hlist_del_init(struct hlist_node
*n
)
506 if (!hlist_unhashed(n
)) {
512 static inline void hlist_add_head(struct hlist_node
*n
, struct hlist_head
*h
)
514 struct hlist_node
*first
= h
->first
;
517 first
->pprev
= &n
->next
;
519 n
->pprev
= &h
->first
;
522 /* next must be != NULL */
523 static inline void hlist_add_before(struct hlist_node
*n
,
524 struct hlist_node
*next
)
526 n
->pprev
= next
->pprev
;
528 next
->pprev
= &n
->next
;
532 static inline void hlist_add_after(struct hlist_node
*n
,
533 struct hlist_node
*next
)
535 next
->next
= n
->next
;
537 next
->pprev
= &n
->next
;
540 next
->next
->pprev
= &next
->next
;
543 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
545 #define hlist_for_each(pos, head) \
546 for (pos = (head)->first; pos; \
549 #define hlist_for_each_safe(pos, n, head) \
550 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
554 * hlist_for_each_entry - iterate over list of given type
555 * @tpos: the type * to use as a loop cursor.
556 * @pos: the &struct hlist_node to use as a loop cursor.
557 * @head: the head for your list.
558 * @member: the name of the hlist_node within the struct.
560 #define hlist_for_each_entry(tpos, pos, head, member) \
561 for (pos = (head)->first; \
563 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
567 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
568 * @tpos: the type * to use as a loop cursor.
569 * @pos: the &struct hlist_node to use as a loop cursor.
570 * @member: the name of the hlist_node within the struct.
572 #define hlist_for_each_entry_continue(tpos, pos, member) \
573 for (pos = (pos)->next; \
575 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
579 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
580 * @tpos: the type * to use as a loop cursor.
581 * @pos: the &struct hlist_node to use as a loop cursor.
582 * @member: the name of the hlist_node within the struct.
584 #define hlist_for_each_entry_from(tpos, pos, member) \
586 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
590 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
591 * @tpos: the type * to use as a loop cursor.
592 * @pos: the &struct hlist_node to use as a loop cursor.
593 * @n: another &struct hlist_node to use as temporary storage
594 * @head: the head for your list.
595 * @member: the name of the hlist_node within the struct.
597 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
598 for (pos = (head)->first; \
599 pos && ({ n = pos->next; 1; }) && \
600 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \