The Netsukuku Project  0.0.9
An Alternative routing method
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
linux_list.h
Go to the documentation of this file.
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
3 
4 #undef offsetof
5 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
6 
15 #define container_of(ptr, type, member) ({ \
16  const typeof( ((type *)0)->member ) *__mptr = (ptr); \
17  (type *)( (char *)__mptr - offsetof(type,member) );})
18 
19 /*
20  * Check at compile time that something is of a particular type.
21  * Always evaluates to 1 so you may use it easily in comparisons.
22  */
23 #define typecheck(type,x) \
24 ({ type __dummy; \
25  typeof(x) __dummy2; \
26  (void)(&__dummy == &__dummy2); \
27  1; \
28 })
29 
30 #define prefetch(x) 1
31 
32 /* empty define to make this work in userspace -HW */
33 #define smp_wmb()
34 
35 /*
36  * These are non-NULL pointers that will result in page faults
37  * under normal circumstances, used to verify that nobody uses
38  * non-initialized list entries.
39  */
40 #define LIST_POISON1 ((void *) 0x00100100)
41 #define LIST_POISON2 ((void *) 0x00200200)
42 
43 /*
44  * Simple doubly linked list implementation.
45  *
46  * Some of the internal functions ("__xxx") are useful when
47  * manipulating whole lists rather than single entries, as
48  * sometimes we already know the next/prev entries and we can
49  * generate better code by using them directly rather than
50  * using the generic single-entry routines.
51  */
52 
53 struct list_head {
54  struct list_head *next, *prev;
55 };
56 
57 #define LIST_HEAD_INIT(name) { &(name), &(name) }
58 
59 #define LIST_HEAD(name) \
60  struct list_head name = LIST_HEAD_INIT(name)
61 
62 #define INIT_LIST_HEAD(ptr) do { \
63  (ptr)->next = (ptr); (ptr)->prev = (ptr); \
64 } while (0)
65 
66 /*
67  * Insert a new entry between two known consecutive entries.
68  *
69  * This is only for internal list manipulation where we know
70  * the prev/next entries already!
71  */
72 static inline void __list_add(struct list_head *new,
73  struct list_head *prev,
74  struct list_head *next)
75 {
76  next->prev = new;
77  new->next = next;
78  new->prev = prev;
79  prev->next = new;
80 }
81 
90 static inline void list_add(struct list_head *new, struct list_head *head)
91 {
92  __list_add(new, head, head->next);
93 }
94 
103 static inline void list_add_tail(struct list_head *new, struct list_head *head)
104 {
105  __list_add(new, head->prev, head);
106 }
107 
108 /*
109  * Insert a new entry between two known consecutive entries.
110  *
111  * This is only for internal list manipulation where we know
112  * the prev/next entries already!
113  */
114 static inline void __list_add_rcu(struct list_head * new,
115  struct list_head * prev, struct list_head * next)
116 {
117  new->next = next;
118  new->prev = prev;
119  smp_wmb();
120  next->prev = new;
121  prev->next = new;
122 }
123 
140 static inline void list_add_rcu(struct list_head *new, struct list_head *head)
141 {
142  __list_add_rcu(new, head, head->next);
143 }
144 
161 static inline void list_add_tail_rcu(struct list_head *new,
162  struct list_head *head)
163 {
164  __list_add_rcu(new, head->prev, head);
165 }
166 
167 /*
168  * Delete a list entry by making the prev/next entries
169  * point to each other.
170  *
171  * This is only for internal list manipulation where we know
172  * the prev/next entries already!
173  */
174 static inline void __list_del(struct list_head * prev, struct list_head * next)
175 {
176  next->prev = prev;
177  prev->next = next;
178 }
179 
186 static inline void list_del(struct list_head *entry)
187 {
188  __list_del(entry->prev, entry->next);
189  entry->next = LIST_POISON1;
190  entry->prev = LIST_POISON2;
191 }
192 
217 static inline void list_del_rcu(struct list_head *entry)
218 {
219  __list_del(entry->prev, entry->next);
220  entry->prev = LIST_POISON2;
221 }
222 
227 static inline void list_del_init(struct list_head *entry)
228 {
229  __list_del(entry->prev, entry->next);
230  INIT_LIST_HEAD(entry);
231 }
232 
238 static inline void list_move(struct list_head *list, struct list_head *head)
239 {
240  __list_del(list->prev, list->next);
241  list_add(list, head);
242 }
243 
249 static inline void list_move_tail(struct list_head *list,
250  struct list_head *head)
251 {
252  __list_del(list->prev, list->next);
253  list_add_tail(list, head);
254 }
255 
260 static inline int list_empty(const struct list_head *head)
261 {
262  return head->next == head;
263 }
264 
277 static inline int list_empty_careful(const struct list_head *head)
278 {
279  struct list_head *next = head->next;
280  return (next == head) && (next == head->prev);
281 }
282 
283 static inline void __list_splice(struct list_head *list,
284  struct list_head *head)
285 {
286  struct list_head *first = list->next;
287  struct list_head *last = list->prev;
288  struct list_head *at = head->next;
289 
290  first->prev = head;
291  head->next = first;
292 
293  last->next = at;
294  at->prev = last;
295 }
296 
302 static inline void list_splice(struct list_head *list, struct list_head *head)
303 {
304  if (!list_empty(list))
305  __list_splice(list, head);
306 }
307 
315 static inline void list_splice_init(struct list_head *list,
316  struct list_head *head)
317 {
318  if (!list_empty(list)) {
319  __list_splice(list, head);
320  INIT_LIST_HEAD(list);
321  }
322 }
323 
330 #define list_entry(ptr, type, member) \
331  container_of(ptr, type, member)
332 
338 #define list_for_each(pos, head) \
339  for (pos = (head)->next, prefetch(pos->next); pos != (head); \
340  pos = pos->next, prefetch(pos->next))
341 
352 #define __list_for_each(pos, head) \
353  for (pos = (head)->next; pos != (head); pos = pos->next)
354 
360 #define list_for_each_prev(pos, head) \
361  for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
362  pos = pos->prev, prefetch(pos->prev))
363 
370 #define list_for_each_safe(pos, n, head) \
371  for (pos = (head)->next, n = pos->next; pos != (head); \
372  pos = n, n = pos->next)
373 
380 #define list_for_each_entry(pos, head, member) \
381  for (pos = list_entry((head)->next, typeof(*pos), member), \
382  prefetch(pos->member.next); \
383  &pos->member != (head); \
384  pos = list_entry(pos->member.next, typeof(*pos), member), \
385  prefetch(pos->member.next))
386 
393 #define list_for_each_entry_reverse(pos, head, member) \
394  for (pos = list_entry((head)->prev, typeof(*pos), member), \
395  prefetch(pos->member.prev); \
396  &pos->member != (head); \
397  pos = list_entry(pos->member.prev, typeof(*pos), member), \
398  prefetch(pos->member.prev))
399 
407 #define list_prepare_entry(pos, head, member) \
408  ((pos) ? : list_entry(head, typeof(*pos), member))
409 
417 #define list_for_each_entry_continue(pos, head, member) \
418  for (pos = list_entry(pos->member.next, typeof(*pos), member), \
419  prefetch(pos->member.next); \
420  &pos->member != (head); \
421  pos = list_entry(pos->member.next, typeof(*pos), member), \
422  prefetch(pos->member.next))
423 
431 #define list_for_each_entry_safe(pos, n, head, member) \
432  for (pos = list_entry((head)->next, typeof(*pos), member), \
433  n = list_entry(pos->member.next, typeof(*pos), member); \
434  &pos->member != (head); \
435  pos = n, n = list_entry(n->member.next, typeof(*n), member))
436 
446 #define list_for_each_rcu(pos, head) \
447  for (pos = (head)->next, prefetch(pos->next); pos != (head); \
448  pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
449 
450 #define __list_for_each_rcu(pos, head) \
451  for (pos = (head)->next; pos != (head); \
452  pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
453 
465 #define list_for_each_safe_rcu(pos, n, head) \
466  for (pos = (head)->next, n = pos->next; pos != (head); \
467  pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
468 
479 #define list_for_each_entry_rcu(pos, head, member) \
480  for (pos = list_entry((head)->next, typeof(*pos), member), \
481  prefetch(pos->member.next); \
482  &pos->member != (head); \
483  pos = list_entry(pos->member.next, typeof(*pos), member), \
484  ({ smp_read_barrier_depends(); 0;}), \
485  prefetch(pos->member.next))
486 
487 
498 #define list_for_each_continue_rcu(pos, head) \
499  for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
500  (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
501 
502 /*
503  * Double linked lists with a single pointer list head.
504  * Mostly useful for hash tables where the two pointer list head is
505  * too wasteful.
506  * You lose the ability to access the tail in O(1).
507  */
508 
509 struct hlist_head {
510  struct hlist_node *first;
511 };
512 
513 struct hlist_node {
514  struct hlist_node *next, **pprev;
515 };
516 
517 #define HLIST_HEAD_INIT { .first = NULL }
518 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
519 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
520 #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
521 
522 static inline int hlist_unhashed(const struct hlist_node *h)
523 {
524  return !h->pprev;
525 }
526 
527 static inline int hlist_empty(const struct hlist_head *h)
528 {
529  return !h->first;
530 }
531 
532 static inline void __hlist_del(struct hlist_node *n)
533 {
534  struct hlist_node *next = n->next;
535  struct hlist_node **pprev = n->pprev;
536  *pprev = next;
537  if (next)
538  next->pprev = pprev;
539 }
540 
541 static inline void hlist_del(struct hlist_node *n)
542 {
543  __hlist_del(n);
544  n->next = LIST_POISON1;
545  n->pprev = LIST_POISON2;
546 }
547 
567 static inline void hlist_del_rcu(struct hlist_node *n)
568 {
569  __hlist_del(n);
570  n->pprev = LIST_POISON2;
571 }
572 
573 static inline void hlist_del_init(struct hlist_node *n)
574 {
575  if (n->pprev) {
576  __hlist_del(n);
577  INIT_HLIST_NODE(n);
578  }
579 }
580 
581 #define hlist_del_rcu_init hlist_del_init
582 
583 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
584 {
585  struct hlist_node *first = h->first;
586  n->next = first;
587  if (first)
588  first->pprev = &n->next;
589  h->first = n;
590  n->pprev = &h->first;
591 }
592 
593 
613 static inline void hlist_add_head_rcu(struct hlist_node *n,
614  struct hlist_head *h)
615 {
616  struct hlist_node *first = h->first;
617  n->next = first;
618  n->pprev = &h->first;
619  smp_wmb();
620  if (first)
621  first->pprev = &n->next;
622  h->first = n;
623 }
624 
625 /* next must be != NULL */
626 static inline void hlist_add_before(struct hlist_node *n,
627  struct hlist_node *next)
628 {
629  n->pprev = next->pprev;
630  n->next = next;
631  next->pprev = &n->next;
632  *(n->pprev) = n;
633 }
634 
635 static inline void hlist_add_after(struct hlist_node *n,
636  struct hlist_node *next)
637 {
638  next->next = n->next;
639  n->next = next;
640  next->pprev = &n->next;
641 
642  if(next->next)
643  next->next->pprev = &next->next;
644 }
645 
646 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
647 
648 #define hlist_for_each(pos, head) \
649  for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
650  pos = pos->next)
651 
652 #define hlist_for_each_safe(pos, n, head) \
653  for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
654  pos = n)
655 
663 #define hlist_for_each_entry(tpos, pos, head, member) \
664  for (pos = (head)->first; \
665  pos && ({ prefetch(pos->next); 1;}) && \
666  ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
667  pos = pos->next)
668 
675 #define hlist_for_each_entry_continue(tpos, pos, member) \
676  for (pos = (pos)->next; \
677  pos && ({ prefetch(pos->next); 1;}) && \
678  ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
679  pos = pos->next)
680 
687 #define hlist_for_each_entry_from(tpos, pos, member) \
688  for (; pos && ({ prefetch(pos->next); 1;}) && \
689  ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
690  pos = pos->next)
691 
700 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
701  for (pos = (head)->first; \
702  pos && ({ n = pos->next; 1; }) && \
703  ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
704  pos = n)
705 
717 #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
718  for (pos = (head)->first; \
719  pos && ({ prefetch(pos->next); 1;}) && \
720  ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
721  pos = pos->next, ({ smp_read_barrier_depends(); 0; }) )
722 
723 #endif
struct hlist_node * next
Definition: linux_list.h:514
Definition: linux_list.h:509
static void list_add_tail_rcu(struct list_head *new, struct list_head *head)
Definition: linux_list.h:161
#define INIT_LIST_HEAD(ptr)
Definition: linux_list.h:62
struct list_head * next
Definition: linux_list.h:54
#define INIT_HLIST_NODE(ptr)
Definition: linux_list.h:520
static void list_add_rcu(struct list_head *new, struct list_head *head)
Definition: linux_list.h:140
static void __list_splice(struct list_head *list, struct list_head *head)
Definition: linux_list.h:283
static void list_del_rcu(struct list_head *entry)
Definition: linux_list.h:217
static void list_del(struct list_head *entry)
Definition: linux_list.h:186
static int hlist_empty(const struct hlist_head *h)
Definition: linux_list.h:527
static void list_splice_init(struct list_head *list, struct list_head *head)
Definition: linux_list.h:315
static void __list_add_rcu(struct list_head *new, struct list_head *prev, struct list_head *next)
Definition: linux_list.h:114
struct list_head * prev
Definition: linux_list.h:54
static void __hlist_del(struct hlist_node *n)
Definition: linux_list.h:532
static void list_add_tail(struct list_head *new, struct list_head *head)
Definition: linux_list.h:103
Definition: linux_list.h:53
static void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
Definition: linux_list.h:583
#define smp_wmb()
Definition: linux_list.h:33
#define LIST_POISON1
Definition: linux_list.h:40
static void hlist_add_before(struct hlist_node *n, struct hlist_node *next)
Definition: linux_list.h:626
static void list_move(struct list_head *list, struct list_head *head)
Definition: linux_list.h:238
static void hlist_del_init(struct hlist_node *n)
Definition: linux_list.h:573
static void hlist_del_rcu(struct hlist_node *n)
Definition: linux_list.h:567
struct hlist_node * first
Definition: linux_list.h:510
static void hlist_del(struct hlist_node *n)
Definition: linux_list.h:541
static void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
Definition: linux_list.h:72
static void list_move_tail(struct list_head *list, struct list_head *head)
Definition: linux_list.h:249
#define LIST_POISON2
Definition: linux_list.h:41
static void hlist_add_after(struct hlist_node *n, struct hlist_node *next)
Definition: linux_list.h:635
static void list_splice(struct list_head *list, struct list_head *head)
Definition: linux_list.h:302
static int list_empty(const struct list_head *head)
Definition: linux_list.h:260
static void hlist_add_head_rcu(struct hlist_node *n, struct hlist_head *h)
Definition: linux_list.h:613
struct hlist_node ** pprev
Definition: linux_list.h:514
static int hlist_unhashed(const struct hlist_node *h)
Definition: linux_list.h:522
static void list_add(struct list_head *new, struct list_head *head)
Definition: linux_list.h:90
static void list_del_init(struct list_head *entry)
Definition: linux_list.h:227
Definition: linux_list.h:513
static void __list_del(struct list_head *prev, struct list_head *next)
Definition: linux_list.h:174
static int list_empty_careful(const struct list_head *head)
Definition: linux_list.h:277