1 #ifndef __LIBCFS_LIST_H__
2 #define __LIBCFS_LIST_H__
4 #if defined (__linux__) && defined(__KERNEL__)
6 #include <linux/list.h>
8 typedef struct list_head cfs_list_t;
10 #define __cfs_list_add(new, prev, next) __list_add(new, prev, next)
11 #define cfs_list_add(new, head) list_add(new, head)
13 #define cfs_list_add_tail(new, head) list_add_tail(new, head)
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)
19 #define cfs_list_move(list, head) list_move(list, head)
20 #define cfs_list_move_tail(list, head) list_move_tail(list, head)
22 #define cfs_list_empty(head) list_empty(head)
23 #define cfs_list_empty_careful(head) list_empty_careful(head)
25 #define __cfs_list_splice(list, head) __list_splice(list, head)
26 #define cfs_list_splice(list, head) list_splice(list, head)
28 #define cfs_list_splice_init(list, head) list_splice_init(list, head)
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(pos, n, head, member) \
39 list_for_each_entry_safe(pos, n, head, member)
40 #ifdef list_for_each_entry_safe_from
41 #define cfs_list_for_each_entry_safe_from(pos, n, head, member) \
42 list_for_each_entry_safe_from(pos, n, head, member)
43 #endif /* list_for_each_entry_safe_from */
44 #define cfs_list_for_each_entry_continue(pos, head, member) \
45 list_for_each_entry_continue(pos, head, member)
47 #define CFS_LIST_HEAD_INIT(n) LIST_HEAD_INIT(n)
48 #define CFS_LIST_HEAD(n) LIST_HEAD(n)
49 #define CFS_INIT_LIST_HEAD(p) INIT_LIST_HEAD(p)
51 typedef struct hlist_head cfs_hlist_head_t;
52 typedef struct hlist_node cfs_hlist_node_t;
54 #define cfs_hlist_unhashed(h) hlist_unhashed(h)
56 #define cfs_hlist_empty(h) hlist_empty(h)
58 #define __cfs_hlist_del(n) __hlist_del(n)
59 #define cfs_hlist_del(n) hlist_del(n)
60 #define cfs_hlist_del_init(n) hlist_del_init(n)
62 #define cfs_hlist_add_head(n, next) hlist_add_head(n, next)
63 #define cfs_hlist_add_before(n, next) hlist_add_before(n, next)
64 #define cfs_hlist_add_after(n, next) hlist_add_after(n, next)
66 #define cfs_hlist_entry(ptr, type, member) hlist_entry(ptr, type, member)
67 #define cfs_hlist_for_each(pos, head) hlist_for_each(pos, head)
68 #define cfs_hlist_for_each_safe(pos, n, head) \
69 hlist_for_each_safe(pos, n, head)
70 #define cfs_hlist_for_each_entry(tpos, pos, head, member) \
71 hlist_for_each_entry(tpos, pos, head, member)
72 #define cfs_hlist_for_each_entry_continue(tpos, pos, member) \
73 hlist_for_each_entry_continue(tpos, pos, member)
74 #define cfs_hlist_for_each_entry_from(tpos, pos, member) \
75 hlist_for_each_entry_from(tpos, pos, member)
76 #define cfs_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
77 hlist_for_each_entry_safe(tpos, pos, n, head, member)
79 #define CFS_HLIST_HEAD_INIT HLIST_HEAD_INIT
80 #define CFS_HLIST_HEAD(n) HLIST_HEAD(n)
81 #define CFS_INIT_HLIST_HEAD(p) INIT_HLIST_HEAD(p)
82 #define CFS_INIT_HLIST_NODE(p) INIT_HLIST_NODE(p)
84 #else /* !defined (__linux__) || !defined(__KERNEL__) */
87 * Simple doubly linked list implementation.
89 * Some of the internal functions ("__xxx") are useful when
90 * manipulating whole lists rather than single entries, as
91 * sometimes we already know the next/prev entries and we can
92 * generate better code by using them directly rather than
93 * using the generic single-entry routines.
96 #define prefetch(a) ((void)a)
98 struct cfs_list_head {
99 struct cfs_list_head *next, *prev;
102 typedef struct cfs_list_head cfs_list_t;
104 #define CFS_LIST_HEAD_INIT(name) { &(name), &(name) }
106 #define CFS_LIST_HEAD(name) \
107 cfs_list_t name = CFS_LIST_HEAD_INIT(name)
109 #define CFS_INIT_LIST_HEAD(ptr) do { \
110 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
114 * Insert a new entry between two known consecutive entries.
116 * This is only for internal list manipulation where we know
117 * the prev/next entries already!
119 static inline void __cfs_list_add(cfs_list_t * new,
130 * Insert an entry at the start of a list.
131 * \param new new entry to be inserted
132 * \param head list to add it to
134 * Insert a new entry after the specified head.
135 * This is good for implementing stacks.
137 static inline void cfs_list_add(cfs_list_t *new,
140 __cfs_list_add(new, head, head->next);
144 * Insert an entry at the end of a list.
145 * \param new new entry to be inserted
146 * \param head list to add it to
148 * Insert a new entry before the specified head.
149 * This is useful for implementing queues.
151 static inline void cfs_list_add_tail(cfs_list_t *new,
154 __cfs_list_add(new, head->prev, head);
158 * Delete a list entry by making the prev/next entries
159 * point to each other.
161 * This is only for internal list manipulation where we know
162 * the prev/next entries already!
164 static inline void __cfs_list_del(cfs_list_t *prev,
172 * Remove an entry from the list it is currently in.
173 * \param entry the entry to remove
174 * Note: list_empty(entry) does not return true after this, the entry is in an
177 static inline void cfs_list_del(cfs_list_t *entry)
179 __cfs_list_del(entry->prev, entry->next);
183 * Remove an entry from the list it is currently in and reinitialize it.
184 * \param entry the entry to remove.
186 static inline void cfs_list_del_init(cfs_list_t *entry)
188 __cfs_list_del(entry->prev, entry->next);
189 CFS_INIT_LIST_HEAD(entry);
193 * Remove an entry from the list it is currently in and insert it at the start
195 * \param list the entry to move
196 * \param head the list to move it to
198 static inline void cfs_list_move(cfs_list_t *list,
201 __cfs_list_del(list->prev, list->next);
202 cfs_list_add(list, head);
206 * Remove an entry from the list it is currently in and insert it at the end of
208 * \param list the entry to move
209 * \param head the list to move it to
211 static inline void cfs_list_move_tail(cfs_list_t *list,
214 __cfs_list_del(list->prev, list->next);
215 cfs_list_add_tail(list, head);
219 * Test whether a list is empty
220 * \param head the list to test.
222 static inline int cfs_list_empty(cfs_list_t *head)
224 return head->next == head;
228 * Test whether a list is empty and not being modified
229 * \param head the list to test
231 * Tests whether a list is empty _and_ checks that no other CPU might be
232 * in the process of modifying either member (next or prev)
234 * NOTE: using cfs_list_empty_careful() without synchronization
235 * can only be safe if the only activity that can happen
236 * to the list entry is cfs_list_del_init(). Eg. it cannot be used
237 * if another CPU could re-list_add() it.
239 static inline int cfs_list_empty_careful(const cfs_list_t *head)
241 cfs_list_t *next = head->next;
242 return (next == head) && (next == head->prev);
245 static inline void __cfs_list_splice(cfs_list_t *list,
248 cfs_list_t *first = list->next;
249 cfs_list_t *last = list->prev;
250 cfs_list_t *at = head->next;
261 * \param list the new list to add.
262 * \param head the place to add it in the first list.
264 * The contents of \a list are added at the start of \a head. \a list is in an
265 * undefined state on return.
267 static inline void cfs_list_splice(cfs_list_t *list,
270 if (!cfs_list_empty(list))
271 __cfs_list_splice(list, head);
275 * Join two lists and reinitialise the emptied list.
276 * \param list the new list to add.
277 * \param head the place to add it in the first list.
279 * The contents of \a list are added at the start of \a head. \a list is empty
282 static inline void cfs_list_splice_init(cfs_list_t *list,
285 if (!cfs_list_empty(list)) {
286 __cfs_list_splice(list, head);
287 CFS_INIT_LIST_HEAD(list);
292 * Get the container of a list
293 * \param ptr the embedded list.
294 * \param type the type of the struct this is embedded in.
295 * \param member the member name of the list within the struct.
297 #define cfs_list_entry(ptr, type, member) \
298 ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
301 * Iterate over a list
302 * \param pos the iterator
303 * \param head the list to iterate over
305 * Behaviour is undefined if \a pos is removed from the list in the body of the
308 #define cfs_list_for_each(pos, head) \
309 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
310 pos = pos->next, prefetch(pos->next))
313 * Iterate over a list safely
314 * \param pos the iterator
315 * \param n temporary storage
316 * \param head the list to iterate over
318 * This is safe to use if \a pos could be removed from the list in the body of
321 #define cfs_list_for_each_safe(pos, n, head) \
322 for (pos = (head)->next, n = pos->next; pos != (head); \
323 pos = n, n = pos->next)
326 * Iterate over a list continuing after existing point
327 * \param pos the type * to use as a loop counter
328 * \param head the list head
329 * \param member the name of the list_struct within the struct
331 #define cfs_list_for_each_entry_continue(pos, head, member) \
332 for (pos = cfs_list_entry(pos->member.next, typeof(*pos), member); \
333 prefetch(pos->member.next), &pos->member != (head); \
334 pos = cfs_list_entry(pos->member.next, typeof(*pos), member))
337 * \defgroup hlist Hash List
338 * Double linked lists with a single pointer list head.
339 * Mostly useful for hash tables where the two pointer list head is too
340 * wasteful. You lose the ability to access the tail in O(1).
344 typedef struct cfs_hlist_node {
345 struct cfs_hlist_node *next, **pprev;
348 typedef struct cfs_hlist_head {
349 cfs_hlist_node_t *first;
355 * "NULL" might not be defined at this point
360 #define NULL_P ((void *)0)
368 #define CFS_HLIST_HEAD_INIT { NULL_P }
369 #define CFS_HLIST_HEAD(name) cfs_hlist_head_t name = { NULL_P }
370 #define CFS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL_P)
371 #define CFS_INIT_HLIST_NODE(ptr) ((ptr)->next = NULL_P, (ptr)->pprev = NULL_P)
373 static inline int cfs_hlist_unhashed(const cfs_hlist_node_t *h)
378 static inline int cfs_hlist_empty(const cfs_hlist_head_t *h)
383 static inline void __cfs_hlist_del(cfs_hlist_node_t *n)
385 cfs_hlist_node_t *next = n->next;
386 cfs_hlist_node_t **pprev = n->pprev;
392 static inline void cfs_hlist_del(cfs_hlist_node_t *n)
397 static inline void cfs_hlist_del_init(cfs_hlist_node_t *n)
401 CFS_INIT_HLIST_NODE(n);
405 static inline void cfs_hlist_add_head(cfs_hlist_node_t *n,
408 cfs_hlist_node_t *first = h->first;
411 first->pprev = &n->next;
413 n->pprev = &h->first;
416 /* next must be != NULL */
417 static inline void cfs_hlist_add_before(cfs_hlist_node_t *n,
418 cfs_hlist_node_t *next)
420 n->pprev = next->pprev;
422 next->pprev = &n->next;
426 static inline void cfs_hlist_add_after(cfs_hlist_node_t *n,
427 cfs_hlist_node_t *next)
429 next->next = n->next;
431 next->pprev = &n->next;
434 next->next->pprev = &next->next;
437 #define cfs_hlist_entry(ptr, type, member) container_of(ptr,type,member)
439 #define cfs_hlist_for_each(pos, head) \
440 for (pos = (head)->first; pos && (prefetch(pos->next), 1); \
443 #define cfs_hlist_for_each_safe(pos, n, head) \
444 for (pos = (head)->first; pos && (n = pos->next, 1); \
448 * Iterate over an hlist of given type
449 * \param tpos the type * to use as a loop counter.
450 * \param pos the &struct hlist_node to use as a loop counter.
451 * \param head the head for your list.
452 * \param member the name of the hlist_node within the struct.
454 #define cfs_hlist_for_each_entry(tpos, pos, head, member) \
455 for (pos = (head)->first; \
456 pos && ({ prefetch(pos->next); 1;}) && \
457 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
461 * Iterate over an hlist continuing after existing point
462 * \param tpos the type * to use as a loop counter.
463 * \param pos the &struct hlist_node to use as a loop counter.
464 * \param member the name of the hlist_node within the struct.
466 #define cfs_hlist_for_each_entry_continue(tpos, pos, member) \
467 for (pos = (pos)->next; \
468 pos && ({ prefetch(pos->next); 1;}) && \
469 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
473 * Iterate over an hlist continuing from an existing point
474 * \param tpos the type * to use as a loop counter.
475 * \param pos the &struct hlist_node to use as a loop counter.
476 * \param member the name of the hlist_node within the struct.
478 #define cfs_hlist_for_each_entry_from(tpos, pos, member) \
479 for (; pos && ({ prefetch(pos->next); 1;}) && \
480 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
484 * Iterate over an hlist of given type safe against removal of list entry
485 * \param tpos the type * to use as a loop counter.
486 * \param pos the &struct hlist_node to use as a loop counter.
487 * \param n another &struct hlist_node to use as temporary storage
488 * \param head the head for your list.
489 * \param member the name of the hlist_node within the struct.
491 #define cfs_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
492 for (pos = (head)->first; \
493 pos && ({ n = pos->next; 1; }) && \
494 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
499 #endif /* __linux__ && __KERNEL__ */
501 #ifndef cfs_list_for_each_prev
503 * Iterate over a list in reverse order
504 * \param pos the &struct list_head to use as a loop counter.
505 * \param head the head for your list.
507 #define cfs_list_for_each_prev(pos, head) \
508 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
509 pos = pos->prev, prefetch(pos->prev))
511 #endif /* cfs_list_for_each_prev */
513 #ifndef cfs_list_for_each_entry
515 * Iterate over a list of given type
516 * \param pos the type * to use as a loop counter.
517 * \param head the head for your list.
518 * \param member the name of the list_struct within the struct.
520 #define cfs_list_for_each_entry(pos, head, member) \
521 for (pos = cfs_list_entry((head)->next, typeof(*pos), member), \
522 prefetch(pos->member.next); \
523 &pos->member != (head); \
524 pos = cfs_list_entry(pos->member.next, typeof(*pos), member), \
525 prefetch(pos->member.next))
526 #endif /* cfs_list_for_each_entry */
528 #ifndef cfs_list_for_each_entry_rcu
529 #define cfs_list_for_each_entry_rcu(pos, head, member) \
530 list_for_each_entry(pos, head, member)
533 #ifndef cfs_list_for_each_entry_rcu
534 #define cfs_list_for_each_entry_rcu(pos, head, member) \
535 list_for_each_entry(pos, head, member)
538 #ifndef cfs_list_for_each_entry_reverse
540 * Iterate backwards over a list of given type.
541 * \param pos the type * to use as a loop counter.
542 * \param head the head for your list.
543 * \param member the name of the list_struct within the struct.
545 #define cfs_list_for_each_entry_reverse(pos, head, member) \
546 for (pos = cfs_list_entry((head)->prev, typeof(*pos), member); \
547 prefetch(pos->member.prev), &pos->member != (head); \
548 pos = cfs_list_entry(pos->member.prev, typeof(*pos), member))
549 #endif /* cfs_list_for_each_entry_reverse */
551 #ifndef cfs_list_for_each_entry_safe
553 * Iterate over a list of given type safe against removal of list entry
554 * \param pos the type * to use as a loop counter.
555 * \param n another type * to use as temporary storage
556 * \param head the head for your list.
557 * \param member the name of the list_struct within the struct.
559 #define cfs_list_for_each_entry_safe(pos, n, head, member) \
560 for (pos = cfs_list_entry((head)->next, typeof(*pos), member), \
561 n = cfs_list_entry(pos->member.next, typeof(*pos), member); \
562 &pos->member != (head); \
563 pos = n, n = cfs_list_entry(n->member.next, typeof(*n), member))
565 #endif /* cfs_list_for_each_entry_safe */
567 #ifndef cfs_list_for_each_entry_safe_from
569 * Iterate over a list continuing from an existing point
570 * \param pos the type * to use as a loop cursor.
571 * \param n another type * to use as temporary storage
572 * \param head the head for your list.
573 * \param member the name of the list_struct within the struct.
575 * Iterate over list of given type from current point, safe against
576 * removal of list entry.
578 #define cfs_list_for_each_entry_safe_from(pos, n, head, member) \
579 for (n = cfs_list_entry(pos->member.next, typeof(*pos), member); \
580 &pos->member != (head); \
581 pos = n, n = cfs_list_entry(n->member.next, typeof(*n), member))
582 #endif /* cfs_list_for_each_entry_safe_from */
584 #define cfs_list_for_each_entry_typed(pos, head, type, member) \
585 for (pos = cfs_list_entry((head)->next, type, member), \
586 prefetch(pos->member.next); \
587 &pos->member != (head); \
588 pos = cfs_list_entry(pos->member.next, type, member), \
589 prefetch(pos->member.next))
591 #define cfs_list_for_each_entry_reverse_typed(pos, head, type, member) \
592 for (pos = cfs_list_entry((head)->prev, type, member); \
593 prefetch(pos->member.prev), &pos->member != (head); \
594 pos = cfs_list_entry(pos->member.prev, type, member))
596 #define cfs_list_for_each_entry_safe_typed(pos, n, head, type, member) \
597 for (pos = cfs_list_entry((head)->next, type, member), \
598 n = cfs_list_entry(pos->member.next, type, member); \
599 &pos->member != (head); \
600 pos = n, n = cfs_list_entry(n->member.next, type, member))
602 #define cfs_list_for_each_entry_safe_from_typed(pos, n, head, type, member) \
603 for (n = cfs_list_entry(pos->member.next, type, member); \
604 &pos->member != (head); \
605 pos = n, n = cfs_list_entry(n->member.next, type, member))
607 #define cfs_hlist_for_each_entry_typed(tpos, pos, head, type, member) \
608 for (pos = (head)->first; \
609 pos && (prefetch(pos->next), 1) && \
610 (tpos = cfs_hlist_entry(pos, type, member), 1); \
613 #define cfs_hlist_for_each_entry_safe_typed(tpos, pos, n, head, type, member) \
614 for (pos = (head)->first; \
615 pos && (n = pos->next, 1) && \
616 (tpos = cfs_hlist_entry(pos, type, member), 1); \
619 #endif /* __LIBCFS_LUSTRE_LIST_H__ */