]> git.0d.be Git - jack_mixer.git/blob - list.h
Renamed classes to conform to standard Python coding style (PEP-8)
[jack_mixer.git] / list.h
1 /* -*- Mode: C ; c-basic-offset: 2 -*- */
2 /*****************************************************************************
3  *
4  *   Linux kernel header adapted for user-mode
5  *   The 2.6.17-rt1 version was used.
6  *
7  *   Original copyright holders of this code are unknown, they were not
8  *   mentioned in the original file.
9  *    
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of the GNU General Public License as published by
12  *   the Free Software Foundation; version 2 of the License
13  *
14  *   This program is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with this program; if not, write to the Free Software
21  *   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  *****************************************************************************/
24
25 #ifndef _LINUX_LIST_H
26 #define _LINUX_LIST_H
27
28 #include <stddef.h>
29
30 #if !defined(offsetof)
31 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
32 #endif
33
34 /**
35  * container_of - cast a member of a structure out to the containing structure
36  * @ptr:        the pointer to the member.
37  * @type:       the type of the container struct this is embedded in.
38  * @member:     the name of the member within the struct.
39  *
40  */
41 #define container_of(ptr, type, member) ({                      \
42         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
43         (type *)( (char *)__mptr - offsetof(type,member) );})
44
45 #define prefetch(x) (x = x)
46
47 /*
48  * These are non-NULL pointers that will result in page faults
49  * under normal circumstances, used to verify that nobody uses
50  * non-initialized list entries.
51  */
52 #define LIST_POISON1  ((void *) 0x00100100)
53 #define LIST_POISON2  ((void *) 0x00200200)
54
55 /*
56  * Simple doubly linked list implementation.
57  *
58  * Some of the internal functions ("__xxx") are useful when
59  * manipulating whole lists rather than single entries, as
60  * sometimes we already know the next/prev entries and we can
61  * generate better code by using them directly rather than
62  * using the generic single-entry routines.
63  */
64
65 struct list_head {
66   struct list_head *next, *prev;
67 };
68
69 #define LIST_HEAD_INIT(name) { &(name), &(name) }
70
71 #define LIST_HEAD(name) \
72   struct list_head name = LIST_HEAD_INIT(name)
73
74 static inline void INIT_LIST_HEAD(struct list_head *list)
75 {
76   list->next = list;
77   list->prev = list;
78 }
79
80 /*
81  * Insert a new entry between two known consecutive entries.
82  *
83  * This is only for internal list manipulation where we know
84  * the prev/next entries already!
85  */
86 static inline void __list_add(struct list_head *new,
87             struct list_head *prev,
88             struct list_head *next)
89 {
90   next->prev = new;
91   new->next = next;
92   new->prev = prev;
93   prev->next = new;
94 }
95
96 /**
97  * list_add - add a new entry
98  * @new: new entry to be added
99  * @head: list head to add it after
100  *
101  * Insert a new entry after the specified head.
102  * This is good for implementing stacks.
103  */
104 static inline void list_add(struct list_head *new, struct list_head *head)
105 {
106   __list_add(new, head, head->next);
107 }
108
109 /**
110  * list_add_tail - add a new entry
111  * @new: new entry to be added
112  * @head: list head to add it before
113  *
114  * Insert a new entry before the specified head.
115  * This is useful for implementing queues.
116  */
117 static inline void list_add_tail(struct list_head *new, struct list_head *head)
118 {
119   __list_add(new, head->prev, head);
120 }
121
122 /*
123  * Insert a new entry between two known consecutive entries.
124  *
125  * This is only for internal list manipulation where we know
126  * the prev/next entries already!
127  */
128 static inline void __list_add_rcu(struct list_head * new,
129     struct list_head * prev, struct list_head * next)
130 {
131   new->next = next;
132   new->prev = prev;
133 //  smp_wmb();
134   next->prev = new;
135   prev->next = new;
136 }
137
138 /**
139  * list_add_rcu - add a new entry to rcu-protected list
140  * @new: new entry to be added
141  * @head: list head to add it after
142  *
143  * Insert a new entry after the specified head.
144  * This is good for implementing stacks.
145  *
146  * The caller must take whatever precautions are necessary
147  * (such as holding appropriate locks) to avoid racing
148  * with another list-mutation primitive, such as list_add_rcu()
149  * or list_del_rcu(), running on this same list.
150  * However, it is perfectly legal to run concurrently with
151  * the _rcu list-traversal primitives, such as
152  * list_for_each_entry_rcu().
153  */
154 static inline void list_add_rcu(struct list_head *new, struct list_head *head)
155 {
156   __list_add_rcu(new, head, head->next);
157 }
158
159 /**
160  * list_add_tail_rcu - add a new entry to rcu-protected list
161  * @new: new entry to be added
162  * @head: list head to add it before
163  *
164  * Insert a new entry before the specified head.
165  * This is useful for implementing queues.
166  *
167  * The caller must take whatever precautions are necessary
168  * (such as holding appropriate locks) to avoid racing
169  * with another list-mutation primitive, such as list_add_tail_rcu()
170  * or list_del_rcu(), running on this same list.
171  * However, it is perfectly legal to run concurrently with
172  * the _rcu list-traversal primitives, such as
173  * list_for_each_entry_rcu().
174  */
175 static inline void list_add_tail_rcu(struct list_head *new,
176           struct list_head *head)
177 {
178   __list_add_rcu(new, head->prev, head);
179 }
180
181 /*
182  * Delete a list entry by making the prev/next entries
183  * point to each other.
184  *
185  * This is only for internal list manipulation where we know
186  * the prev/next entries already!
187  */
188 static inline void __list_del(struct list_head * prev, struct list_head * next)
189 {
190   next->prev = prev;
191   prev->next = next;
192 }
193
194 /**
195  * list_del - deletes entry from list.
196  * @entry: the element to delete from the list.
197  * Note: list_empty on entry does not return true after this, the entry is
198  * in an undefined state.
199  */
200 static inline void list_del(struct list_head *entry)
201 {
202   __list_del(entry->prev, entry->next);
203   entry->next = LIST_POISON1;
204   entry->prev = LIST_POISON2;
205 }
206
207 /**
208  * list_del_rcu - deletes entry from list without re-initialization
209  * @entry: the element to delete from the list.
210  *
211  * Note: list_empty on entry does not return true after this,
212  * the entry is in an undefined state. It is useful for RCU based
213  * lockfree traversal.
214  *
215  * In particular, it means that we can not poison the forward
216  * pointers that may still be used for walking the list.
217  *
218  * The caller must take whatever precautions are necessary
219  * (such as holding appropriate locks) to avoid racing
220  * with another list-mutation primitive, such as list_del_rcu()
221  * or list_add_rcu(), running on this same list.
222  * However, it is perfectly legal to run concurrently with
223  * the _rcu list-traversal primitives, such as
224  * list_for_each_entry_rcu().
225  *
226  * Note that the caller is not permitted to immediately free
227  * the newly deleted entry.  Instead, either synchronize_rcu()
228  * or call_rcu() must be used to defer freeing until an RCU
229  * grace period has elapsed.
230  */
231 static inline void list_del_rcu(struct list_head *entry)
232 {
233   __list_del(entry->prev, entry->next);
234   entry->prev = LIST_POISON2;
235 }
236
237 /*
238  * list_replace_rcu - replace old entry by new one
239  * @old : the element to be replaced
240  * @new : the new element to insert
241  *
242  * The old entry will be replaced with the new entry atomically.
243  */
244 static inline void list_replace_rcu(struct list_head *old,
245         struct list_head *new)
246 {
247   new->next = old->next;
248   new->prev = old->prev;
249 //  smp_wmb();
250   new->next->prev = new;
251   new->prev->next = new;
252   old->prev = LIST_POISON2;
253 }
254
255 /**
256  * list_del_init - deletes entry from list and reinitialize it.
257  * @entry: the element to delete from the list.
258  */
259 static inline void list_del_init(struct list_head *entry)
260 {
261   __list_del(entry->prev, entry->next);
262   INIT_LIST_HEAD(entry);
263 }
264
265 /**
266  * list_move - delete from one list and add as another's head
267  * @list: the entry to move
268  * @head: the head that will precede our entry
269  */
270 static inline void list_move(struct list_head *list, struct list_head *head)
271 {
272         __list_del(list->prev, list->next);
273         list_add(list, head);
274 }
275
276 /**
277  * list_move_tail - delete from one list and add as another's tail
278  * @list: the entry to move
279  * @head: the head that will follow our entry
280  */
281 static inline void list_move_tail(struct list_head *list,
282           struct list_head *head)
283 {
284         __list_del(list->prev, list->next);
285         list_add_tail(list, head);
286 }
287
288 /**
289  * list_empty - tests whether a list is empty
290  * @head: the list to test.
291  */
292 static inline int list_empty(const struct list_head *head)
293 {
294   return head->next == head;
295 }
296
297 /**
298  * list_empty_careful - tests whether a list is
299  * empty _and_ checks that no other CPU might be
300  * in the process of still modifying either member
301  *
302  * NOTE: using list_empty_careful() without synchronization
303  * can only be safe if the only activity that can happen
304  * to the list entry is list_del_init(). Eg. it cannot be used
305  * if another CPU could re-list_add() it.
306  *
307  * @head: the list to test.
308  */
309 static inline int list_empty_careful(const struct list_head *head)
310 {
311   struct list_head *next = head->next;
312   return (next == head) && (next == head->prev);
313 }
314
315 static inline void __list_splice(struct list_head *list,
316          struct list_head *head)
317 {
318   struct list_head *first = list->next;
319   struct list_head *last = list->prev;
320   struct list_head *at = head->next;
321
322   first->prev = head;
323   head->next = first;
324
325   last->next = at;
326   at->prev = last;
327 }
328
329 /**
330  * list_splice - join two lists
331  * @list: the new list to add.
332  * @head: the place to add it in the first list.
333  */
334 static inline void list_splice(struct list_head *list, struct list_head *head)
335 {
336   if (!list_empty(list))
337     __list_splice(list, head);
338 }
339
340 /**
341  * list_splice_init - join two lists and reinitialise the emptied list.
342  * @list: the new list to add.
343  * @head: the place to add it in the first list.
344  *
345  * The list at @list is reinitialised
346  */
347 static inline void list_splice_init(struct list_head *list,
348             struct list_head *head)
349 {
350   if (!list_empty(list)) {
351     __list_splice(list, head);
352     INIT_LIST_HEAD(list);
353   }
354 }
355
356 /**
357  * list_entry - get the struct for this entry
358  * @ptr:  the &struct list_head pointer.
359  * @type: the type of the struct this is embedded in.
360  * @member: the name of the list_struct within the struct.
361  */
362 #define list_entry(ptr, type, member) \
363   container_of(ptr, type, member)
364
365 /**
366  * list_for_each  - iterate over a list
367  * @pos:  the &struct list_head to use as a loop counter.
368  * @head: the head for your list.
369  */
370 #define list_for_each(pos, head) \
371   for (pos = (head)->next; prefetch(pos->next), pos != (head); \
372           pos = pos->next)
373
374 /**
375  * __list_for_each  - iterate over a list
376  * @pos:  the &struct list_head to use as a loop counter.
377  * @head: the head for your list.
378  *
379  * This variant differs from list_for_each() in that it's the
380  * simplest possible list iteration code, no prefetching is done.
381  * Use this for code that knows the list to be very short (empty
382  * or 1 entry) most of the time.
383  */
384 #define __list_for_each(pos, head) \
385   for (pos = (head)->next; pos != (head); pos = pos->next)
386
387 /**
388  * list_for_each_prev - iterate over a list backwards
389  * @pos:  the &struct list_head to use as a loop counter.
390  * @head: the head for your list.
391  */
392 #define list_for_each_prev(pos, head) \
393   for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
394           pos = pos->prev)
395
396 /**
397  * list_for_each_safe - iterate over a list safe against removal of list entry
398  * @pos:  the &struct list_head to use as a loop counter.
399  * @n:    another &struct list_head to use as temporary storage
400  * @head: the head for your list.
401  */
402 #define list_for_each_safe(pos, n, head) \
403   for (pos = (head)->next, n = pos->next; pos != (head); \
404     pos = n, n = pos->next)
405
406 /**
407  * list_for_each_entry  - iterate over list of given type
408  * @pos:  the type * to use as a loop counter.
409  * @head: the head for your list.
410  * @member: the name of the list_struct within the struct.
411  */
412 #define list_for_each_entry(pos, head, member)        \
413   for (pos = list_entry((head)->next, typeof(*pos), member);  \
414        prefetch(pos->member.next), &pos->member != (head);  \
415        pos = list_entry(pos->member.next, typeof(*pos), member))
416
417 /**
418  * list_for_each_entry_reverse - iterate backwards over list of given type.
419  * @pos:  the type * to use as a loop counter.
420  * @head: the head for your list.
421  * @member: the name of the list_struct within the struct.
422  */
423 #define list_for_each_entry_reverse(pos, head, member)      \
424   for (pos = list_entry((head)->prev, typeof(*pos), member);  \
425        prefetch(pos->member.prev), &pos->member != (head);  \
426        pos = list_entry(pos->member.prev, typeof(*pos), member))
427
428 /**
429  * list_prepare_entry - prepare a pos entry for use as a start point in
430  *      list_for_each_entry_continue
431  * @pos:  the type * to use as a start point
432  * @head: the head of the list
433  * @member: the name of the list_struct within the struct.
434  */
435 #define list_prepare_entry(pos, head, member) \
436   ((pos) ? : list_entry(head, typeof(*pos), member))
437
438 /**
439  * list_for_each_entry_continue - iterate over list of given type
440  *      continuing after existing point
441  * @pos:  the type * to use as a loop counter.
442  * @head: the head for your list.
443  * @member: the name of the list_struct within the struct.
444  */
445 #define list_for_each_entry_continue(pos, head, member)     \
446   for (pos = list_entry(pos->member.next, typeof(*pos), member);  \
447        prefetch(pos->member.next), &pos->member != (head);  \
448        pos = list_entry(pos->member.next, typeof(*pos), member))
449
450 /**
451  * list_for_each_entry_from - iterate over list of given type
452  *      continuing from existing point
453  * @pos:  the type * to use as a loop counter.
454  * @head: the head for your list.
455  * @member: the name of the list_struct within the struct.
456  */
457 #define list_for_each_entry_from(pos, head, member)       \
458   for (; prefetch(pos->member.next), &pos->member != (head);  \
459        pos = list_entry(pos->member.next, typeof(*pos), member))
460
461 /**
462  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
463  * @pos:  the type * to use as a loop counter.
464  * @n:    another type * to use as temporary storage
465  * @head: the head for your list.
466  * @member: the name of the list_struct within the struct.
467  */
468 #define list_for_each_entry_safe(pos, n, head, member)      \
469   for (pos = list_entry((head)->next, typeof(*pos), member),  \
470     n = list_entry(pos->member.next, typeof(*pos), member); \
471        &pos->member != (head);          \
472        pos = n, n = list_entry(n->member.next, typeof(*n), member))
473
474 /**
475  * list_for_each_entry_safe_continue -  iterate over list of given type
476  *      continuing after existing point safe against removal of list entry
477  * @pos:  the type * to use as a loop counter.
478  * @n:    another type * to use as temporary storage
479  * @head: the head for your list.
480  * @member: the name of the list_struct within the struct.
481  */
482 #define list_for_each_entry_safe_continue(pos, n, head, member)     \
483   for (pos = list_entry(pos->member.next, typeof(*pos), member),    \
484     n = list_entry(pos->member.next, typeof(*pos), member);   \
485        &pos->member != (head);            \
486        pos = n, n = list_entry(n->member.next, typeof(*n), member))
487
488 /**
489  * list_for_each_entry_safe_from - iterate over list of given type
490  *      from existing point safe against removal of list entry
491  * @pos:  the type * to use as a loop counter.
492  * @n:    another type * to use as temporary storage
493  * @head: the head for your list.
494  * @member: the name of the list_struct within the struct.
495  */
496 #define list_for_each_entry_safe_from(pos, n, head, member)       \
497   for (n = list_entry(pos->member.next, typeof(*pos), member);    \
498        &pos->member != (head);            \
499        pos = n, n = list_entry(n->member.next, typeof(*n), member))
500
501 /**
502  * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
503  *              removal of list entry
504  * @pos:  the type * to use as a loop counter.
505  * @n:    another type * to use as temporary storage
506  * @head: the head for your list.
507  * @member: the name of the list_struct within the struct.
508  */
509 #define list_for_each_entry_safe_reverse(pos, n, head, member)    \
510   for (pos = list_entry((head)->prev, typeof(*pos), member),  \
511     n = list_entry(pos->member.prev, typeof(*pos), member); \
512        &pos->member != (head);          \
513        pos = n, n = list_entry(n->member.prev, typeof(*n), member))
514
515 /**
516  * list_for_each_rcu  - iterate over an rcu-protected list
517  * @pos:  the &struct list_head to use as a loop counter.
518  * @head: the head for your list.
519  *
520  * This list-traversal primitive may safely run concurrently with
521  * the _rcu list-mutation primitives such as list_add_rcu()
522  * as long as the traversal is guarded by rcu_read_lock().
523  */
524 #define list_for_each_rcu(pos, head) \
525   for (pos = (head)->next; \
526     prefetch(rcu_dereference(pos)->next), pos != (head); \
527           pos = pos->next)
528
529 #define __list_for_each_rcu(pos, head) \
530   for (pos = (head)->next; \
531     rcu_dereference(pos) != (head); \
532           pos = pos->next)
533
534 /**
535  * list_for_each_safe_rcu - iterate over an rcu-protected list safe
536  *          against removal of list entry
537  * @pos:  the &struct list_head to use as a loop counter.
538  * @n:    another &struct list_head to use as temporary storage
539  * @head: the head for your list.
540  *
541  * This list-traversal primitive may safely run concurrently with
542  * the _rcu list-mutation primitives such as list_add_rcu()
543  * as long as the traversal is guarded by rcu_read_lock().
544  */
545 #define list_for_each_safe_rcu(pos, n, head) \
546   for (pos = (head)->next; \
547     n = rcu_dereference(pos)->next, pos != (head); \
548     pos = n)
549
550 /**
551  * list_for_each_entry_rcu  - iterate over rcu list of given type
552  * @pos:  the type * to use as a loop counter.
553  * @head: the head for your list.
554  * @member: the name of the list_struct within the struct.
555  *
556  * This list-traversal primitive may safely run concurrently with
557  * the _rcu list-mutation primitives such as list_add_rcu()
558  * as long as the traversal is guarded by rcu_read_lock().
559  */
560 #define list_for_each_entry_rcu(pos, head, member) \
561   for (pos = list_entry((head)->next, typeof(*pos), member); \
562     prefetch(rcu_dereference(pos)->member.next), \
563       &pos->member != (head); \
564     pos = list_entry(pos->member.next, typeof(*pos), member))
565
566
567 /**
568  * list_for_each_continue_rcu - iterate over an rcu-protected list
569  *      continuing after existing point.
570  * @pos:  the &struct list_head to use as a loop counter.
571  * @head: the head for your list.
572  *
573  * This list-traversal primitive may safely run concurrently with
574  * the _rcu list-mutation primitives such as list_add_rcu()
575  * as long as the traversal is guarded by rcu_read_lock().
576  */
577 #define list_for_each_continue_rcu(pos, head) \
578   for ((pos) = (pos)->next; \
579     prefetch(rcu_dereference((pos))->next), (pos) != (head); \
580           (pos) = (pos)->next)
581
582 /*
583  * Double linked lists with a single pointer list head.
584  * Mostly useful for hash tables where the two pointer list head is
585  * too wasteful.
586  * You lose the ability to access the tail in O(1).
587  */
588
589 struct hlist_head {
590   struct hlist_node *first;
591 };
592
593 struct hlist_node {
594   struct hlist_node *next, **pprev;
595 };
596
597 #define HLIST_HEAD_INIT { .first = NULL }
598 #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
599 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
600 static inline void INIT_HLIST_NODE(struct hlist_node *h)
601 {
602   h->next = NULL;
603   h->pprev = NULL;
604 }
605
606 static inline int hlist_unhashed(const struct hlist_node *h)
607 {
608   return !h->pprev;
609 }
610
611 static inline int hlist_empty(const struct hlist_head *h)
612 {
613   return !h->first;
614 }
615
616 static inline void __hlist_del(struct hlist_node *n)
617 {
618   struct hlist_node *next = n->next;
619   struct hlist_node **pprev = n->pprev;
620   *pprev = next;
621   if (next)
622     next->pprev = pprev;
623 }
624
625 static inline void hlist_del(struct hlist_node *n)
626 {
627   __hlist_del(n);
628   n->next = LIST_POISON1;
629   n->pprev = LIST_POISON2;
630 }
631
632 /**
633  * hlist_del_rcu - deletes entry from hash list without re-initialization
634  * @n: the element to delete from the hash list.
635  *
636  * Note: list_unhashed() on entry does not return true after this,
637  * the entry is in an undefined state. It is useful for RCU based
638  * lockfree traversal.
639  *
640  * In particular, it means that we can not poison the forward
641  * pointers that may still be used for walking the hash list.
642  *
643  * The caller must take whatever precautions are necessary
644  * (such as holding appropriate locks) to avoid racing
645  * with another list-mutation primitive, such as hlist_add_head_rcu()
646  * or hlist_del_rcu(), running on this same list.
647  * However, it is perfectly legal to run concurrently with
648  * the _rcu list-traversal primitives, such as
649  * hlist_for_each_entry().
650  */
651 static inline void hlist_del_rcu(struct hlist_node *n)
652 {
653   __hlist_del(n);
654   n->pprev = LIST_POISON2;
655 }
656
657 static inline void hlist_del_init(struct hlist_node *n)
658 {
659   if (!hlist_unhashed(n)) {
660     __hlist_del(n);
661     INIT_HLIST_NODE(n);
662   }
663 }
664
665 /*
666  * hlist_replace_rcu - replace old entry by new one
667  * @old : the element to be replaced
668  * @new : the new element to insert
669  *
670  * The old entry will be replaced with the new entry atomically.
671  */
672 static inline void hlist_replace_rcu(struct hlist_node *old,
673           struct hlist_node *new)
674 {
675   struct hlist_node *next = old->next;
676
677   new->next = next;
678   new->pprev = old->pprev;
679 //  smp_wmb();
680   if (next)
681     new->next->pprev = &new->next;
682   *new->pprev = new;
683   old->pprev = LIST_POISON2;
684 }
685
686 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
687 {
688   struct hlist_node *first = h->first;
689   n->next = first;
690   if (first)
691     first->pprev = &n->next;
692   h->first = n;
693   n->pprev = &h->first;
694 }
695
696
697 /**
698  * hlist_add_head_rcu - adds the specified element to the specified hlist,
699  * while permitting racing traversals.
700  * @n: the element to add to the hash list.
701  * @h: the list to add to.
702  *
703  * The caller must take whatever precautions are necessary
704  * (such as holding appropriate locks) to avoid racing
705  * with another list-mutation primitive, such as hlist_add_head_rcu()
706  * or hlist_del_rcu(), running on this same list.
707  * However, it is perfectly legal to run concurrently with
708  * the _rcu list-traversal primitives, such as
709  * hlist_for_each_entry_rcu(), used to prevent memory-consistency
710  * problems on Alpha CPUs.  Regardless of the type of CPU, the
711  * list-traversal primitive must be guarded by rcu_read_lock().
712  */
713 static inline void hlist_add_head_rcu(struct hlist_node *n,
714           struct hlist_head *h)
715 {
716   struct hlist_node *first = h->first;
717   n->next = first;
718   n->pprev = &h->first;
719 //  smp_wmb();
720   if (first)
721     first->pprev = &n->next;
722   h->first = n;
723 }
724
725 /* next must be != NULL */
726 static inline void hlist_add_before(struct hlist_node *n,
727           struct hlist_node *next)
728 {
729   n->pprev = next->pprev;
730   n->next = next;
731   next->pprev = &n->next;
732   *(n->pprev) = n;
733 }
734
735 static inline void hlist_add_after(struct hlist_node *n,
736           struct hlist_node *next)
737 {
738   next->next = n->next;
739   n->next = next;
740   next->pprev = &n->next;
741
742   if(next->next)
743     next->next->pprev  = &next->next;
744 }
745
746 /**
747  * hlist_add_before_rcu - adds the specified element to the specified hlist
748  * before the specified node while permitting racing traversals.
749  * @n: the new element to add to the hash list.
750  * @next: the existing element to add the new element before.
751  *
752  * The caller must take whatever precautions are necessary
753  * (such as holding appropriate locks) to avoid racing
754  * with another list-mutation primitive, such as hlist_add_head_rcu()
755  * or hlist_del_rcu(), running on this same list.
756  * However, it is perfectly legal to run concurrently with
757  * the _rcu list-traversal primitives, such as
758  * hlist_for_each_entry_rcu(), used to prevent memory-consistency
759  * problems on Alpha CPUs.
760  */
761 static inline void hlist_add_before_rcu(struct hlist_node *n,
762           struct hlist_node *next)
763 {
764   n->pprev = next->pprev;
765   n->next = next;
766 //  smp_wmb();
767   next->pprev = &n->next;
768   *(n->pprev) = n;
769 }
770
771 /**
772  * hlist_add_after_rcu - adds the specified element to the specified hlist
773  * after the specified node while permitting racing traversals.
774  * @prev: the existing element to add the new element after.
775  * @n: the new element to add to the hash list.
776  *
777  * The caller must take whatever precautions are necessary
778  * (such as holding appropriate locks) to avoid racing
779  * with another list-mutation primitive, such as hlist_add_head_rcu()
780  * or hlist_del_rcu(), running on this same list.
781  * However, it is perfectly legal to run concurrently with
782  * the _rcu list-traversal primitives, such as
783  * hlist_for_each_entry_rcu(), used to prevent memory-consistency
784  * problems on Alpha CPUs.
785  */
786 static inline void hlist_add_after_rcu(struct hlist_node *prev,
787                struct hlist_node *n)
788 {
789   n->next = prev->next;
790   n->pprev = &prev->next;
791 //  smp_wmb();
792   prev->next = n;
793   if (n->next)
794     n->next->pprev = &n->next;
795 }
796
797 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
798
799 #define hlist_for_each(pos, head) \
800   for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
801        pos = pos->next)
802
803 #define hlist_for_each_safe(pos, n, head) \
804   for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
805        pos = n)
806
807 /**
808  * hlist_for_each_entry - iterate over list of given type
809  * @tpos: the type * to use as a loop counter.
810  * @pos:  the &struct hlist_node to use as a loop counter.
811  * @head: the head for your list.
812  * @member: the name of the hlist_node within the struct.
813  */
814 #define hlist_for_each_entry(tpos, pos, head, member)      \
815   for (pos = (head)->first;          \
816        pos && ({ prefetch(pos->next); 1;}) &&      \
817     ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
818        pos = pos->next)
819
820 /**
821  * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
822  * @tpos: the type * to use as a loop counter.
823  * @pos:  the &struct hlist_node to use as a loop counter.
824  * @member: the name of the hlist_node within the struct.
825  */
826 #define hlist_for_each_entry_continue(tpos, pos, member)     \
827   for (pos = (pos)->next;            \
828        pos && ({ prefetch(pos->next); 1;}) &&      \
829     ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
830        pos = pos->next)
831
832 /**
833  * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
834  * @tpos: the type * to use as a loop counter.
835  * @pos:  the &struct hlist_node to use as a loop counter.
836  * @member: the name of the hlist_node within the struct.
837  */
838 #define hlist_for_each_entry_from(tpos, pos, member)       \
839   for (; pos && ({ prefetch(pos->next); 1;}) &&      \
840     ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
841        pos = pos->next)
842
843 /**
844  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
845  * @tpos: the type * to use as a loop counter.
846  * @pos:  the &struct hlist_node to use as a loop counter.
847  * @n:    another &struct hlist_node to use as temporary storage
848  * @head: the head for your list.
849  * @member: the name of the hlist_node within the struct.
850  */
851 #define hlist_for_each_entry_safe(tpos, pos, n, head, member)      \
852   for (pos = (head)->first;          \
853        pos && ({ n = pos->next; 1; }) &&         \
854     ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
855        pos = n)
856
857 /**
858  * hlist_for_each_entry_rcu - iterate over rcu list of given type
859  * @tpos: the type * to use as a loop counter.
860  * @pos:  the &struct hlist_node to use as a loop counter.
861  * @head: the head for your list.
862  * @member: the name of the hlist_node within the struct.
863  *
864  * This list-traversal primitive may safely run concurrently with
865  * the _rcu list-mutation primitives such as hlist_add_head_rcu()
866  * as long as the traversal is guarded by rcu_read_lock().
867  */
868 #define hlist_for_each_entry_rcu(tpos, pos, head, member)    \
869   for (pos = (head)->first;          \
870        rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) &&   \
871     ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
872        pos = pos->next)
873
874 #endif