Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
list.h
Go to the documentation of this file.
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
3 
4 #include <linux/types.h>
5 #include <linux/stddef.h>
6 #include <linux/poison.h>
7 #include <linux/const.h>
8 
9 /*
10  * Simple doubly linked list implementation.
11  *
12  * Some of the internal functions ("__xxx") are useful when
13  * manipulating whole lists rather than single entries, as
14  * sometimes we already know the next/prev entries and we can
15  * generate better code by using them directly rather than
16  * using the generic single-entry routines.
17  */
18 
19 #define LIST_HEAD_INIT(name) { &(name), &(name) }
20 
21 #define LIST_HEAD(name) \
22  struct list_head name = LIST_HEAD_INIT(name)
23 
24 static inline void INIT_LIST_HEAD(struct list_head *list)
25 {
26  list->next = list;
27  list->prev = list;
28 }
29 
30 /*
31  * Insert a new entry between two known consecutive entries.
32  *
33  * This is only for internal list manipulation where we know
34  * the prev/next entries already!
35  */
36 #ifndef CONFIG_DEBUG_LIST
37 static inline void __list_add(struct list_head *new,
38  struct list_head *prev,
39  struct list_head *next)
40 {
41  next->prev = new;
42  new->next = next;
43  new->prev = prev;
44  prev->next = new;
45 }
46 #else
47 extern void __list_add(struct list_head *new,
48  struct list_head *prev,
49  struct list_head *next);
50 #endif
51 
60 static inline void list_add(struct list_head *new, struct list_head *head)
61 {
62  __list_add(new, head, head->next);
63 }
64 
65 
74 static inline void list_add_tail(struct list_head *new, struct list_head *head)
75 {
76  __list_add(new, head->prev, head);
77 }
78 
79 /*
80  * Delete a list entry by making the prev/next entries
81  * point to each other.
82  *
83  * This is only for internal list manipulation where we know
84  * the prev/next entries already!
85  */
86 static inline void __list_del(struct list_head * prev, struct list_head * next)
87 {
88  next->prev = prev;
89  prev->next = next;
90 }
91 
98 #ifndef CONFIG_DEBUG_LIST
99 static inline void __list_del_entry(struct list_head *entry)
100 {
101  __list_del(entry->prev, entry->next);
102 }
103 
104 static inline void list_del(struct list_head *entry)
105 {
106  __list_del(entry->prev, entry->next);
107  entry->next = LIST_POISON1;
108  entry->prev = LIST_POISON2;
109 }
110 #else
111 extern void __list_del_entry(struct list_head *entry);
112 extern void list_del(struct list_head *entry);
113 #endif
114 
122 static inline void list_replace(struct list_head *old,
123  struct list_head *new)
124 {
125  new->next = old->next;
126  new->next->prev = new;
127  new->prev = old->prev;
128  new->prev->next = new;
129 }
130 
131 static inline void list_replace_init(struct list_head *old,
132  struct list_head *new)
133 {
134  list_replace(old, new);
135  INIT_LIST_HEAD(old);
136 }
137 
142 static inline void list_del_init(struct list_head *entry)
143 {
144  __list_del_entry(entry);
145  INIT_LIST_HEAD(entry);
146 }
147 
153 static inline void list_move(struct list_head *list, struct list_head *head)
154 {
155  __list_del_entry(list);
156  list_add(list, head);
157 }
158 
164 static inline void list_move_tail(struct list_head *list,
165  struct list_head *head)
166 {
167  __list_del_entry(list);
168  list_add_tail(list, head);
169 }
170 
176 static inline int list_is_last(const struct list_head *list,
177  const struct list_head *head)
178 {
179  return list->next == head;
180 }
181 
186 static inline int list_empty(const struct list_head *head)
187 {
188  return head->next == head;
189 }
190 
204 static inline int list_empty_careful(const struct list_head *head)
205 {
206  struct list_head *next = head->next;
207  return (next == head) && (next == head->prev);
208 }
209 
214 static inline void list_rotate_left(struct list_head *head)
215 {
216  struct list_head *first;
217 
218  if (!list_empty(head)) {
219  first = head->next;
220  list_move_tail(first, head);
221  }
222 }
223 
228 static inline int list_is_singular(const struct list_head *head)
229 {
230  return !list_empty(head) && (head->next == head->prev);
231 }
232 
233 static inline void __list_cut_position(struct list_head *list,
234  struct list_head *head, struct list_head *entry)
235 {
236  struct list_head *new_first = entry->next;
237  list->next = head->next;
238  list->next->prev = list;
239  list->prev = entry;
240  entry->next = list;
241  head->next = new_first;
242  new_first->prev = head;
243 }
244 
259 static inline void list_cut_position(struct list_head *list,
260  struct list_head *head, struct list_head *entry)
261 {
262  if (list_empty(head))
263  return;
264  if (list_is_singular(head) &&
265  (head->next != entry && head != entry))
266  return;
267  if (entry == head)
268  INIT_LIST_HEAD(list);
269  else
270  __list_cut_position(list, head, entry);
271 }
272 
273 static inline void __list_splice(const struct list_head *list,
274  struct list_head *prev,
275  struct list_head *next)
276 {
277  struct list_head *first = list->next;
278  struct list_head *last = list->prev;
279 
280  first->prev = prev;
281  prev->next = first;
282 
283  last->next = next;
284  next->prev = last;
285 }
286 
292 static inline void list_splice(const struct list_head *list,
293  struct list_head *head)
294 {
295  if (!list_empty(list))
296  __list_splice(list, head, head->next);
297 }
298 
304 static inline void list_splice_tail(struct list_head *list,
305  struct list_head *head)
306 {
307  if (!list_empty(list))
308  __list_splice(list, head->prev, head);
309 }
310 
318 static inline void list_splice_init(struct list_head *list,
319  struct list_head *head)
320 {
321  if (!list_empty(list)) {
322  __list_splice(list, head, head->next);
323  INIT_LIST_HEAD(list);
324  }
325 }
326 
335 static inline void list_splice_tail_init(struct list_head *list,
336  struct list_head *head)
337 {
338  if (!list_empty(list)) {
339  __list_splice(list, head->prev, head);
340  INIT_LIST_HEAD(list);
341  }
342 }
343 
350 #define list_entry(ptr, type, member) \
351  container_of(ptr, type, member)
352 
361 #define list_first_entry(ptr, type, member) \
362  list_entry((ptr)->next, type, member)
363 
369 #define list_for_each(pos, head) \
370  for (pos = (head)->next; pos != (head); pos = pos->next)
371 
380 #define __list_for_each(pos, head) \
381  for (pos = (head)->next; pos != (head); pos = pos->next)
382 
388 #define list_for_each_prev(pos, head) \
389  for (pos = (head)->prev; pos != (head); pos = pos->prev)
390 
397 #define list_for_each_safe(pos, n, head) \
398  for (pos = (head)->next, n = pos->next; pos != (head); \
399  pos = n, n = pos->next)
400 
407 #define list_for_each_prev_safe(pos, n, head) \
408  for (pos = (head)->prev, n = pos->prev; \
409  pos != (head); \
410  pos = n, n = pos->prev)
411 
418 #define list_for_each_entry(pos, head, member) \
419  for (pos = list_entry((head)->next, typeof(*pos), member); \
420  &pos->member != (head); \
421  pos = list_entry(pos->member.next, typeof(*pos), member))
422 
429 #define list_for_each_entry_reverse(pos, head, member) \
430  for (pos = list_entry((head)->prev, typeof(*pos), member); \
431  &pos->member != (head); \
432  pos = list_entry(pos->member.prev, typeof(*pos), member))
433 
442 #define list_prepare_entry(pos, head, member) \
443  ((pos) ? : list_entry(head, typeof(*pos), member))
444 
454 #define list_for_each_entry_continue(pos, head, member) \
455  for (pos = list_entry(pos->member.next, typeof(*pos), member); \
456  &pos->member != (head); \
457  pos = list_entry(pos->member.next, typeof(*pos), member))
458 
468 #define list_for_each_entry_continue_reverse(pos, head, member) \
469  for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
470  &pos->member != (head); \
471  pos = list_entry(pos->member.prev, typeof(*pos), member))
472 
481 #define list_for_each_entry_from(pos, head, member) \
482  for (; &pos->member != (head); \
483  pos = list_entry(pos->member.next, typeof(*pos), member))
484 
492 #define list_for_each_entry_safe(pos, n, head, member) \
493  for (pos = list_entry((head)->next, typeof(*pos), member), \
494  n = list_entry(pos->member.next, typeof(*pos), member); \
495  &pos->member != (head); \
496  pos = n, n = list_entry(n->member.next, typeof(*n), member))
497 
508 #define list_for_each_entry_safe_continue(pos, n, head, member) \
509  for (pos = list_entry(pos->member.next, typeof(*pos), member), \
510  n = list_entry(pos->member.next, typeof(*pos), member); \
511  &pos->member != (head); \
512  pos = n, n = list_entry(n->member.next, typeof(*n), member))
513 
524 #define list_for_each_entry_safe_from(pos, n, head, member) \
525  for (n = list_entry(pos->member.next, typeof(*pos), member); \
526  &pos->member != (head); \
527  pos = n, n = list_entry(n->member.next, typeof(*n), member))
528 
539 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
540  for (pos = list_entry((head)->prev, typeof(*pos), member), \
541  n = list_entry(pos->member.prev, typeof(*pos), member); \
542  &pos->member != (head); \
543  pos = n, n = list_entry(n->member.prev, typeof(*n), member))
544 
557 #define list_safe_reset_next(pos, n, member) \
558  n = list_entry(pos->member.next, typeof(*pos), member)
559 
560 /*
561  * Double linked lists with a single pointer list head.
562  * Mostly useful for hash tables where the two pointer list head is
563  * too wasteful.
564  * You lose the ability to access the tail in O(1).
565  */
566 
567 #define HLIST_HEAD_INIT { .first = NULL }
568 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
569 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
570 static inline void INIT_HLIST_NODE(struct hlist_node *h)
571 {
572  h->next = NULL;
573  h->pprev = NULL;
574 }
575 
576 static inline int hlist_unhashed(const struct hlist_node *h)
577 {
578  return !h->pprev;
579 }
580 
581 static inline int hlist_empty(const struct hlist_head *h)
582 {
583  return !h->first;
584 }
585 
586 static inline void __hlist_del(struct hlist_node *n)
587 {
588  struct hlist_node *next = n->next;
589  struct hlist_node **pprev = n->pprev;
590  *pprev = next;
591  if (next)
592  next->pprev = pprev;
593 }
594 
595 static inline void hlist_del(struct hlist_node *n)
596 {
597  __hlist_del(n);
598  n->next = LIST_POISON1;
599  n->pprev = LIST_POISON2;
600 }
601 
602 static inline void hlist_del_init(struct hlist_node *n)
603 {
604  if (!hlist_unhashed(n)) {
605  __hlist_del(n);
606  INIT_HLIST_NODE(n);
607  }
608 }
609 
610 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
611 {
612  struct hlist_node *first = h->first;
613  n->next = first;
614  if (first)
615  first->pprev = &n->next;
616  h->first = n;
617  n->pprev = &h->first;
618 }
619 
620 /* next must be != NULL */
621 static inline void hlist_add_before(struct hlist_node *n,
622  struct hlist_node *next)
623 {
624  n->pprev = next->pprev;
625  n->next = next;
626  next->pprev = &n->next;
627  *(n->pprev) = n;
628 }
629 
630 static inline void hlist_add_after(struct hlist_node *n,
631  struct hlist_node *next)
632 {
633  next->next = n->next;
634  n->next = next;
635  next->pprev = &n->next;
636 
637  if(next->next)
638  next->next->pprev = &next->next;
639 }
640 
641 /* after that we'll appear to be on some hlist and hlist_del will work */
642 static inline void hlist_add_fake(struct hlist_node *n)
643 {
644  n->pprev = &n->next;
645 }
646 
647 /*
648  * Move a list from one list head to another. Fixup the pprev
649  * reference of the first entry if it exists.
650  */
651 static inline void hlist_move_list(struct hlist_head *old,
652  struct hlist_head *new)
653 {
654  new->first = old->first;
655  if (new->first)
656  new->first->pprev = &new->first;
657  old->first = NULL;
658 }
659 
660 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
661 
662 #define hlist_for_each(pos, head) \
663  for (pos = (head)->first; pos ; pos = pos->next)
664 
665 #define hlist_for_each_safe(pos, n, head) \
666  for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
667  pos = n)
668 
676 #define hlist_for_each_entry(tpos, pos, head, member) \
677  for (pos = (head)->first; \
678  pos && \
679  ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
680  pos = pos->next)
681 
688 #define hlist_for_each_entry_continue(tpos, pos, member) \
689  for (pos = (pos)->next; \
690  pos && \
691  ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
692  pos = pos->next)
693 
700 #define hlist_for_each_entry_from(tpos, pos, member) \
701  for (; pos && \
702  ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
703  pos = pos->next)
704 
713 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
714  for (pos = (head)->first; \
715  pos && ({ n = pos->next; 1; }) && \
716  ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
717  pos = n)
718 
719 #endif