Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / iptables / libiptc / linux_list.h
blobabdcf88dadd644353779ffb314894cc1ffff4967
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
4 #undef offsetof
5 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
7 /**
8 * container_of - cast a member of a structure out to the containing structure
10 * @ptr: the pointer to the member.
11 * @type: the type of the container struct this is embedded in.
12 * @member: the name of the member within the struct.
15 #define container_of(ptr, type, member) ({ \
16 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
17 (type *)( (char *)__mptr - offsetof(type,member) );})
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.
23 #define typecheck(type,x) \
24 ({ type __dummy; \
25 typeof(x) __dummy2; \
26 (void)(&__dummy == &__dummy2); \
27 1; \
30 #define prefetch(x) 1
32 /* empty define to make this work in userspace -HW */
33 #define smp_wmb()
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.
40 #define LIST_POISON1 ((void *) 0x00100100)
41 #define LIST_POISON2 ((void *) 0x00200200)
44 * Simple doubly linked list implementation.
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.
53 struct list_head {
54 struct list_head *next, *prev;
57 #define LIST_HEAD_INIT(name) { &(name), &(name) }
59 #define LIST_HEAD(name) \
60 struct list_head name = LIST_HEAD_INIT(name)
62 #define INIT_LIST_HEAD(ptr) do { \
63 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
64 } while (0)
67 * Insert a new entry between two known consecutive entries.
69 * This is only for internal list manipulation where we know
70 * the prev/next entries already!
72 static inline void __list_add(struct list_head *new,
73 struct list_head *prev,
74 struct list_head *next)
76 next->prev = new;
77 new->next = next;
78 new->prev = prev;
79 prev->next = new;
82 /**
83 * list_add - add a new entry
84 * @new: new entry to be added
85 * @head: list head to add it after
87 * Insert a new entry after the specified head.
88 * This is good for implementing stacks.
90 static inline void list_add(struct list_head *new, struct list_head *head)
92 __list_add(new, head, head->next);
95 /**
96 * list_add_tail - add a new entry
97 * @new: new entry to be added
98 * @head: list head to add it before
100 * Insert a new entry before the specified head.
101 * This is useful for implementing queues.
103 static inline void list_add_tail(struct list_head *new, struct list_head *head)
105 __list_add(new, head->prev, head);
109 * Insert a new entry between two known consecutive entries.
111 * This is only for internal list manipulation where we know
112 * the prev/next entries already!
114 static inline void __list_add_rcu(struct list_head * new,
115 struct list_head * prev, struct list_head * next)
117 new->next = next;
118 new->prev = prev;
119 smp_wmb();
120 next->prev = new;
121 prev->next = new;
125 * list_add_rcu - add a new entry to rcu-protected list
126 * @new: new entry to be added
127 * @head: list head to add it after
129 * Insert a new entry after the specified head.
130 * This is good for implementing stacks.
132 * The caller must take whatever precautions are necessary
133 * (such as holding appropriate locks) to avoid racing
134 * with another list-mutation primitive, such as list_add_rcu()
135 * or list_del_rcu(), running on this same list.
136 * However, it is perfectly legal to run concurrently with
137 * the _rcu list-traversal primitives, such as
138 * list_for_each_entry_rcu().
140 static inline void list_add_rcu(struct list_head *new, struct list_head *head)
142 __list_add_rcu(new, head, head->next);
146 * list_add_tail_rcu - add a new entry to rcu-protected list
147 * @new: new entry to be added
148 * @head: list head to add it before
150 * Insert a new entry before the specified head.
151 * This is useful for implementing queues.
153 * The caller must take whatever precautions are necessary
154 * (such as holding appropriate locks) to avoid racing
155 * with another list-mutation primitive, such as list_add_tail_rcu()
156 * or list_del_rcu(), running on this same list.
157 * However, it is perfectly legal to run concurrently with
158 * the _rcu list-traversal primitives, such as
159 * list_for_each_entry_rcu().
161 static inline void list_add_tail_rcu(struct list_head *new,
162 struct list_head *head)
164 __list_add_rcu(new, head->prev, head);
168 * Delete a list entry by making the prev/next entries
169 * point to each other.
171 * This is only for internal list manipulation where we know
172 * the prev/next entries already!
174 static inline void __list_del(struct list_head * prev, struct list_head * next)
176 next->prev = prev;
177 prev->next = next;
181 * list_del - deletes entry from list.
182 * @entry: the element to delete from the list.
183 * Note: list_empty on entry does not return true after this, the entry is
184 * in an undefined state.
186 static inline void list_del(struct list_head *entry)
188 __list_del(entry->prev, entry->next);
189 entry->next = LIST_POISON1;
190 entry->prev = LIST_POISON2;
194 * list_del_rcu - deletes entry from list without re-initialization
195 * @entry: the element to delete from the list.
197 * Note: list_empty on entry does not return true after this,
198 * the entry is in an undefined state. It is useful for RCU based
199 * lockfree traversal.
201 * In particular, it means that we can not poison the forward
202 * pointers that may still be used for walking the list.
204 * The caller must take whatever precautions are necessary
205 * (such as holding appropriate locks) to avoid racing
206 * with another list-mutation primitive, such as list_del_rcu()
207 * or list_add_rcu(), running on this same list.
208 * However, it is perfectly legal to run concurrently with
209 * the _rcu list-traversal primitives, such as
210 * list_for_each_entry_rcu().
212 * Note that the caller is not permitted to immediately free
213 * the newly deleted entry. Instead, either synchronize_kernel()
214 * or call_rcu() must be used to defer freeing until an RCU
215 * grace period has elapsed.
217 static inline void list_del_rcu(struct list_head *entry)
219 __list_del(entry->prev, entry->next);
220 entry->prev = LIST_POISON2;
224 * list_del_init - deletes entry from list and reinitialize it.
225 * @entry: the element to delete from the list.
227 static inline void list_del_init(struct list_head *entry)
229 __list_del(entry->prev, entry->next);
230 INIT_LIST_HEAD(entry);
234 * list_move - delete from one list and add as another's head
235 * @list: the entry to move
236 * @head: the head that will precede our entry
238 static inline void list_move(struct list_head *list, struct list_head *head)
240 __list_del(list->prev, list->next);
241 list_add(list, head);
245 * list_move_tail - delete from one list and add as another's tail
246 * @list: the entry to move
247 * @head: the head that will follow our entry
249 static inline void list_move_tail(struct list_head *list,
250 struct list_head *head)
252 __list_del(list->prev, list->next);
253 list_add_tail(list, head);
257 * list_empty - tests whether a list is empty
258 * @head: the list to test.
260 static inline int list_empty(const struct list_head *head)
262 return head->next == head;
266 * list_empty_careful - tests whether a list is
267 * empty _and_ checks that no other CPU might be
268 * in the process of still modifying either member
270 * NOTE: using list_empty_careful() without synchronization
271 * can only be safe if the only activity that can happen
272 * to the list entry is list_del_init(). Eg. it cannot be used
273 * if another CPU could re-list_add() it.
275 * @head: the list to test.
277 static inline int list_empty_careful(const struct list_head *head)
279 struct list_head *next = head->next;
280 return (next == head) && (next == head->prev);
283 static inline void __list_splice(struct list_head *list,
284 struct list_head *head)
286 struct list_head *first = list->next;
287 struct list_head *last = list->prev;
288 struct list_head *at = head->next;
290 first->prev = head;
291 head->next = first;
293 last->next = at;
294 at->prev = last;
298 * list_splice - join two lists
299 * @list: the new list to add.
300 * @head: the place to add it in the first list.
302 static inline void list_splice(struct list_head *list, struct list_head *head)
304 if (!list_empty(list))
305 __list_splice(list, head);
309 * list_splice_init - join two lists and reinitialise the emptied list.
310 * @list: the new list to add.
311 * @head: the place to add it in the first list.
313 * The list at @list is reinitialised
315 static inline void list_splice_init(struct list_head *list,
316 struct list_head *head)
318 if (!list_empty(list)) {
319 __list_splice(list, head);
320 INIT_LIST_HEAD(list);
325 * list_entry - get the struct for this entry
326 * @ptr: the &struct list_head pointer.
327 * @type: the type of the struct this is embedded in.
328 * @member: the name of the list_struct within the struct.
330 #define list_entry(ptr, type, member) \
331 container_of(ptr, type, member)
334 * list_for_each - iterate over a list
335 * @pos: the &struct list_head to use as a loop counter.
336 * @head: the head for your list.
338 #define list_for_each(pos, head) \
339 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
340 pos = pos->next, prefetch(pos->next))
343 * __list_for_each - iterate over a list
344 * @pos: the &struct list_head to use as a loop counter.
345 * @head: the head for your list.
347 * This variant differs from list_for_each() in that it's the
348 * simplest possible list iteration code, no prefetching is done.
349 * Use this for code that knows the list to be very short (empty
350 * or 1 entry) most of the time.
352 #define __list_for_each(pos, head) \
353 for (pos = (head)->next; pos != (head); pos = pos->next)
356 * list_for_each_prev - iterate over a list backwards
357 * @pos: the &struct list_head to use as a loop counter.
358 * @head: the head for your list.
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))
365 * list_for_each_safe - iterate over a list safe against removal of list entry
366 * @pos: the &struct list_head to use as a loop counter.
367 * @n: another &struct list_head to use as temporary storage
368 * @head: the head for your list.
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)
375 * list_for_each_entry - iterate over list of given type
376 * @pos: the type * to use as a loop counter.
377 * @head: the head for your list.
378 * @member: the name of the list_struct within the struct.
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))
388 * list_for_each_entry_reverse - iterate backwards over list of given type.
389 * @pos: the type * to use as a loop counter.
390 * @head: the head for your list.
391 * @member: the name of the list_struct within the struct.
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))
401 * list_prepare_entry - prepare a pos entry for use as a start point in
402 * list_for_each_entry_continue
403 * @pos: the type * to use as a start point
404 * @head: the head of the list
405 * @member: the name of the list_struct within the struct.
407 #define list_prepare_entry(pos, head, member) \
408 ((pos) ? : list_entry(head, typeof(*pos), member))
411 * list_for_each_entry_continue - iterate over list of given type
412 * continuing after existing point
413 * @pos: the type * to use as a loop counter.
414 * @head: the head for your list.
415 * @member: the name of the list_struct within the struct.
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))
425 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
426 * @pos: the type * to use as a loop counter.
427 * @n: another type * to use as temporary storage
428 * @head: the head for your list.
429 * @member: the name of the list_struct within the struct.
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))
438 * list_for_each_rcu - iterate over an rcu-protected list
439 * @pos: the &struct list_head to use as a loop counter.
440 * @head: the head for your list.
442 * This list-traversal primitive may safely run concurrently with
443 * the _rcu list-mutation primitives such as list_add_rcu()
444 * as long as the traversal is guarded by rcu_read_lock().
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))
450 #define __list_for_each_rcu(pos, head) \
451 for (pos = (head)->next; pos != (head); \
452 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
455 * list_for_each_safe_rcu - iterate over an rcu-protected list safe
456 * against removal of list entry
457 * @pos: the &struct list_head to use as a loop counter.
458 * @n: another &struct list_head to use as temporary storage
459 * @head: the head for your list.
461 * This list-traversal primitive may safely run concurrently with
462 * the _rcu list-mutation primitives such as list_add_rcu()
463 * as long as the traversal is guarded by rcu_read_lock().
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)
470 * list_for_each_entry_rcu - iterate over rcu list of given type
471 * @pos: the type * to use as a loop counter.
472 * @head: the head for your list.
473 * @member: the name of the list_struct within the struct.
475 * This list-traversal primitive may safely run concurrently with
476 * the _rcu list-mutation primitives such as list_add_rcu()
477 * as long as the traversal is guarded by rcu_read_lock().
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))
489 * list_for_each_continue_rcu - iterate over an rcu-protected list
490 * continuing after existing point.
491 * @pos: the &struct list_head to use as a loop counter.
492 * @head: the head for your list.
494 * This list-traversal primitive may safely run concurrently with
495 * the _rcu list-mutation primitives such as list_add_rcu()
496 * as long as the traversal is guarded by rcu_read_lock().
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))
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).
509 struct hlist_head {
510 struct hlist_node *first;
513 struct hlist_node {
514 struct hlist_node *next, **pprev;
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)
522 static inline int hlist_unhashed(const struct hlist_node *h)
524 return !h->pprev;
527 static inline int hlist_empty(const struct hlist_head *h)
529 return !h->first;
532 static inline void __hlist_del(struct hlist_node *n)
534 struct hlist_node *next = n->next;
535 struct hlist_node **pprev = n->pprev;
536 *pprev = next;
537 if (next)
538 next->pprev = pprev;
541 static inline void hlist_del(struct hlist_node *n)
543 __hlist_del(n);
544 n->next = LIST_POISON1;
545 n->pprev = LIST_POISON2;
549 * hlist_del_rcu - deletes entry from hash list without re-initialization
550 * @n: the element to delete from the hash list.
552 * Note: list_unhashed() on entry does not return true after this,
553 * the entry is in an undefined state. It is useful for RCU based
554 * lockfree traversal.
556 * In particular, it means that we can not poison the forward
557 * pointers that may still be used for walking the hash list.
559 * The caller must take whatever precautions are necessary
560 * (such as holding appropriate locks) to avoid racing
561 * with another list-mutation primitive, such as hlist_add_head_rcu()
562 * or hlist_del_rcu(), running on this same list.
563 * However, it is perfectly legal to run concurrently with
564 * the _rcu list-traversal primitives, such as
565 * hlist_for_each_entry().
567 static inline void hlist_del_rcu(struct hlist_node *n)
569 __hlist_del(n);
570 n->pprev = LIST_POISON2;
573 static inline void hlist_del_init(struct hlist_node *n)
575 if (n->pprev) {
576 __hlist_del(n);
577 INIT_HLIST_NODE(n);
581 #define hlist_del_rcu_init hlist_del_init
583 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
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;
595 * hlist_add_head_rcu - adds the specified element to the specified hlist,
596 * while permitting racing traversals.
597 * @n: the element to add to the hash list.
598 * @h: the list to add to.
600 * The caller must take whatever precautions are necessary
601 * (such as holding appropriate locks) to avoid racing
602 * with another list-mutation primitive, such as hlist_add_head_rcu()
603 * or hlist_del_rcu(), running on this same list.
604 * However, it is perfectly legal to run concurrently with
605 * the _rcu list-traversal primitives, such as
606 * hlist_for_each_entry(), but only if smp_read_barrier_depends()
607 * is used to prevent memory-consistency problems on Alpha CPUs.
608 * Regardless of the type of CPU, the list-traversal primitive
609 * must be guarded by rcu_read_lock().
611 * OK, so why don't we have an hlist_for_each_entry_rcu()???
613 static inline void hlist_add_head_rcu(struct hlist_node *n,
614 struct hlist_head *h)
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;
625 /* next must be != NULL */
626 static inline void hlist_add_before(struct hlist_node *n,
627 struct hlist_node *next)
629 n->pprev = next->pprev;
630 n->next = next;
631 next->pprev = &n->next;
632 *(n->pprev) = n;
635 static inline void hlist_add_after(struct hlist_node *n,
636 struct hlist_node *next)
638 next->next = n->next;
639 n->next = next;
640 next->pprev = &n->next;
642 if(next->next)
643 next->next->pprev = &next->next;
646 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
648 #define hlist_for_each(pos, head) \
649 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
650 pos = pos->next)
652 #define hlist_for_each_safe(pos, n, head) \
653 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
654 pos = n)
657 * hlist_for_each_entry - iterate over list of given type
658 * @tpos: the type * to use as a loop counter.
659 * @pos: the &struct hlist_node to use as a loop counter.
660 * @head: the head for your list.
661 * @member: the name of the hlist_node within the struct.
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)
670 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
671 * @tpos: the type * to use as a loop counter.
672 * @pos: the &struct hlist_node to use as a loop counter.
673 * @member: the name of the hlist_node within the struct.
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)
682 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
683 * @tpos: the type * to use as a loop counter.
684 * @pos: the &struct hlist_node to use as a loop counter.
685 * @member: the name of the hlist_node within the struct.
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)
693 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
694 * @tpos: the type * to use as a loop counter.
695 * @pos: the &struct hlist_node to use as a loop counter.
696 * @n: another &struct hlist_node to use as temporary storage
697 * @head: the head for your list.
698 * @member: the name of the hlist_node within the struct.
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)
707 * hlist_for_each_entry_rcu - iterate over rcu list of given type
708 * @pos: the type * to use as a loop counter.
709 * @pos: the &struct hlist_node to use as a loop counter.
710 * @head: the head for your list.
711 * @member: the name of the hlist_node within the struct.
713 * This list-traversal primitive may safely run concurrently with
714 * the _rcu list-mutation primitives such as hlist_add_rcu()
715 * as long as the traversal is guarded by rcu_read_lock().
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; }) )
723 #endif