Whamcloud - gitweb
27de832647e4af6517d65185bbb1daa0a749d086
[fs/lustre-release.git] / libcfs / include / libcfs / list.h
1 #ifndef __LIBCFS_LIST_H__
2 #define __LIBCFS_LIST_H__
3
4 #if defined (__linux__) && defined(__KERNEL__)
5
6 #include <linux/list.h>
7
8 typedef struct list_head cfs_list_t;
9
10 #define __cfs_list_add(new, prev, next)      __list_add(new, prev, next)
11 #define cfs_list_add(new, head)              list_add(new, head)
12
13 #define cfs_list_add_tail(new, head)         list_add_tail(new, head)
14
15 #define __cfs_list_del(prev, next)           __list_del(prev, next)
16 #define cfs_list_del(entry)                  list_del(entry)
17 #define cfs_list_del_init(entry)             list_del_init(entry)
18
19 #define cfs_list_move(list, head)            list_move(list, head)
20 #define cfs_list_move_tail(list, head)       list_move_tail(list, head)
21
22 #define cfs_list_empty(head)                 list_empty(head)
23 #define cfs_list_empty_careful(head)         list_empty_careful(head)
24
25 #define __cfs_list_splice(list, head)        __list_splice(list, head)
26 #define cfs_list_splice(list, head)          list_splice(list, head)
27
28 #define cfs_list_splice_init(list, head)     list_splice_init(list, head)
29
30 #define cfs_list_entry(ptr, type, member)    list_entry(ptr, type, member)
31 #define cfs_list_for_each(pos, head)         list_for_each(pos, head)
32 #define cfs_list_for_each_safe(pos, n, head) list_for_each_safe(pos, n, head)
33 #define cfs_list_for_each_prev(pos, head)    list_for_each_prev(pos, head)
34 #define cfs_list_for_each_entry(pos, head, member) \
35         list_for_each_entry(pos, head, member)
36 #define cfs_list_for_each_entry_reverse(pos, head, member) \
37         list_for_each_entry_reverse(pos, head, member)
38 #define cfs_list_for_each_entry_safe_reverse(pos, n, head, member) \
39         list_for_each_entry_safe_reverse(pos, n, head, member)
40 #define cfs_list_for_each_entry_safe(pos, n, head, member) \
41         list_for_each_entry_safe(pos, n, head, member)
42 #ifdef list_for_each_entry_safe_from
43 #define cfs_list_for_each_entry_safe_from(pos, n, head, member) \
44         list_for_each_entry_safe_from(pos, n, head, member)
45 #endif /* list_for_each_entry_safe_from */
46 #define cfs_list_for_each_entry_continue(pos, head, member) \
47         list_for_each_entry_continue(pos, head, member)
48
49 #define CFS_LIST_HEAD_INIT(n)                LIST_HEAD_INIT(n)
50 #define CFS_LIST_HEAD(n)                     LIST_HEAD(n)
51 #define CFS_INIT_LIST_HEAD(p)                INIT_LIST_HEAD(p)
52
53 typedef struct hlist_head cfs_hlist_head_t;
54 typedef struct hlist_node cfs_hlist_node_t;
55
56 #define cfs_hlist_unhashed(h)              hlist_unhashed(h)
57
58 #define cfs_hlist_empty(h)                 hlist_empty(h)
59
60 #define __cfs_hlist_del(n)                 __hlist_del(n)
61 #define cfs_hlist_del(n)                   hlist_del(n)
62 #define cfs_hlist_del_init(n)              hlist_del_init(n)
63
64 #define cfs_hlist_add_head(n, next)        hlist_add_head(n, next)
65 #define cfs_hlist_add_before(n, next)      hlist_add_before(n, next)
66 #define cfs_hlist_add_after(n, next)       hlist_add_after(n, next)
67
68 #define cfs_hlist_entry(ptr, type, member) hlist_entry(ptr, type, member)
69 #define cfs_hlist_for_each(pos, head)      hlist_for_each(pos, head)
70 #define cfs_hlist_for_each_safe(pos, n, head) \
71         hlist_for_each_safe(pos, n, head)
72 #define cfs_hlist_for_each_entry(tpos, pos, head, member) \
73         hlist_for_each_entry(tpos, pos, head, member)
74 #define cfs_hlist_for_each_entry_continue(tpos, pos, member) \
75         hlist_for_each_entry_continue(tpos, pos, member)
76 #define cfs_hlist_for_each_entry_from(tpos, pos, member) \
77         hlist_for_each_entry_from(tpos, pos, member)
78 #define cfs_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
79         hlist_for_each_entry_safe(tpos, pos, n, head, member)
80
81 #define CFS_HLIST_HEAD_INIT                HLIST_HEAD_INIT
82 #define CFS_HLIST_HEAD(n)                  HLIST_HEAD(n)
83 #define CFS_INIT_HLIST_HEAD(p)             INIT_HLIST_HEAD(p)
84 #define CFS_INIT_HLIST_NODE(p)             INIT_HLIST_NODE(p)
85
86 #else /* !defined (__linux__) || !defined(__KERNEL__) */
87
88 /*
89  * Simple doubly linked list implementation.
90  *
91  * Some of the internal functions ("__xxx") are useful when
92  * manipulating whole lists rather than single entries, as
93  * sometimes we already know the next/prev entries and we can
94  * generate better code by using them directly rather than
95  * using the generic single-entry routines.
96  */
97
98 #define prefetch(a) ((void)a)
99
100 struct cfs_list_head {
101         struct cfs_list_head *next, *prev;
102 };
103
104 typedef struct cfs_list_head cfs_list_t;
105
106 #define CFS_LIST_HEAD_INIT(name) { &(name), &(name) }
107
108 #define CFS_LIST_HEAD(name) \
109         cfs_list_t name = CFS_LIST_HEAD_INIT(name)
110
111 #define CFS_INIT_LIST_HEAD(ptr) do { \
112         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
113 } while (0)
114
115 /**
116  * Insert a new entry between two known consecutive entries.
117  *
118  * This is only for internal list manipulation where we know
119  * the prev/next entries already!
120  */
121 static inline void __cfs_list_add(cfs_list_t * new,
122                                   cfs_list_t * prev,
123                                   cfs_list_t * next)
124 {
125         next->prev = new;
126         new->next = next;
127         new->prev = prev;
128         prev->next = new;
129 }
130
131 /**
132  * Insert an entry at the start of a list.
133  * \param new  new entry to be inserted
134  * \param head list to add it to
135  *
136  * Insert a new entry after the specified head.
137  * This is good for implementing stacks.
138  */
139 static inline void cfs_list_add(cfs_list_t *new,
140                                 cfs_list_t *head)
141 {
142         __cfs_list_add(new, head, head->next);
143 }
144
145 /**
146  * Insert an entry at the end of a list.
147  * \param new  new entry to be inserted
148  * \param head list to add it to
149  *
150  * Insert a new entry before the specified head.
151  * This is useful for implementing queues.
152  */
153 static inline void cfs_list_add_tail(cfs_list_t *new,
154                                      cfs_list_t *head)
155 {
156         __cfs_list_add(new, head->prev, head);
157 }
158
159 /*
160  * Delete a list entry by making the prev/next entries
161  * point to each other.
162  *
163  * This is only for internal list manipulation where we know
164  * the prev/next entries already!
165  */
166 static inline void __cfs_list_del(cfs_list_t *prev,
167                                   cfs_list_t *next)
168 {
169         next->prev = prev;
170         prev->next = next;
171 }
172
173 /**
174  * Remove an entry from the list it is currently in.
175  * \param entry the entry to remove
176  * Note: list_empty(entry) does not return true after this, the entry is in an
177  * undefined state.
178  */
179 static inline void cfs_list_del(cfs_list_t *entry)
180 {
181         __cfs_list_del(entry->prev, entry->next);
182 }
183
184 /**
185  * Remove an entry from the list it is currently in and reinitialize it.
186  * \param entry the entry to remove.
187  */
188 static inline void cfs_list_del_init(cfs_list_t *entry)
189 {
190         __cfs_list_del(entry->prev, entry->next);
191         CFS_INIT_LIST_HEAD(entry);
192 }
193
194 /**
195  * Remove an entry from the list it is currently in and insert it at the start
196  * of another list.
197  * \param list the entry to move
198  * \param head the list to move it to
199  */
200 static inline void cfs_list_move(cfs_list_t *list,
201                                  cfs_list_t *head)
202 {
203         __cfs_list_del(list->prev, list->next);
204         cfs_list_add(list, head);
205 }
206
207 /**
208  * Remove an entry from the list it is currently in and insert it at the end of
209  * another list.
210  * \param list the entry to move
211  * \param head the list to move it to
212  */
213 static inline void cfs_list_move_tail(cfs_list_t *list,
214                                       cfs_list_t *head)
215 {
216         __cfs_list_del(list->prev, list->next);
217         cfs_list_add_tail(list, head);
218 }
219
220 /**
221  * Test whether a list is empty
222  * \param head the list to test.
223  */
224 static inline int cfs_list_empty(cfs_list_t *head)
225 {
226         return head->next == head;
227 }
228
229 /**
230  * Test whether a list is empty and not being modified
231  * \param head the list to test
232  *
233  * Tests whether a list is empty _and_ checks that no other CPU might be
234  * in the process of modifying either member (next or prev)
235  *
236  * NOTE: using cfs_list_empty_careful() without synchronization
237  * can only be safe if the only activity that can happen
238  * to the list entry is cfs_list_del_init(). Eg. it cannot be used
239  * if another CPU could re-list_add() it.
240  */
241 static inline int cfs_list_empty_careful(const cfs_list_t *head)
242 {
243         cfs_list_t *next = head->next;
244         return (next == head) && (next == head->prev);
245 }
246
247 static inline void __cfs_list_splice(cfs_list_t *list,
248                                      cfs_list_t *head)
249 {
250         cfs_list_t *first = list->next;
251         cfs_list_t *last = list->prev;
252         cfs_list_t *at = head->next;
253
254         first->prev = head;
255         head->next = first;
256
257         last->next = at;
258         at->prev = last;
259 }
260
261 /**
262  * Join two lists
263  * \param list the new list to add.
264  * \param head the place to add it in the first list.
265  *
266  * The contents of \a list are added at the start of \a head.  \a list is in an
267  * undefined state on return.
268  */
269 static inline void cfs_list_splice(cfs_list_t *list,
270                                    cfs_list_t *head)
271 {
272         if (!cfs_list_empty(list))
273                 __cfs_list_splice(list, head);
274 }
275
276 /**
277  * Join two lists and reinitialise the emptied list.
278  * \param list the new list to add.
279  * \param head the place to add it in the first list.
280  *
281  * The contents of \a list are added at the start of \a head.  \a list is empty
282  * on return.
283  */
284 static inline void cfs_list_splice_init(cfs_list_t *list,
285                                         cfs_list_t *head)
286 {
287         if (!cfs_list_empty(list)) {
288                 __cfs_list_splice(list, head);
289                 CFS_INIT_LIST_HEAD(list);
290         }
291 }
292
293 /**
294  * Get the container of a list
295  * \param ptr    the embedded list.
296  * \param type   the type of the struct this is embedded in.
297  * \param member the member name of the list within the struct.
298  */
299 #define cfs_list_entry(ptr, type, member) \
300         ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
301
302 /**
303  * Iterate over a list
304  * \param pos   the iterator
305  * \param head  the list to iterate over
306  *
307  * Behaviour is undefined if \a pos is removed from the list in the body of the
308  * loop.
309  */
310 #define cfs_list_for_each(pos, head) \
311         for (pos = (head)->next, prefetch(pos->next); pos != (head); \
312                 pos = pos->next, prefetch(pos->next))
313
314 /**
315  * Iterate over a list safely
316  * \param pos   the iterator
317  * \param n     temporary storage
318  * \param head  the list to iterate over
319  *
320  * This is safe to use if \a pos could be removed from the list in the body of
321  * the loop.
322  */
323 #define cfs_list_for_each_safe(pos, n, head) \
324         for (pos = (head)->next, n = pos->next; pos != (head); \
325                 pos = n, n = pos->next)
326
327 /**
328  * Iterate over a list continuing after existing point
329  * \param pos    the type * to use as a loop counter
330  * \param head   the list head
331  * \param member the name of the list_struct within the struct  
332  */
333 #define cfs_list_for_each_entry_continue(pos, head, member)                 \
334         for (pos = cfs_list_entry(pos->member.next, typeof(*pos), member);  \
335              prefetch(pos->member.next), &pos->member != (head);            \
336              pos = cfs_list_entry(pos->member.next, typeof(*pos), member))
337
338 /**
339  * \defgroup hlist Hash List
340  * Double linked lists with a single pointer list head.
341  * Mostly useful for hash tables where the two pointer list head is too
342  * wasteful.  You lose the ability to access the tail in O(1).
343  * @{
344  */
345
346 typedef struct cfs_hlist_node {
347         struct cfs_hlist_node *next, **pprev;
348 } cfs_hlist_node_t;
349
350 typedef struct cfs_hlist_head {
351         cfs_hlist_node_t *first;
352 } cfs_hlist_head_t;
353
354 /* @} */
355
356 /*
357  * "NULL" might not be defined at this point
358  */
359 #ifdef NULL
360 #define NULL_P NULL
361 #else
362 #define NULL_P ((void *)0)
363 #endif
364
365 /**
366  * \addtogroup hlist
367  * @{
368  */
369
370 #define CFS_HLIST_HEAD_INIT { NULL_P }
371 #define CFS_HLIST_HEAD(name) cfs_hlist_head_t name = { NULL_P }
372 #define CFS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL_P)
373 #define CFS_INIT_HLIST_NODE(ptr) ((ptr)->next = NULL_P, (ptr)->pprev = NULL_P)
374
375 static inline int cfs_hlist_unhashed(const cfs_hlist_node_t *h)
376 {
377         return !h->pprev;
378 }
379
380 static inline int cfs_hlist_empty(const cfs_hlist_head_t *h)
381 {
382         return !h->first;
383 }
384
385 static inline void __cfs_hlist_del(cfs_hlist_node_t *n)
386 {
387         cfs_hlist_node_t *next = n->next;
388         cfs_hlist_node_t **pprev = n->pprev;
389         *pprev = next;
390         if (next)
391                 next->pprev = pprev;
392 }
393
394 static inline void cfs_hlist_del(cfs_hlist_node_t *n)
395 {
396         __cfs_hlist_del(n);
397 }
398
399 static inline void cfs_hlist_del_init(cfs_hlist_node_t *n)
400 {
401         if (n->pprev)  {
402                 __cfs_hlist_del(n);
403                 CFS_INIT_HLIST_NODE(n);
404         }
405 }
406
407 static inline void cfs_hlist_add_head(cfs_hlist_node_t *n,
408                                       cfs_hlist_head_t *h)
409 {
410         cfs_hlist_node_t *first = h->first;
411         n->next = first;
412         if (first)
413                 first->pprev = &n->next;
414         h->first = n;
415         n->pprev = &h->first;
416 }
417
418 /* next must be != NULL */
419 static inline void cfs_hlist_add_before(cfs_hlist_node_t *n,
420                                         cfs_hlist_node_t *next)
421 {
422         n->pprev = next->pprev;
423         n->next = next;
424         next->pprev = &n->next;
425         *(n->pprev) = n;
426 }
427
428 static inline void cfs_hlist_add_after(cfs_hlist_node_t *n,
429                                        cfs_hlist_node_t *next)
430 {
431         next->next = n->next;
432         n->next = next;
433         next->pprev = &n->next;
434
435         if(next->next)
436                 next->next->pprev  = &next->next;
437 }
438
439 #define cfs_hlist_entry(ptr, type, member) container_of(ptr,type,member)
440
441 #define cfs_hlist_for_each(pos, head) \
442         for (pos = (head)->first; pos && (prefetch(pos->next), 1); \
443              pos = pos->next)
444
445 #define cfs_hlist_for_each_safe(pos, n, head) \
446         for (pos = (head)->first; pos && (n = pos->next, 1); \
447              pos = n)
448
449 /**
450  * Iterate over an hlist of given type
451  * \param tpos   the type * to use as a loop counter.
452  * \param pos    the &struct hlist_node to use as a loop counter.
453  * \param head   the head for your list.
454  * \param member the name of the hlist_node within the struct.
455  */
456 #define cfs_hlist_for_each_entry(tpos, pos, head, member)                    \
457         for (pos = (head)->first;                                            \
458              pos && ({ prefetch(pos->next); 1;}) &&                          \
459                 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
460              pos = pos->next)
461
462 /**
463  * Iterate over an hlist continuing after existing point
464  * \param tpos   the type * to use as a loop counter.
465  * \param pos    the &struct hlist_node to use as a loop counter.
466  * \param member the name of the hlist_node within the struct.
467  */
468 #define cfs_hlist_for_each_entry_continue(tpos, pos, member)                 \
469         for (pos = (pos)->next;                                              \
470              pos && ({ prefetch(pos->next); 1;}) &&                          \
471                 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
472              pos = pos->next)
473
474 /**
475  * Iterate over an hlist continuing from an existing point
476  * \param tpos   the type * to use as a loop counter.
477  * \param pos    the &struct hlist_node to use as a loop counter.
478  * \param member the name of the hlist_node within the struct.
479  */
480 #define cfs_hlist_for_each_entry_from(tpos, pos, member)                         \
481         for (; pos && ({ prefetch(pos->next); 1;}) &&                        \
482                 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
483              pos = pos->next)
484
485 /**
486  * Iterate over an hlist of given type safe against removal of list entry
487  * \param tpos   the type * to use as a loop counter.
488  * \param pos    the &struct hlist_node to use as a loop counter.
489  * \param n      another &struct hlist_node to use as temporary storage
490  * \param head   the head for your list.
491  * \param member the name of the hlist_node within the struct.
492  */
493 #define cfs_hlist_for_each_entry_safe(tpos, pos, n, head, member)            \
494         for (pos = (head)->first;                                            \
495              pos && ({ n = pos->next; 1; }) &&                               \
496                 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
497              pos = n)
498
499 /* @} */
500
501 #endif /* __linux__ && __KERNEL__ */
502
503 #ifndef cfs_list_for_each_prev
504 /**
505  * Iterate over a list in reverse order
506  * \param pos   the &struct list_head to use as a loop counter.
507  * \param head  the head for your list.
508  */
509 #define cfs_list_for_each_prev(pos, head) \
510         for (pos = (head)->prev, prefetch(pos->prev); pos != (head);     \
511                 pos = pos->prev, prefetch(pos->prev))
512
513 #endif /* cfs_list_for_each_prev */
514
515 #ifndef cfs_list_for_each_entry
516 /**
517  * Iterate over a list of given type
518  * \param pos        the type * to use as a loop counter.
519  * \param head       the head for your list.
520  * \param member     the name of the list_struct within the struct.
521  */
522 #define cfs_list_for_each_entry(pos, head, member)                          \
523         for (pos = cfs_list_entry((head)->next, typeof(*pos), member),      \
524                      prefetch(pos->member.next);                            \
525              &pos->member != (head);                                        \
526              pos = cfs_list_entry(pos->member.next, typeof(*pos), member),  \
527              prefetch(pos->member.next))
528 #endif /* cfs_list_for_each_entry */
529
530 #ifndef cfs_list_for_each_entry_rcu
531 #define cfs_list_for_each_entry_rcu(pos, head, member) \
532        list_for_each_entry(pos, head, member)
533 #endif
534
535 #ifndef cfs_list_for_each_entry_rcu
536 #define cfs_list_for_each_entry_rcu(pos, head, member) \
537        list_for_each_entry(pos, head, member)
538 #endif
539
540 #ifndef cfs_list_for_each_entry_reverse
541 /**
542  * Iterate backwards over a list of given type.
543  * \param pos        the type * to use as a loop counter.
544  * \param head       the head for your list.
545  * \param member     the name of the list_struct within the struct.
546  */
547 #define cfs_list_for_each_entry_reverse(pos, head, member)                  \
548         for (pos = cfs_list_entry((head)->prev, typeof(*pos), member);      \
549              prefetch(pos->member.prev), &pos->member != (head);            \
550              pos = cfs_list_entry(pos->member.prev, typeof(*pos), member))
551 #endif /* cfs_list_for_each_entry_reverse */
552
553 #ifndef cfs_list_for_each_entry_safe
554 /**
555  * Iterate over a list of given type safe against removal of list entry
556  * \param pos        the type * to use as a loop counter.
557  * \param n          another type * to use as temporary storage
558  * \param head       the head for your list.
559  * \param member     the name of the list_struct within the struct.
560  */
561 #define cfs_list_for_each_entry_safe(pos, n, head, member)                   \
562         for (pos = cfs_list_entry((head)->next, typeof(*pos), member),       \
563                 n = cfs_list_entry(pos->member.next, typeof(*pos), member);  \
564              &pos->member != (head);                                         \
565              pos = n, n = cfs_list_entry(n->member.next, typeof(*n), member))
566
567 #endif /* cfs_list_for_each_entry_safe */
568
569 #ifndef cfs_list_for_each_entry_safe_from
570 /**
571  * Iterate over a list continuing from an existing point
572  * \param pos        the type * to use as a loop cursor.
573  * \param n          another type * to use as temporary storage
574  * \param head       the head for your list.
575  * \param member     the name of the list_struct within the struct.
576  *
577  * Iterate over list of given type from current point, safe against
578  * removal of list entry.
579  */
580 #define cfs_list_for_each_entry_safe_from(pos, n, head, member)             \
581         for (n = cfs_list_entry(pos->member.next, typeof(*pos), member);    \
582              &pos->member != (head);                                        \
583              pos = n, n = cfs_list_entry(n->member.next, typeof(*n), member))
584 #endif /* cfs_list_for_each_entry_safe_from */
585
586 #define cfs_list_for_each_entry_typed(pos, head, type, member)          \
587         for (pos = cfs_list_entry((head)->next, type, member),          \
588                      prefetch(pos->member.next);                        \
589              &pos->member != (head);                                    \
590              pos = cfs_list_entry(pos->member.next, type, member),      \
591              prefetch(pos->member.next))
592
593 #define cfs_list_for_each_entry_reverse_typed(pos, head, type, member)  \
594         for (pos = cfs_list_entry((head)->prev, type, member);          \
595              prefetch(pos->member.prev), &pos->member != (head);        \
596              pos = cfs_list_entry(pos->member.prev, type, member))
597
598 #define cfs_list_for_each_entry_safe_typed(pos, n, head, type, member)  \
599     for (pos = cfs_list_entry((head)->next, type, member),              \
600                 n = cfs_list_entry(pos->member.next, type, member);     \
601              &pos->member != (head);                                    \
602              pos = n, n = cfs_list_entry(n->member.next, type, member))
603
604 #define cfs_list_for_each_entry_safe_from_typed(pos, n, head, type, member)  \
605         for (n = cfs_list_entry(pos->member.next, type, member);             \
606              &pos->member != (head);                                         \
607              pos = n, n = cfs_list_entry(n->member.next, type, member))
608
609 #define cfs_hlist_for_each_entry_typed(tpos, pos, head, type, member)   \
610         for (pos = (head)->first;                                       \
611              pos && (prefetch(pos->next), 1) &&                         \
612                 (tpos = cfs_hlist_entry(pos, type, member), 1);         \
613              pos = pos->next)
614
615 #define cfs_hlist_for_each_entry_safe_typed(tpos, pos, n, head, type, member) \
616         for (pos = (head)->first;                                             \
617              pos && (n = pos->next, 1) &&                                     \
618                 (tpos = cfs_hlist_entry(pos, type, member), 1);               \
619              pos = n)
620
621 #endif /* __LIBCFS_LUSTRE_LIST_H__ */