Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / libnfnetlink / include / linux_list.h
blobde182a476470616cf4117239d2f7bab60f0c00b1
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
4 #include <stddef.h>
6 #undef offsetof
7 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
9 /**
10 * container_of - cast a member of a structure out to the containing structure
12 * @ptr: the pointer to the member.
13 * @type: the type of the container struct this is embedded in.
14 * @member: the name of the member within the struct.
17 #define container_of(ptr, type, member) ({ \
18 typeof( ((type *)0)->member ) *__mptr = (ptr); \
19 (type *)( (char *)__mptr - offsetof(type,member) );})
22 * Check at compile time that something is of a particular type.
23 * Always evaluates to 1 so you may use it easily in comparisons.
25 #define typecheck(type,x) \
26 ({ type __dummy; \
27 typeof(x) __dummy2; \
28 (void)(&__dummy == &__dummy2); \
29 1; \
32 #define prefetch(x) 1
34 /* empty define to make this work in userspace -HW */
35 #ifndef smp_wmb
36 #define smp_wmb()
37 #endif
40 * These are non-NULL pointers that will result in page faults
41 * under normal circumstances, used to verify that nobody uses
42 * non-initialized list entries.
44 #define LIST_POISON1 ((void *) 0x00100100)
45 #define LIST_POISON2 ((void *) 0x00200200)
48 * Simple doubly linked list implementation.
50 * Some of the internal functions ("__xxx") are useful when
51 * manipulating whole lists rather than single entries, as
52 * sometimes we already know the next/prev entries and we can
53 * generate better code by using them directly rather than
54 * using the generic single-entry routines.
57 struct list_head {
58 struct list_head *next, *prev;
61 #define LIST_HEAD_INIT(name) { &(name), &(name) }
63 #define LIST_HEAD(name) \
64 struct list_head name = LIST_HEAD_INIT(name)
66 #define INIT_LIST_HEAD(ptr) do { \
67 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
68 } while (0)
71 * Insert a new entry between two known consecutive entries.
73 * This is only for internal list manipulation where we know
74 * the prev/next entries already!
76 static inline void __list_add(struct list_head *new,
77 struct list_head *prev,
78 struct list_head *next)
80 next->prev = new;
81 new->next = next;
82 new->prev = prev;
83 prev->next = new;
86 /**
87 * list_add - add a new entry
88 * @new: new entry to be added
89 * @head: list head to add it after
91 * Insert a new entry after the specified head.
92 * This is good for implementing stacks.
94 static inline void list_add(struct list_head *new, struct list_head *head)
96 __list_add(new, head, head->next);
99 /**
100 * list_add_tail - add a new entry
101 * @new: new entry to be added
102 * @head: list head to add it before
104 * Insert a new entry before the specified head.
105 * This is useful for implementing queues.
107 static inline void list_add_tail(struct list_head *new, struct list_head *head)
109 __list_add(new, head->prev, head);
113 * Insert a new entry between two known consecutive entries.
115 * This is only for internal list manipulation where we know
116 * the prev/next entries already!
118 static inline void __list_add_rcu(struct list_head * new,
119 struct list_head * prev, struct list_head * next)
121 new->next = next;
122 new->prev = prev;
123 smp_wmb();
124 next->prev = new;
125 prev->next = new;
129 * list_add_rcu - add a new entry to rcu-protected list
130 * @new: new entry to be added
131 * @head: list head to add it after
133 * Insert a new entry after the specified head.
134 * This is good for implementing stacks.
136 * The caller must take whatever precautions are necessary
137 * (such as holding appropriate locks) to avoid racing
138 * with another list-mutation primitive, such as list_add_rcu()
139 * or list_del_rcu(), running on this same list.
140 * However, it is perfectly legal to run concurrently with
141 * the _rcu list-traversal primitives, such as
142 * list_for_each_entry_rcu().
144 static inline void list_add_rcu(struct list_head *new, struct list_head *head)
146 __list_add_rcu(new, head, head->next);
150 * list_add_tail_rcu - add a new entry to rcu-protected list
151 * @new: new entry to be added
152 * @head: list head to add it before
154 * Insert a new entry before the specified head.
155 * This is useful for implementing queues.
157 * The caller must take whatever precautions are necessary
158 * (such as holding appropriate locks) to avoid racing
159 * with another list-mutation primitive, such as list_add_tail_rcu()
160 * or list_del_rcu(), running on this same list.
161 * However, it is perfectly legal to run concurrently with
162 * the _rcu list-traversal primitives, such as
163 * list_for_each_entry_rcu().
165 static inline void list_add_tail_rcu(struct list_head *new,
166 struct list_head *head)
168 __list_add_rcu(new, head->prev, head);
172 * Delete a list entry by making the prev/next entries
173 * point to each other.
175 * This is only for internal list manipulation where we know
176 * the prev/next entries already!
178 static inline void __list_del(struct list_head * prev, struct list_head * next)
180 next->prev = prev;
181 prev->next = next;
185 * list_del - deletes entry from list.
186 * @entry: the element to delete from the list.
187 * Note: list_empty on entry does not return true after this, the entry is
188 * in an undefined state.
190 static inline void list_del(struct list_head *entry)
192 __list_del(entry->prev, entry->next);
193 entry->next = LIST_POISON1;
194 entry->prev = LIST_POISON2;
198 * list_del_rcu - deletes entry from list without re-initialization
199 * @entry: the element to delete from the list.
201 * Note: list_empty on entry does not return true after this,
202 * the entry is in an undefined state. It is useful for RCU based
203 * lockfree traversal.
205 * In particular, it means that we can not poison the forward
206 * pointers that may still be used for walking the list.
208 * The caller must take whatever precautions are necessary
209 * (such as holding appropriate locks) to avoid racing
210 * with another list-mutation primitive, such as list_del_rcu()
211 * or list_add_rcu(), running on this same list.
212 * However, it is perfectly legal to run concurrently with
213 * the _rcu list-traversal primitives, such as
214 * list_for_each_entry_rcu().
216 * Note that the caller is not permitted to immediately free
217 * the newly deleted entry. Instead, either synchronize_kernel()
218 * or call_rcu() must be used to defer freeing until an RCU
219 * grace period has elapsed.
221 static inline void list_del_rcu(struct list_head *entry)
223 __list_del(entry->prev, entry->next);
224 entry->prev = LIST_POISON2;
228 * list_del_init - deletes entry from list and reinitialize it.
229 * @entry: the element to delete from the list.
231 static inline void list_del_init(struct list_head *entry)
233 __list_del(entry->prev, entry->next);
234 INIT_LIST_HEAD(entry);
238 * list_move - delete from one list and add as another's head
239 * @list: the entry to move
240 * @head: the head that will precede our entry
242 static inline void list_move(struct list_head *list, struct list_head *head)
244 __list_del(list->prev, list->next);
245 list_add(list, head);
249 * list_move_tail - delete from one list and add as another's tail
250 * @list: the entry to move
251 * @head: the head that will follow our entry
253 static inline void list_move_tail(struct list_head *list,
254 struct list_head *head)
256 __list_del(list->prev, list->next);
257 list_add_tail(list, head);
261 * list_empty - tests whether a list is empty
262 * @head: the list to test.
264 static inline int list_empty(const struct list_head *head)
266 return head->next == head;
270 * list_empty_careful - tests whether a list is
271 * empty _and_ checks that no other CPU might be
272 * in the process of still modifying either member
274 * NOTE: using list_empty_careful() without synchronization
275 * can only be safe if the only activity that can happen
276 * to the list entry is list_del_init(). Eg. it cannot be used
277 * if another CPU could re-list_add() it.
279 * @head: the list to test.
281 static inline int list_empty_careful(const struct list_head *head)
283 struct list_head *next = head->next;
284 return (next == head) && (next == head->prev);
287 static inline void __list_splice(struct list_head *list,
288 struct list_head *head)
290 struct list_head *first = list->next;
291 struct list_head *last = list->prev;
292 struct list_head *at = head->next;
294 first->prev = head;
295 head->next = first;
297 last->next = at;
298 at->prev = last;
302 * list_splice - join two lists
303 * @list: the new list to add.
304 * @head: the place to add it in the first list.
306 static inline void list_splice(struct list_head *list, struct list_head *head)
308 if (!list_empty(list))
309 __list_splice(list, head);
313 * list_splice_init - join two lists and reinitialise the emptied list.
314 * @list: the new list to add.
315 * @head: the place to add it in the first list.
317 * The list at @list is reinitialised
319 static inline void list_splice_init(struct list_head *list,
320 struct list_head *head)
322 if (!list_empty(list)) {
323 __list_splice(list, head);
324 INIT_LIST_HEAD(list);
329 * list_entry - get the struct for this entry
330 * @ptr: the &struct list_head pointer.
331 * @type: the type of the struct this is embedded in.
332 * @member: the name of the list_struct within the struct.
334 #define list_entry(ptr, type, member) \
335 container_of(ptr, type, member)
338 * list_for_each - iterate over a list
339 * @pos: the &struct list_head to use as a loop counter.
340 * @head: the head for your list.
342 #define list_for_each(pos, head) \
343 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
344 pos = pos->next, prefetch(pos->next))
347 * __list_for_each - iterate over a list
348 * @pos: the &struct list_head to use as a loop counter.
349 * @head: the head for your list.
351 * This variant differs from list_for_each() in that it's the
352 * simplest possible list iteration code, no prefetching is done.
353 * Use this for code that knows the list to be very short (empty
354 * or 1 entry) most of the time.
356 #define __list_for_each(pos, head) \
357 for (pos = (head)->next; pos != (head); pos = pos->next)
360 * list_for_each_prev - iterate over a list backwards
361 * @pos: the &struct list_head to use as a loop counter.
362 * @head: the head for your list.
364 #define list_for_each_prev(pos, head) \
365 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
366 pos = pos->prev, prefetch(pos->prev))
369 * list_for_each_safe - iterate over a list safe against removal of list entry
370 * @pos: the &struct list_head to use as a loop counter.
371 * @n: another &struct list_head to use as temporary storage
372 * @head: the head for your list.
374 #define list_for_each_safe(pos, n, head) \
375 for (pos = (head)->next, n = pos->next; pos != (head); \
376 pos = n, n = pos->next)
379 * list_for_each_entry - iterate over list of given type
380 * @pos: the type * to use as a loop counter.
381 * @head: the head for your list.
382 * @member: the name of the list_struct within the struct.
384 #define list_for_each_entry(pos, head, member) \
385 for (pos = list_entry((head)->next, typeof(*pos), member), \
386 prefetch(pos->member.next); \
387 &pos->member != (head); \
388 pos = list_entry(pos->member.next, typeof(*pos), member), \
389 prefetch(pos->member.next))
392 * list_for_each_entry_reverse - iterate backwards over list of given type.
393 * @pos: the type * to use as a loop counter.
394 * @head: the head for your list.
395 * @member: the name of the list_struct within the struct.
397 #define list_for_each_entry_reverse(pos, head, member) \
398 for (pos = list_entry((head)->prev, typeof(*pos), member), \
399 prefetch(pos->member.prev); \
400 &pos->member != (head); \
401 pos = list_entry(pos->member.prev, typeof(*pos), member), \
402 prefetch(pos->member.prev))
405 * list_prepare_entry - prepare a pos entry for use as a start point in
406 * list_for_each_entry_continue
407 * @pos: the type * to use as a start point
408 * @head: the head of the list
409 * @member: the name of the list_struct within the struct.
411 #define list_prepare_entry(pos, head, member) \
412 ((pos) ? : list_entry(head, typeof(*pos), member))
415 * list_for_each_entry_continue - iterate over list of given type
416 * continuing after existing point
417 * @pos: the type * to use as a loop counter.
418 * @head: the head for your list.
419 * @member: the name of the list_struct within the struct.
421 #define list_for_each_entry_continue(pos, head, member) \
422 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
423 prefetch(pos->member.next); \
424 &pos->member != (head); \
425 pos = list_entry(pos->member.next, typeof(*pos), member), \
426 prefetch(pos->member.next))
429 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
430 * @pos: the type * to use as a loop counter.
431 * @n: another type * to use as temporary storage
432 * @head: the head for your list.
433 * @member: the name of the list_struct within the struct.
435 #define list_for_each_entry_safe(pos, n, head, member) \
436 for (pos = list_entry((head)->next, typeof(*pos), member), \
437 n = list_entry(pos->member.next, typeof(*pos), member); \
438 &pos->member != (head); \
439 pos = n, n = list_entry(n->member.next, typeof(*n), member))
442 * list_for_each_rcu - iterate over an rcu-protected list
443 * @pos: the &struct list_head to use as a loop counter.
444 * @head: the head for your list.
446 * This list-traversal primitive may safely run concurrently with
447 * the _rcu list-mutation primitives such as list_add_rcu()
448 * as long as the traversal is guarded by rcu_read_lock().
450 #define list_for_each_rcu(pos, head) \
451 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
452 pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
454 #define __list_for_each_rcu(pos, head) \
455 for (pos = (head)->next; pos != (head); \
456 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
459 * list_for_each_safe_rcu - iterate over an rcu-protected list safe
460 * against removal of list entry
461 * @pos: the &struct list_head to use as a loop counter.
462 * @n: another &struct list_head to use as temporary storage
463 * @head: the head for your list.
465 * This list-traversal primitive may safely run concurrently with
466 * the _rcu list-mutation primitives such as list_add_rcu()
467 * as long as the traversal is guarded by rcu_read_lock().
469 #define list_for_each_safe_rcu(pos, n, head) \
470 for (pos = (head)->next, n = pos->next; pos != (head); \
471 pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
474 * list_for_each_entry_rcu - iterate over rcu list of given type
475 * @pos: the type * to use as a loop counter.
476 * @head: the head for your list.
477 * @member: the name of the list_struct within the struct.
479 * This list-traversal primitive may safely run concurrently with
480 * the _rcu list-mutation primitives such as list_add_rcu()
481 * as long as the traversal is guarded by rcu_read_lock().
483 #define list_for_each_entry_rcu(pos, head, member) \
484 for (pos = list_entry((head)->next, typeof(*pos), member), \
485 prefetch(pos->member.next); \
486 &pos->member != (head); \
487 pos = list_entry(pos->member.next, typeof(*pos), member), \
488 ({ smp_read_barrier_depends(); 0;}), \
489 prefetch(pos->member.next))
493 * list_for_each_continue_rcu - iterate over an rcu-protected list
494 * continuing after existing point.
495 * @pos: the &struct list_head to use as a loop counter.
496 * @head: the head for your list.
498 * This list-traversal primitive may safely run concurrently with
499 * the _rcu list-mutation primitives such as list_add_rcu()
500 * as long as the traversal is guarded by rcu_read_lock().
502 #define list_for_each_continue_rcu(pos, head) \
503 for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
504 (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
507 * Double linked lists with a single pointer list head.
508 * Mostly useful for hash tables where the two pointer list head is
509 * too wasteful.
510 * You lose the ability to access the tail in O(1).
513 struct hlist_head {
514 struct hlist_node *first;
517 struct hlist_node {
518 struct hlist_node *next, **pprev;
521 #define HLIST_HEAD_INIT { .first = NULL }
522 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
523 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
524 #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
526 static inline int hlist_unhashed(const struct hlist_node *h)
528 return !h->pprev;
531 static inline int hlist_empty(const struct hlist_head *h)
533 return !h->first;
536 static inline void __hlist_del(struct hlist_node *n)
538 struct hlist_node *next = n->next;
539 struct hlist_node **pprev = n->pprev;
540 *pprev = next;
541 if (next)
542 next->pprev = pprev;
545 static inline void hlist_del(struct hlist_node *n)
547 __hlist_del(n);
548 n->next = LIST_POISON1;
549 n->pprev = LIST_POISON2;
553 * hlist_del_rcu - deletes entry from hash list without re-initialization
554 * @n: the element to delete from the hash list.
556 * Note: list_unhashed() on entry does not return true after this,
557 * the entry is in an undefined state. It is useful for RCU based
558 * lockfree traversal.
560 * In particular, it means that we can not poison the forward
561 * pointers that may still be used for walking the hash list.
563 * The caller must take whatever precautions are necessary
564 * (such as holding appropriate locks) to avoid racing
565 * with another list-mutation primitive, such as hlist_add_head_rcu()
566 * or hlist_del_rcu(), running on this same list.
567 * However, it is perfectly legal to run concurrently with
568 * the _rcu list-traversal primitives, such as
569 * hlist_for_each_entry().
571 static inline void hlist_del_rcu(struct hlist_node *n)
573 __hlist_del(n);
574 n->pprev = LIST_POISON2;
577 static inline void hlist_del_init(struct hlist_node *n)
579 if (n->pprev) {
580 __hlist_del(n);
581 INIT_HLIST_NODE(n);
585 #define hlist_del_rcu_init hlist_del_init
587 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
589 struct hlist_node *first = h->first;
590 n->next = first;
591 if (first)
592 first->pprev = &n->next;
593 h->first = n;
594 n->pprev = &h->first;
599 * hlist_add_head_rcu - adds the specified element to the specified hlist,
600 * while permitting racing traversals.
601 * @n: the element to add to the hash list.
602 * @h: the list to add to.
604 * The caller must take whatever precautions are necessary
605 * (such as holding appropriate locks) to avoid racing
606 * with another list-mutation primitive, such as hlist_add_head_rcu()
607 * or hlist_del_rcu(), running on this same list.
608 * However, it is perfectly legal to run concurrently with
609 * the _rcu list-traversal primitives, such as
610 * hlist_for_each_entry(), but only if smp_read_barrier_depends()
611 * is used to prevent memory-consistency problems on Alpha CPUs.
612 * Regardless of the type of CPU, the list-traversal primitive
613 * must be guarded by rcu_read_lock().
615 * OK, so why don't we have an hlist_for_each_entry_rcu()???
617 static inline void hlist_add_head_rcu(struct hlist_node *n,
618 struct hlist_head *h)
620 struct hlist_node *first = h->first;
621 n->next = first;
622 n->pprev = &h->first;
623 smp_wmb();
624 if (first)
625 first->pprev = &n->next;
626 h->first = n;
629 /* next must be != NULL */
630 static inline void hlist_add_before(struct hlist_node *n,
631 struct hlist_node *next)
633 n->pprev = next->pprev;
634 n->next = next;
635 next->pprev = &n->next;
636 *(n->pprev) = n;
639 static inline void hlist_add_after(struct hlist_node *n,
640 struct hlist_node *next)
642 next->next = n->next;
643 n->next = next;
644 next->pprev = &n->next;
646 if(next->next)
647 next->next->pprev = &next->next;
650 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
652 #define hlist_for_each(pos, head) \
653 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
654 pos = pos->next)
656 #define hlist_for_each_safe(pos, n, head) \
657 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
658 pos = n)
661 * hlist_for_each_entry - iterate over list of given type
662 * @tpos: the type * to use as a loop counter.
663 * @pos: the &struct hlist_node to use as a loop counter.
664 * @head: the head for your list.
665 * @member: the name of the hlist_node within the struct.
667 #define hlist_for_each_entry(tpos, pos, head, member) \
668 for (pos = (head)->first; \
669 pos && ({ prefetch(pos->next); 1;}) && \
670 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
671 pos = pos->next)
674 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
675 * @tpos: the type * to use as a loop counter.
676 * @pos: the &struct hlist_node to use as a loop counter.
677 * @member: the name of the hlist_node within the struct.
679 #define hlist_for_each_entry_continue(tpos, pos, member) \
680 for (pos = (pos)->next; \
681 pos && ({ prefetch(pos->next); 1;}) && \
682 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
683 pos = pos->next)
686 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
687 * @tpos: the type * to use as a loop counter.
688 * @pos: the &struct hlist_node to use as a loop counter.
689 * @member: the name of the hlist_node within the struct.
691 #define hlist_for_each_entry_from(tpos, pos, member) \
692 for (; pos && ({ prefetch(pos->next); 1;}) && \
693 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
694 pos = pos->next)
697 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
698 * @tpos: the type * to use as a loop counter.
699 * @pos: the &struct hlist_node to use as a loop counter.
700 * @n: another &struct hlist_node to use as temporary storage
701 * @head: the head for your list.
702 * @member: the name of the hlist_node within the struct.
704 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
705 for (pos = (head)->first; \
706 pos && ({ n = pos->next; 1; }) && \
707 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
708 pos = n)
711 * hlist_for_each_entry_rcu - iterate over rcu list of given type
712 * @pos: the type * to use as a loop counter.
713 * @pos: the &struct hlist_node to use as a loop counter.
714 * @head: the head for your list.
715 * @member: the name of the hlist_node within the struct.
717 * This list-traversal primitive may safely run concurrently with
718 * the _rcu list-mutation primitives such as hlist_add_rcu()
719 * as long as the traversal is guarded by rcu_read_lock().
721 #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
722 for (pos = (head)->first; \
723 pos && ({ prefetch(pos->next); 1;}) && \
724 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
725 pos = pos->next, ({ smp_read_barrier_depends(); 0; }) )
727 #endif