MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / include / linux / list.h
blob2846f2493042a1551340b1a610fd1bcfadaffb93
1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
4 #ifdef __KERNEL__
6 #include <linux/stddef.h>
7 #include <linux/prefetch.h>
8 #include <asm/system.h>
11 * These are non-NULL pointers that will result in page faults
12 * under normal circumstances, used to verify that nobody uses
13 * non-initialized list entries.
15 #define LIST_POISON1 ((void *) 0x00100100)
16 #define LIST_POISON2 ((void *) 0x00200200)
19 * Simple doubly linked list implementation.
21 * Some of the internal functions ("__xxx") are useful when
22 * manipulating whole lists rather than single entries, as
23 * sometimes we already know the next/prev entries and we can
24 * generate better code by using them directly rather than
25 * using the generic single-entry routines.
28 struct list_head {
29 struct list_head *next, *prev;
32 #define LIST_HEAD_INIT(name) { &(name), &(name) }
34 #define LIST_HEAD(name) \
35 struct list_head name = LIST_HEAD_INIT(name)
37 #define INIT_LIST_HEAD(ptr) do { \
38 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
39 } while (0)
42 * Insert a new entry between two known consecutive entries.
44 * This is only for internal list manipulation where we know
45 * the prev/next entries already!
47 static inline void __list_add(struct list_head *new,
48 struct list_head *prev,
49 struct list_head *next)
51 next->prev = new;
52 new->next = next;
53 new->prev = prev;
54 prev->next = new;
57 /**
58 * list_add - add a new entry
59 * @new: new entry to be added
60 * @head: list head to add it after
62 * Insert a new entry after the specified head.
63 * This is good for implementing stacks.
65 static inline void list_add(struct list_head *new, struct list_head *head)
67 __list_add(new, head, head->next);
70 /**
71 * list_add_tail - add a new entry
72 * @new: new entry to be added
73 * @head: list head to add it before
75 * Insert a new entry before the specified head.
76 * This is useful for implementing queues.
78 static inline void list_add_tail(struct list_head *new, struct list_head *head)
80 __list_add(new, head->prev, head);
84 * Insert a new entry between two known consecutive entries.
86 * This is only for internal list manipulation where we know
87 * the prev/next entries already!
89 static inline void __list_add_rcu(struct list_head * new,
90 struct list_head * prev, struct list_head * next)
92 new->next = next;
93 new->prev = prev;
94 smp_wmb();
95 next->prev = new;
96 prev->next = new;
99 /**
100 * list_add_rcu - add a new entry to rcu-protected list
101 * @new: new entry to be added
102 * @head: list head to add it after
104 * Insert a new entry after the specified head.
105 * This is good for implementing stacks.
107 * The caller must take whatever precautions are necessary
108 * (such as holding appropriate locks) to avoid racing
109 * with another list-mutation primitive, such as list_add_rcu()
110 * or list_del_rcu(), running on this same list.
111 * However, it is perfectly legal to run concurrently with
112 * the _rcu list-traversal primitives, such as
113 * list_for_each_entry_rcu().
115 static inline void list_add_rcu(struct list_head *new, struct list_head *head)
117 __list_add_rcu(new, head, head->next);
121 * list_add_tail_rcu - add a new entry to rcu-protected list
122 * @new: new entry to be added
123 * @head: list head to add it before
125 * Insert a new entry before the specified head.
126 * This is useful for implementing queues.
128 * The caller must take whatever precautions are necessary
129 * (such as holding appropriate locks) to avoid racing
130 * with another list-mutation primitive, such as list_add_tail_rcu()
131 * or list_del_rcu(), running on this same list.
132 * However, it is perfectly legal to run concurrently with
133 * the _rcu list-traversal primitives, such as
134 * list_for_each_entry_rcu().
136 static inline void list_add_tail_rcu(struct list_head *new,
137 struct list_head *head)
139 __list_add_rcu(new, head->prev, head);
143 * Delete a list entry by making the prev/next entries
144 * point to each other.
146 * This is only for internal list manipulation where we know
147 * the prev/next entries already!
149 static inline void __list_del(struct list_head * prev, struct list_head * next)
151 next->prev = prev;
152 prev->next = next;
156 * list_del - deletes entry from list.
157 * @entry: the element to delete from the list.
158 * Note: list_empty on entry does not return true after this, the entry is
159 * in an undefined state.
161 static inline void list_del(struct list_head *entry)
163 __list_del(entry->prev, entry->next);
164 entry->next = LIST_POISON1;
165 entry->prev = LIST_POISON2;
169 * list_del_rcu - deletes entry from list without re-initialization
170 * @entry: the element to delete from the list.
172 * Note: list_empty on entry does not return true after this,
173 * the entry is in an undefined state. It is useful for RCU based
174 * lockfree traversal.
176 * In particular, it means that we can not poison the forward
177 * pointers that may still be used for walking the list.
179 * The caller must take whatever precautions are necessary
180 * (such as holding appropriate locks) to avoid racing
181 * with another list-mutation primitive, such as list_del_rcu()
182 * or list_add_rcu(), running on this same list.
183 * However, it is perfectly legal to run concurrently with
184 * the _rcu list-traversal primitives, such as
185 * list_for_each_entry_rcu().
187 * Note that the caller is not permitted to immediately free
188 * the newly deleted entry. Instead, either synchronize_kernel()
189 * or call_rcu() must be used to defer freeing until an RCU
190 * grace period has elapsed.
192 static inline void list_del_rcu(struct list_head *entry)
194 __list_del(entry->prev, entry->next);
195 entry->prev = LIST_POISON2;
199 * list_replace_rcu - replace old entry by new one
200 * @old : the element to be replaced
201 * @new : the new element to insert
203 * The old entry will be replaced with the new entry atomically.
205 static inline void list_replace_rcu(struct list_head *old, struct list_head *new){
206 new->next = old->next;
207 new->prev = old->prev;
208 smp_wmb();
209 new->next->prev = new;
210 new->prev->next = new;
214 * list_del_init - deletes entry from list and reinitialize it.
215 * @entry: the element to delete from the list.
217 static inline void list_del_init(struct list_head *entry)
219 __list_del(entry->prev, entry->next);
220 INIT_LIST_HEAD(entry);
224 * list_move - delete from one list and add as another's head
225 * @list: the entry to move
226 * @head: the head that will precede our entry
228 static inline void list_move(struct list_head *list, struct list_head *head)
230 __list_del(list->prev, list->next);
231 list_add(list, head);
235 * list_move_tail - delete from one list and add as another's tail
236 * @list: the entry to move
237 * @head: the head that will follow our entry
239 static inline void list_move_tail(struct list_head *list,
240 struct list_head *head)
242 __list_del(list->prev, list->next);
243 list_add_tail(list, head);
247 * list_empty - tests whether a list is empty
248 * @head: the list to test.
250 static inline int list_empty(const struct list_head *head)
252 return head->next == head;
256 * list_empty_careful - tests whether a list is
257 * empty _and_ checks that no other CPU might be
258 * in the process of still modifying either member
260 * NOTE: using list_empty_careful() without synchronization
261 * can only be safe if the only activity that can happen
262 * to the list entry is list_del_init(). Eg. it cannot be used
263 * if another CPU could re-list_add() it.
265 * @head: the list to test.
267 static inline int list_empty_careful(const struct list_head *head)
269 struct list_head *next = head->next;
270 return (next == head) && (next == head->prev);
273 static inline void __list_splice(struct list_head *list,
274 struct list_head *head)
276 struct list_head *first = list->next;
277 struct list_head *last = list->prev;
278 struct list_head *at = head->next;
280 first->prev = head;
281 head->next = first;
283 last->next = at;
284 at->prev = last;
288 * list_splice - join two lists
289 * @list: the new list to add.
290 * @head: the place to add it in the first list.
292 static inline void list_splice(struct list_head *list, struct list_head *head)
294 if (!list_empty(list))
295 __list_splice(list, head);
299 * list_splice_init - join two lists and reinitialise the emptied list.
300 * @list: the new list to add.
301 * @head: the place to add it in the first list.
303 * The list at @list is reinitialised
305 static inline void list_splice_init(struct list_head *list,
306 struct list_head *head)
308 if (!list_empty(list)) {
309 __list_splice(list, head);
310 INIT_LIST_HEAD(list);
315 * list_entry - get the struct for this entry
316 * @ptr: the &struct list_head pointer.
317 * @type: the type of the struct this is embedded in.
318 * @member: the name of the list_struct within the struct.
320 #define list_entry(ptr, type, member) \
321 container_of(ptr, type, member)
324 * list_for_each - iterate over a list
325 * @pos: the &struct list_head to use as a loop counter.
326 * @head: the head for your list.
328 #define list_for_each(pos, head) \
329 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
330 pos = pos->next, prefetch(pos->next))
333 * __list_for_each - iterate over a list
334 * @pos: the &struct list_head to use as a loop counter.
335 * @head: the head for your list.
337 * This variant differs from list_for_each() in that it's the
338 * simplest possible list iteration code, no prefetching is done.
339 * Use this for code that knows the list to be very short (empty
340 * or 1 entry) most of the time.
342 #define __list_for_each(pos, head) \
343 for (pos = (head)->next; pos != (head); pos = pos->next)
346 * list_for_each_prev - iterate over a list backwards
347 * @pos: the &struct list_head to use as a loop counter.
348 * @head: the head for your list.
350 #define list_for_each_prev(pos, head) \
351 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
352 pos = pos->prev, prefetch(pos->prev))
355 * list_for_each_safe - iterate over a list safe against removal of list entry
356 * @pos: the &struct list_head to use as a loop counter.
357 * @n: another &struct list_head to use as temporary storage
358 * @head: the head for your list.
360 #define list_for_each_safe(pos, n, head) \
361 for (pos = (head)->next, n = pos->next; pos != (head); \
362 pos = n, n = pos->next)
365 * list_for_each_entry - iterate over list of given type
366 * @pos: the type * to use as a loop counter.
367 * @head: the head for your list.
368 * @member: the name of the list_struct within the struct.
370 #define list_for_each_entry(pos, head, member) \
371 for (pos = list_entry((head)->next, typeof(*pos), member), \
372 prefetch(pos->member.next); \
373 &pos->member != (head); \
374 pos = list_entry(pos->member.next, typeof(*pos), member), \
375 prefetch(pos->member.next))
378 * list_for_each_entry_reverse - iterate backwards over list of given type.
379 * @pos: the type * to use as a loop counter.
380 * @head: the head for your list.
381 * @member: the name of the list_struct within the struct.
383 #define list_for_each_entry_reverse(pos, head, member) \
384 for (pos = list_entry((head)->prev, typeof(*pos), member), \
385 prefetch(pos->member.prev); \
386 &pos->member != (head); \
387 pos = list_entry(pos->member.prev, typeof(*pos), member), \
388 prefetch(pos->member.prev))
391 * list_prepare_entry - prepare a pos entry for use as a start point in
392 * list_for_each_entry_continue
393 * @pos: the type * to use as a start point
394 * @head: the head of the list
395 * @member: the name of the list_struct within the struct.
397 #define list_prepare_entry(pos, head, member) \
398 ((pos) ? : list_entry(head, typeof(*pos), member))
401 * list_for_each_entry_continue - iterate over list of given type
402 * continuing after existing point
403 * @pos: the type * to use as a loop counter.
404 * @head: the head for your list.
405 * @member: the name of the list_struct within the struct.
407 #define list_for_each_entry_continue(pos, head, member) \
408 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
409 prefetch(pos->member.next); \
410 &pos->member != (head); \
411 pos = list_entry(pos->member.next, typeof(*pos), member), \
412 prefetch(pos->member.next))
415 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
416 * @pos: the type * to use as a loop counter.
417 * @n: another type * to use as temporary storage
418 * @head: the head for your list.
419 * @member: the name of the list_struct within the struct.
421 #define list_for_each_entry_safe(pos, n, head, member) \
422 for (pos = list_entry((head)->next, typeof(*pos), member), \
423 n = list_entry(pos->member.next, typeof(*pos), member); \
424 &pos->member != (head); \
425 pos = n, n = list_entry(n->member.next, typeof(*n), member))
428 * list_for_each_rcu - iterate over an rcu-protected list
429 * @pos: the &struct list_head to use as a loop counter.
430 * @head: the head for your list.
432 * This list-traversal primitive may safely run concurrently with
433 * the _rcu list-mutation primitives such as list_add_rcu()
434 * as long as the traversal is guarded by rcu_read_lock().
436 #define list_for_each_rcu(pos, head) \
437 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
438 pos = rcu_dereference(pos->next), prefetch(pos->next))
440 #define __list_for_each_rcu(pos, head) \
441 for (pos = (head)->next; pos != (head); \
442 pos = rcu_dereference(pos->next))
445 * list_for_each_safe_rcu - iterate over an rcu-protected list safe
446 * against removal of list entry
447 * @pos: the &struct list_head to use as a loop counter.
448 * @n: another &struct list_head to use as temporary storage
449 * @head: the head for your list.
451 * This list-traversal primitive may safely run concurrently with
452 * the _rcu list-mutation primitives such as list_add_rcu()
453 * as long as the traversal is guarded by rcu_read_lock().
455 #define list_for_each_safe_rcu(pos, n, head) \
456 for (pos = (head)->next, n = pos->next; pos != (head); \
457 pos = rcu_dereference(n), n = pos->next)
460 * list_for_each_entry_rcu - iterate over rcu list of given type
461 * @pos: the type * to use as a loop counter.
462 * @head: the head for your list.
463 * @member: the name of the list_struct within the struct.
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_entry_rcu(pos, head, member) \
470 for (pos = list_entry((head)->next, typeof(*pos), member), \
471 prefetch(pos->member.next); \
472 &pos->member != (head); \
473 pos = rcu_dereference(list_entry(pos->member.next, \
474 typeof(*pos), member)), \
475 prefetch(pos->member.next))
479 * list_for_each_continue_rcu - iterate over an rcu-protected list
480 * continuing after existing point.
481 * @pos: the &struct list_head to use as a loop counter.
482 * @head: the head for your list.
484 * This list-traversal primitive may safely run concurrently with
485 * the _rcu list-mutation primitives such as list_add_rcu()
486 * as long as the traversal is guarded by rcu_read_lock().
488 #define list_for_each_continue_rcu(pos, head) \
489 for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
490 (pos) = rcu_dereference((pos)->next), prefetch((pos)->next))
493 * Double linked lists with a single pointer list head.
494 * Mostly useful for hash tables where the two pointer list head is
495 * too wasteful.
496 * You lose the ability to access the tail in O(1).
499 struct hlist_head {
500 struct hlist_node *first;
503 struct hlist_node {
504 struct hlist_node *next, **pprev;
507 #define HLIST_HEAD_INIT { .first = NULL }
508 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
509 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
510 #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
512 static inline int hlist_unhashed(const struct hlist_node *h)
514 return !h->pprev;
517 static inline int hlist_empty(const struct hlist_head *h)
519 return !h->first;
522 static inline void __hlist_del(struct hlist_node *n)
524 struct hlist_node *next = n->next;
525 struct hlist_node **pprev = n->pprev;
526 *pprev = next;
527 if (next)
528 next->pprev = pprev;
531 static inline void hlist_del(struct hlist_node *n)
533 __hlist_del(n);
534 n->next = LIST_POISON1;
535 n->pprev = LIST_POISON2;
539 * hlist_del_rcu - deletes entry from hash list without re-initialization
540 * @n: the element to delete from the hash list.
542 * Note: list_unhashed() on entry does not return true after this,
543 * the entry is in an undefined state. It is useful for RCU based
544 * lockfree traversal.
546 * In particular, it means that we can not poison the forward
547 * pointers that may still be used for walking the hash list.
549 * The caller must take whatever precautions are necessary
550 * (such as holding appropriate locks) to avoid racing
551 * with another list-mutation primitive, such as hlist_add_head_rcu()
552 * or hlist_del_rcu(), running on this same list.
553 * However, it is perfectly legal to run concurrently with
554 * the _rcu list-traversal primitives, such as
555 * hlist_for_each_entry().
557 static inline void hlist_del_rcu(struct hlist_node *n)
559 __hlist_del(n);
560 n->pprev = LIST_POISON2;
563 static inline void hlist_del_init(struct hlist_node *n)
565 if (n->pprev) {
566 __hlist_del(n);
567 INIT_HLIST_NODE(n);
571 #define hlist_del_rcu_init hlist_del_init
573 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
575 struct hlist_node *first = h->first;
576 n->next = first;
577 if (first)
578 first->pprev = &n->next;
579 h->first = n;
580 n->pprev = &h->first;
585 * hlist_add_head_rcu - adds the specified element to the specified hlist,
586 * while permitting racing traversals.
587 * @n: the element to add to the hash list.
588 * @h: the list to add to.
590 * The caller must take whatever precautions are necessary
591 * (such as holding appropriate locks) to avoid racing
592 * with another list-mutation primitive, such as hlist_add_head_rcu()
593 * or hlist_del_rcu(), running on this same list.
594 * However, it is perfectly legal to run concurrently with
595 * the _rcu list-traversal primitives, such as
596 * hlist_for_each_rcu(), used to prevent memory-consistency
597 * problems on Alpha CPUs. Regardless of the type of CPU, the
598 * list-traversal primitive must be guarded by rcu_read_lock().
600 static inline void hlist_add_head_rcu(struct hlist_node *n,
601 struct hlist_head *h)
603 struct hlist_node *first = h->first;
604 n->next = first;
605 n->pprev = &h->first;
606 smp_wmb();
607 if (first)
608 first->pprev = &n->next;
609 h->first = n;
612 /* next must be != NULL */
613 static inline void hlist_add_before(struct hlist_node *n,
614 struct hlist_node *next)
616 n->pprev = next->pprev;
617 n->next = next;
618 next->pprev = &n->next;
619 *(n->pprev) = n;
622 static inline void hlist_add_after(struct hlist_node *n,
623 struct hlist_node *next)
625 next->next = n->next;
626 n->next = next;
627 next->pprev = &n->next;
629 if(next->next)
630 next->next->pprev = &next->next;
633 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
635 #define hlist_for_each(pos, head) \
636 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
637 pos = pos->next)
639 #define hlist_for_each_safe(pos, n, head) \
640 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
641 pos = n)
643 #define hlist_for_each_rcu(pos, head) \
644 for ((pos) = (head)->first; pos && ({ prefetch((pos)->next); 1; }); \
645 (pos) = rcu_dereference((pos)->next))
648 * hlist_for_each_entry - iterate over list of given type
649 * @tpos: the type * to use as a loop counter.
650 * @pos: the &struct hlist_node to use as a loop counter.
651 * @head: the head for your list.
652 * @member: the name of the hlist_node within the struct.
654 #define hlist_for_each_entry(tpos, pos, head, member) \
655 for (pos = (head)->first; \
656 pos && ({ prefetch(pos->next); 1;}) && \
657 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
658 pos = pos->next)
661 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
662 * @tpos: the type * to use as a loop counter.
663 * @pos: the &struct hlist_node to use as a loop counter.
664 * @member: the name of the hlist_node within the struct.
666 #define hlist_for_each_entry_continue(tpos, pos, member) \
667 for (pos = (pos)->next; \
668 pos && ({ prefetch(pos->next); 1;}) && \
669 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
670 pos = pos->next)
673 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
674 * @tpos: the type * to use as a loop counter.
675 * @pos: the &struct hlist_node to use as a loop counter.
676 * @member: the name of the hlist_node within the struct.
678 #define hlist_for_each_entry_from(tpos, pos, member) \
679 for (; pos && ({ prefetch(pos->next); 1;}) && \
680 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
681 pos = pos->next)
684 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
685 * @tpos: the type * to use as a loop counter.
686 * @pos: the &struct hlist_node to use as a loop counter.
687 * @n: another &struct hlist_node to use as temporary storage
688 * @head: the head for your list.
689 * @member: the name of the hlist_node within the struct.
691 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
692 for (pos = (head)->first; \
693 pos && ({ n = pos->next; 1; }) && \
694 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
695 pos = n)
698 * hlist_for_each_entry_rcu - iterate over rcu list of given type
699 * @pos: the type * to use as a loop counter.
700 * @pos: the &struct hlist_node to use as a loop counter.
701 * @head: the head for your list.
702 * @member: the name of the hlist_node within the struct.
704 * This list-traversal primitive may safely run concurrently with
705 * the _rcu list-mutation primitives such as hlist_add_rcu()
706 * as long as the traversal is guarded by rcu_read_lock().
708 #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
709 for (pos = (head)->first; \
710 pos && ({ prefetch(pos->next); 1;}) && \
711 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
712 pos = rcu_dereference(pos->next))
714 #else
715 //#warning "don't include kernel headers in userspace"
716 #endif /* __KERNEL__ */
717 #endif