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_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)
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)
53 typedef struct hlist_head cfs_hlist_head_t;
54 typedef struct hlist_node cfs_hlist_node_t;
56 #define cfs_hlist_unhashed(h) hlist_unhashed(h)
58 #define cfs_hlist_empty(h) hlist_empty(h)
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)
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)
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)
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)
86 #else /* !defined (__linux__) || !defined(__KERNEL__) */
89 * Simple doubly linked list implementation.
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.
98 #define prefetch(a) ((void)a)
100 struct cfs_list_head {
101 struct cfs_list_head *next, *prev;
104 typedef struct cfs_list_head cfs_list_t;
106 #define CFS_LIST_HEAD_INIT(name) { &(name), &(name) }
108 #define CFS_LIST_HEAD(name) \
109 cfs_list_t name = CFS_LIST_HEAD_INIT(name)
111 #define CFS_INIT_LIST_HEAD(ptr) do { \
112 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
116 * Insert a new entry between two known consecutive entries.
118 * This is only for internal list manipulation where we know
119 * the prev/next entries already!
121 static inline void __cfs_list_add(cfs_list_t * new,
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
136 * Insert a new entry after the specified head.
137 * This is good for implementing stacks.
139 static inline void cfs_list_add(cfs_list_t *new,
142 __cfs_list_add(new, head, head->next);
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
150 * Insert a new entry before the specified head.
151 * This is useful for implementing queues.
153 static inline void cfs_list_add_tail(cfs_list_t *new,
156 __cfs_list_add(new, head->prev, head);
160 * Delete a list entry by making the prev/next entries
161 * point to each other.
163 * This is only for internal list manipulation where we know
164 * the prev/next entries already!
166 static inline void __cfs_list_del(cfs_list_t *prev,
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
179 static inline void cfs_list_del(cfs_list_t *entry)
181 __cfs_list_del(entry->prev, entry->next);
185 * Remove an entry from the list it is currently in and reinitialize it.
186 * \param entry the entry to remove.
188 static inline void cfs_list_del_init(cfs_list_t *entry)
190 __cfs_list_del(entry->prev, entry->next);
191 CFS_INIT_LIST_HEAD(entry);
195 * Remove an entry from the list it is currently in and insert it at the start
197 * \param list the entry to move
198 * \param head the list to move it to
200 static inline void cfs_list_move(cfs_list_t *list,
203 __cfs_list_del(list->prev, list->next);
204 cfs_list_add(list, head);
208 * Remove an entry from the list it is currently in and insert it at the end of
210 * \param list the entry to move
211 * \param head the list to move it to
213 static inline void cfs_list_move_tail(cfs_list_t *list,
216 __cfs_list_del(list->prev, list->next);
217 cfs_list_add_tail(list, head);
221 * Test whether a list is empty
222 * \param head the list to test.
224 static inline int cfs_list_empty(cfs_list_t *head)
226 return head->next == head;
230 * Test whether a list is empty and not being modified
231 * \param head the list to test
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)
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.
241 static inline int cfs_list_empty_careful(const cfs_list_t *head)
243 cfs_list_t *next = head->next;
244 return (next == head) && (next == head->prev);
247 static inline void __cfs_list_splice(cfs_list_t *list,
250 cfs_list_t *first = list->next;
251 cfs_list_t *last = list->prev;
252 cfs_list_t *at = head->next;
263 * \param list the new list to add.
264 * \param head the place to add it in the first list.
266 * The contents of \a list are added at the start of \a head. \a list is in an
267 * undefined state on return.
269 static inline void cfs_list_splice(cfs_list_t *list,
272 if (!cfs_list_empty(list))
273 __cfs_list_splice(list, head);
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.
281 * The contents of \a list are added at the start of \a head. \a list is empty
284 static inline void cfs_list_splice_init(cfs_list_t *list,
287 if (!cfs_list_empty(list)) {
288 __cfs_list_splice(list, head);
289 CFS_INIT_LIST_HEAD(list);
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.
299 #define cfs_list_entry(ptr, type, member) \
300 ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
303 * Iterate over a list
304 * \param pos the iterator
305 * \param head the list to iterate over
307 * Behaviour is undefined if \a pos is removed from the list in the body of the
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))
315 * Iterate over a list safely
316 * \param pos the iterator
317 * \param n temporary storage
318 * \param head the list to iterate over
320 * This is safe to use if \a pos could be removed from the list in the body of
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)
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
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))
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).
346 typedef struct cfs_hlist_node {
347 struct cfs_hlist_node *next, **pprev;
350 typedef struct cfs_hlist_head {
351 cfs_hlist_node_t *first;
357 * "NULL" might not be defined at this point
362 #define NULL_P ((void *)0)
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)
375 static inline int cfs_hlist_unhashed(const cfs_hlist_node_t *h)
380 static inline int cfs_hlist_empty(const cfs_hlist_head_t *h)
385 static inline void __cfs_hlist_del(cfs_hlist_node_t *n)
387 cfs_hlist_node_t *next = n->next;
388 cfs_hlist_node_t **pprev = n->pprev;
394 static inline void cfs_hlist_del(cfs_hlist_node_t *n)
399 static inline void cfs_hlist_del_init(cfs_hlist_node_t *n)
403 CFS_INIT_HLIST_NODE(n);
407 static inline void cfs_hlist_add_head(cfs_hlist_node_t *n,
410 cfs_hlist_node_t *first = h->first;
413 first->pprev = &n->next;
415 n->pprev = &h->first;
418 /* next must be != NULL */
419 static inline void cfs_hlist_add_before(cfs_hlist_node_t *n,
420 cfs_hlist_node_t *next)
422 n->pprev = next->pprev;
424 next->pprev = &n->next;
428 static inline void cfs_hlist_add_after(cfs_hlist_node_t *n,
429 cfs_hlist_node_t *next)
431 next->next = n->next;
433 next->pprev = &n->next;
436 next->next->pprev = &next->next;
439 #define cfs_hlist_entry(ptr, type, member) container_of(ptr,type,member)
441 #define cfs_hlist_for_each(pos, head) \
442 for (pos = (head)->first; pos && (prefetch(pos->next), 1); \
445 #define cfs_hlist_for_each_safe(pos, n, head) \
446 for (pos = (head)->first; pos && (n = pos->next, 1); \
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.
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;}); \
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.
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;}); \
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.
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;}); \
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.
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;}); \
501 #endif /* __linux__ && __KERNEL__ */
503 #ifndef cfs_list_for_each_prev
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.
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))
513 #endif /* cfs_list_for_each_prev */
515 #ifndef cfs_list_for_each_entry
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.
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 */
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)
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)
540 #ifndef cfs_list_for_each_entry_reverse
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.
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 */
553 #ifndef cfs_list_for_each_entry_safe
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.
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))
567 #endif /* cfs_list_for_each_entry_safe */
569 #ifndef cfs_list_for_each_entry_safe_from
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.
577 * Iterate over list of given type from current point, safe against
578 * removal of list entry.
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 */
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))
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))
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))
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))
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); \
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); \
621 #endif /* __LIBCFS_LUSTRE_LIST_H__ */