4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifndef __LIBCFS_LIST_H__
24 #define __LIBCFS_LIST_H__
26 #if defined (__linux__) && defined(__KERNEL__)
28 #include <linux/list.h>
30 typedef struct list_head cfs_list_t;
32 #define __cfs_list_add(new, prev, next) __list_add(new, prev, next)
33 #define cfs_list_add(new, head) list_add(new, head)
35 #define cfs_list_add_tail(new, head) list_add_tail(new, head)
37 #define __cfs_list_del(prev, next) __list_del(prev, next)
38 #define cfs_list_del(entry) list_del(entry)
39 #define cfs_list_del_init(entry) list_del_init(entry)
41 #define cfs_list_move(list, head) list_move(list, head)
42 #define cfs_list_move_tail(list, head) list_move_tail(list, head)
44 #define cfs_list_empty(head) list_empty(head)
45 #define cfs_list_empty_careful(head) list_empty_careful(head)
47 #define __cfs_list_splice(list, head) __list_splice(list, head)
48 #define cfs_list_splice(list, head) list_splice(list, head)
49 #define cfs_list_splice_tail(list, head) list_splice_tail(list, head)
51 #define cfs_list_splice_init(list, head) list_splice_init(list, head)
53 #define cfs_list_entry(ptr, type, member) list_entry(ptr, type, member)
54 #define cfs_list_for_each(pos, head) list_for_each(pos, head)
55 #define cfs_list_for_each_safe(pos, n, head) list_for_each_safe(pos, n, head)
56 #define cfs_list_for_each_prev(pos, head) list_for_each_prev(pos, head)
57 #define cfs_list_for_each_entry(pos, head, member) \
58 list_for_each_entry(pos, head, member)
59 #define cfs_list_for_each_entry_reverse(pos, head, member) \
60 list_for_each_entry_reverse(pos, head, member)
61 #define cfs_list_for_each_entry_safe_reverse(pos, n, head, member) \
62 list_for_each_entry_safe_reverse(pos, n, head, member)
63 #define cfs_list_for_each_entry_safe(pos, n, head, member) \
64 list_for_each_entry_safe(pos, n, head, member)
65 #ifdef list_for_each_entry_safe_from
66 #define cfs_list_for_each_entry_safe_from(pos, n, head, member) \
67 list_for_each_entry_safe_from(pos, n, head, member)
68 #endif /* list_for_each_entry_safe_from */
69 #define cfs_list_for_each_entry_continue(pos, head, member) \
70 list_for_each_entry_continue(pos, head, member)
72 #define CFS_LIST_HEAD_INIT(n) LIST_HEAD_INIT(n)
73 #define CFS_LIST_HEAD(n) LIST_HEAD(n)
74 #define CFS_INIT_LIST_HEAD(p) INIT_LIST_HEAD(p)
76 typedef struct hlist_head cfs_hlist_head_t;
77 typedef struct hlist_node cfs_hlist_node_t;
79 #define cfs_hlist_unhashed(h) hlist_unhashed(h)
81 #define cfs_hlist_empty(h) hlist_empty(h)
83 #define __cfs_hlist_del(n) __hlist_del(n)
84 #define cfs_hlist_del(n) hlist_del(n)
85 #define cfs_hlist_del_init(n) hlist_del_init(n)
87 #define cfs_hlist_add_head(n, next) hlist_add_head(n, next)
88 #define cfs_hlist_add_before(n, next) hlist_add_before(n, next)
89 #define cfs_hlist_add_after(n, next) hlist_add_after(n, next)
91 #define cfs_hlist_entry(ptr, type, member) hlist_entry(ptr, type, member)
92 #define cfs_hlist_for_each(pos, head) hlist_for_each(pos, head)
93 #define cfs_hlist_for_each_safe(pos, n, head) \
94 hlist_for_each_safe(pos, n, head)
95 #ifdef HAVE_HLIST_FOR_EACH_3ARG
96 #define cfs_hlist_for_each_entry(tpos, pos, head, member) \
97 pos = NULL; hlist_for_each_entry(tpos, head, member)
99 #define cfs_hlist_for_each_entry(tpos, pos, head, member) \
100 hlist_for_each_entry(tpos, pos, head, member)
102 #define cfs_hlist_for_each_entry_continue(tpos, pos, member) \
103 hlist_for_each_entry_continue(tpos, pos, member)
104 #define cfs_hlist_for_each_entry_from(tpos, pos, member) \
105 hlist_for_each_entry_from(tpos, pos, member)
106 #ifdef HAVE_HLIST_FOR_EACH_3ARG
107 #define cfs_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
108 pos = NULL; hlist_for_each_entry_safe(tpos, n, head, member)
110 #define cfs_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
111 hlist_for_each_entry_safe(tpos, pos, n, head, member)
114 #define CFS_HLIST_HEAD_INIT HLIST_HEAD_INIT
115 #define CFS_HLIST_HEAD(n) HLIST_HEAD(n)
116 #define CFS_INIT_HLIST_HEAD(p) INIT_HLIST_HEAD(p)
117 #define CFS_INIT_HLIST_NODE(p) INIT_HLIST_NODE(p)
119 #else /* !defined (__linux__) || !defined(__KERNEL__) */
122 * Simple doubly linked list implementation.
124 * Some of the internal functions ("__xxx") are useful when
125 * manipulating whole lists rather than single entries, as
126 * sometimes we already know the next/prev entries and we can
127 * generate better code by using them directly rather than
128 * using the generic single-entry routines.
131 #define prefetch(a) ((void)a)
133 struct cfs_list_head {
134 struct cfs_list_head *next, *prev;
137 typedef struct cfs_list_head cfs_list_t;
139 #define CFS_LIST_HEAD_INIT(name) { &(name), &(name) }
141 #define CFS_LIST_HEAD(name) \
142 cfs_list_t name = CFS_LIST_HEAD_INIT(name)
144 #define CFS_INIT_LIST_HEAD(ptr) do { \
145 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
149 * Insert a new entry between two known consecutive entries.
151 * This is only for internal list manipulation where we know
152 * the prev/next entries already!
154 static inline void __cfs_list_add(cfs_list_t * new,
165 * Insert an entry at the start of a list.
166 * \param new new entry to be inserted
167 * \param head list to add it to
169 * Insert a new entry after the specified head.
170 * This is good for implementing stacks.
172 static inline void cfs_list_add(cfs_list_t *new,
175 __cfs_list_add(new, head, head->next);
179 * Insert an entry at the end of a list.
180 * \param new new entry to be inserted
181 * \param head list to add it to
183 * Insert a new entry before the specified head.
184 * This is useful for implementing queues.
186 static inline void cfs_list_add_tail(cfs_list_t *new,
189 __cfs_list_add(new, head->prev, head);
193 * Delete a list entry by making the prev/next entries
194 * point to each other.
196 * This is only for internal list manipulation where we know
197 * the prev/next entries already!
199 static inline void __cfs_list_del(cfs_list_t *prev,
207 * Remove an entry from the list it is currently in.
208 * \param entry the entry to remove
209 * Note: list_empty(entry) does not return true after this, the entry is in an
212 static inline void cfs_list_del(cfs_list_t *entry)
214 __cfs_list_del(entry->prev, entry->next);
218 * Remove an entry from the list it is currently in and reinitialize it.
219 * \param entry the entry to remove.
221 static inline void cfs_list_del_init(cfs_list_t *entry)
223 __cfs_list_del(entry->prev, entry->next);
224 CFS_INIT_LIST_HEAD(entry);
228 * Remove an entry from the list it is currently in and insert it at the start
230 * \param list the entry to move
231 * \param head the list to move it to
233 static inline void cfs_list_move(cfs_list_t *list,
236 __cfs_list_del(list->prev, list->next);
237 cfs_list_add(list, head);
241 * Remove an entry from the list it is currently in and insert it at the end of
243 * \param list the entry to move
244 * \param head the list to move it to
246 static inline void cfs_list_move_tail(cfs_list_t *list,
249 __cfs_list_del(list->prev, list->next);
250 cfs_list_add_tail(list, head);
254 * Test whether a list is empty
255 * \param head the list to test.
257 static inline int cfs_list_empty(cfs_list_t *head)
259 return head->next == head;
263 * Test whether a list is empty and not being modified
264 * \param head the list to test
266 * Tests whether a list is empty _and_ checks that no other CPU might be
267 * in the process of modifying either member (next or prev)
269 * NOTE: using cfs_list_empty_careful() without synchronization
270 * can only be safe if the only activity that can happen
271 * to the list entry is cfs_list_del_init(). Eg. it cannot be used
272 * if another CPU could re-list_add() it.
274 static inline int cfs_list_empty_careful(const cfs_list_t *head)
276 cfs_list_t *next = head->next;
277 return (next == head) && (next == head->prev);
280 static inline void __cfs_list_splice(cfs_list_t *list,
283 cfs_list_t *first = list->next;
284 cfs_list_t *last = list->prev;
285 cfs_list_t *at = head->next;
296 * \param list the new list to add.
297 * \param head the place to add it in the first list.
299 * The contents of \a list are added at the start of \a head. \a list is in an
300 * undefined state on return.
302 static inline void cfs_list_splice(cfs_list_t *list,
305 if (!cfs_list_empty(list))
306 __cfs_list_splice(list, head);
309 static inline void cfs_list_splice_tail(cfs_list_t *list, cfs_list_t *head)
311 if (!cfs_list_empty(list))
312 __cfs_list_splice(list, head->prev);
316 * Join two lists and reinitialise the emptied list.
317 * \param list the new list to add.
318 * \param head the place to add it in the first list.
320 * The contents of \a list are added at the start of \a head. \a list is empty
323 static inline void cfs_list_splice_init(cfs_list_t *list,
326 if (!cfs_list_empty(list)) {
327 __cfs_list_splice(list, head);
328 CFS_INIT_LIST_HEAD(list);
333 * Get the container of a list
334 * \param ptr the embedded list.
335 * \param type the type of the struct this is embedded in.
336 * \param member the member name of the list within the struct.
338 #define cfs_list_entry(ptr, type, member) \
339 ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
342 * Iterate over a list
343 * \param pos the iterator
344 * \param head the list to iterate over
346 * Behaviour is undefined if \a pos is removed from the list in the body of the
349 #define cfs_list_for_each(pos, head) \
350 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
351 pos = pos->next, prefetch(pos->next))
354 * Iterate over a list safely
355 * \param pos the iterator
356 * \param n temporary storage
357 * \param head the list to iterate over
359 * This is safe to use if \a pos could be removed from the list in the body of
362 #define cfs_list_for_each_safe(pos, n, head) \
363 for (pos = (head)->next, n = pos->next; pos != (head); \
364 pos = n, n = pos->next)
367 * Iterate over a list continuing after existing point
368 * \param pos the type * to use as a loop counter
369 * \param head the list head
370 * \param member the name of the list_struct within the struct
372 #define cfs_list_for_each_entry_continue(pos, head, member) \
373 for (pos = cfs_list_entry(pos->member.next, typeof(*pos), member); \
374 prefetch(pos->member.next), &pos->member != (head); \
375 pos = cfs_list_entry(pos->member.next, typeof(*pos), member))
378 * \defgroup hlist Hash List
379 * Double linked lists with a single pointer list head.
380 * Mostly useful for hash tables where the two pointer list head is too
381 * wasteful. You lose the ability to access the tail in O(1).
385 typedef struct cfs_hlist_node {
386 struct cfs_hlist_node *next, **pprev;
389 typedef struct cfs_hlist_head {
390 cfs_hlist_node_t *first;
396 * "NULL" might not be defined at this point
401 #define NULL_P ((void *)0)
409 #define CFS_HLIST_HEAD_INIT { NULL_P }
410 #define CFS_HLIST_HEAD(name) cfs_hlist_head_t name = { NULL_P }
411 #define CFS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL_P)
412 #define CFS_INIT_HLIST_NODE(ptr) ((ptr)->next = NULL_P, (ptr)->pprev = NULL_P)
414 static inline int cfs_hlist_unhashed(const cfs_hlist_node_t *h)
419 static inline int cfs_hlist_empty(const cfs_hlist_head_t *h)
424 static inline void __cfs_hlist_del(cfs_hlist_node_t *n)
426 cfs_hlist_node_t *next = n->next;
427 cfs_hlist_node_t **pprev = n->pprev;
433 static inline void cfs_hlist_del(cfs_hlist_node_t *n)
438 static inline void cfs_hlist_del_init(cfs_hlist_node_t *n)
442 CFS_INIT_HLIST_NODE(n);
446 static inline void cfs_hlist_add_head(cfs_hlist_node_t *n,
449 cfs_hlist_node_t *first = h->first;
452 first->pprev = &n->next;
454 n->pprev = &h->first;
457 /* next must be != NULL */
458 static inline void cfs_hlist_add_before(cfs_hlist_node_t *n,
459 cfs_hlist_node_t *next)
461 n->pprev = next->pprev;
463 next->pprev = &n->next;
467 static inline void cfs_hlist_add_after(cfs_hlist_node_t *n,
468 cfs_hlist_node_t *next)
470 next->next = n->next;
472 next->pprev = &n->next;
475 next->next->pprev = &next->next;
478 #define cfs_hlist_entry(ptr, type, member) container_of(ptr,type,member)
480 #define cfs_hlist_for_each(pos, head) \
481 for (pos = (head)->first; pos && (prefetch(pos->next), 1); \
484 #define cfs_hlist_for_each_safe(pos, n, head) \
485 for (pos = (head)->first; pos && (n = pos->next, 1); \
489 * Iterate over an hlist of given type
490 * \param tpos the type * to use as a loop counter.
491 * \param pos the &struct hlist_node to use as a loop counter.
492 * \param head the head for your list.
493 * \param member the name of the hlist_node within the struct.
495 #define cfs_hlist_for_each_entry(tpos, pos, head, member) \
496 for (pos = (head)->first; \
497 pos && ({ prefetch(pos->next); 1;}) && \
498 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
502 * Iterate over an hlist continuing after existing point
503 * \param tpos the type * to use as a loop counter.
504 * \param pos the &struct hlist_node to use as a loop counter.
505 * \param member the name of the hlist_node within the struct.
507 #define cfs_hlist_for_each_entry_continue(tpos, pos, member) \
508 for (pos = (pos)->next; \
509 pos && ({ prefetch(pos->next); 1;}) && \
510 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
514 * Iterate over an hlist continuing from an existing point
515 * \param tpos the type * to use as a loop counter.
516 * \param pos the &struct hlist_node to use as a loop counter.
517 * \param member the name of the hlist_node within the struct.
519 #define cfs_hlist_for_each_entry_from(tpos, pos, member) \
520 for (; pos && ({ prefetch(pos->next); 1;}) && \
521 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
525 * Iterate over an hlist of given type safe against removal of list entry
526 * \param tpos the type * to use as a loop counter.
527 * \param pos the &struct hlist_node to use as a loop counter.
528 * \param n another &struct hlist_node to use as temporary storage
529 * \param head the head for your list.
530 * \param member the name of the hlist_node within the struct.
532 #define cfs_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
533 for (pos = (head)->first; \
534 pos && ({ n = pos->next; 1; }) && \
535 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
540 #endif /* __linux__ && __KERNEL__ */
542 #ifndef cfs_list_for_each_prev
544 * Iterate over a list in reverse order
545 * \param pos the &struct list_head to use as a loop counter.
546 * \param head the head for your list.
548 #define cfs_list_for_each_prev(pos, head) \
549 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
550 pos = pos->prev, prefetch(pos->prev))
552 #endif /* cfs_list_for_each_prev */
554 #ifndef cfs_list_for_each_entry
556 * Iterate over a list of given type
557 * \param pos the type * to use as a loop counter.
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(pos, head, member) \
562 for (pos = cfs_list_entry((head)->next, typeof(*pos), member), \
563 prefetch(pos->member.next); \
564 &pos->member != (head); \
565 pos = cfs_list_entry(pos->member.next, typeof(*pos), member), \
566 prefetch(pos->member.next))
567 #endif /* cfs_list_for_each_entry */
569 #ifndef cfs_list_for_each_entry_rcu
570 #define cfs_list_for_each_entry_rcu(pos, head, member) \
571 list_for_each_entry(pos, head, member)
574 #ifndef cfs_list_for_each_entry_rcu
575 #define cfs_list_for_each_entry_rcu(pos, head, member) \
576 list_for_each_entry(pos, head, member)
579 #ifndef cfs_list_for_each_entry_reverse
581 * Iterate backwards over a list of given type.
582 * \param pos the type * to use as a loop counter.
583 * \param head the head for your list.
584 * \param member the name of the list_struct within the struct.
586 #define cfs_list_for_each_entry_reverse(pos, head, member) \
587 for (pos = cfs_list_entry((head)->prev, typeof(*pos), member); \
588 prefetch(pos->member.prev), &pos->member != (head); \
589 pos = cfs_list_entry(pos->member.prev, typeof(*pos), member))
590 #endif /* cfs_list_for_each_entry_reverse */
592 #ifndef cfs_list_for_each_entry_safe
594 * Iterate over a list of given type safe against removal of list entry
595 * \param pos the type * to use as a loop counter.
596 * \param n another type * to use as temporary storage
597 * \param head the head for your list.
598 * \param member the name of the list_struct within the struct.
600 #define cfs_list_for_each_entry_safe(pos, n, head, member) \
601 for (pos = cfs_list_entry((head)->next, typeof(*pos), member), \
602 n = cfs_list_entry(pos->member.next, typeof(*pos), member); \
603 &pos->member != (head); \
604 pos = n, n = cfs_list_entry(n->member.next, typeof(*n), member))
606 #endif /* cfs_list_for_each_entry_safe */
608 #ifndef cfs_list_for_each_entry_safe_from
610 * Iterate over a list continuing from an existing point
611 * \param pos the type * to use as a loop cursor.
612 * \param n another type * to use as temporary storage
613 * \param head the head for your list.
614 * \param member the name of the list_struct within the struct.
616 * Iterate over list of given type from current point, safe against
617 * removal of list entry.
619 #define cfs_list_for_each_entry_safe_from(pos, n, head, member) \
620 for (n = cfs_list_entry(pos->member.next, typeof(*pos), member); \
621 &pos->member != (head); \
622 pos = n, n = cfs_list_entry(n->member.next, typeof(*n), member))
623 #endif /* cfs_list_for_each_entry_safe_from */
625 #define cfs_list_for_each_entry_typed(pos, head, type, member) \
626 for (pos = cfs_list_entry((head)->next, type, member), \
627 prefetch(pos->member.next); \
628 &pos->member != (head); \
629 pos = cfs_list_entry(pos->member.next, type, member), \
630 prefetch(pos->member.next))
632 #define cfs_list_for_each_entry_reverse_typed(pos, head, type, member) \
633 for (pos = cfs_list_entry((head)->prev, type, member); \
634 prefetch(pos->member.prev), &pos->member != (head); \
635 pos = cfs_list_entry(pos->member.prev, type, member))
637 #define cfs_list_for_each_entry_safe_typed(pos, n, head, type, member) \
638 for (pos = cfs_list_entry((head)->next, type, member), \
639 n = cfs_list_entry(pos->member.next, type, member); \
640 &pos->member != (head); \
641 pos = n, n = cfs_list_entry(n->member.next, type, member))
643 #define cfs_list_for_each_entry_safe_from_typed(pos, n, head, type, member) \
644 for (n = cfs_list_entry(pos->member.next, type, member); \
645 &pos->member != (head); \
646 pos = n, n = cfs_list_entry(n->member.next, type, member))
648 #define cfs_hlist_for_each_entry_typed(tpos, pos, head, type, member) \
649 for (pos = (head)->first; \
650 pos && (prefetch(pos->next), 1) && \
651 (tpos = cfs_hlist_entry(pos, type, member), 1); \
654 #define cfs_hlist_for_each_entry_safe_typed(tpos, pos, n, head, type, member) \
655 for (pos = (head)->first; \
656 pos && (n = pos->next, 1) && \
657 (tpos = cfs_hlist_entry(pos, type, member), 1); \
660 #endif /* __LIBCFS_LUSTRE_LIST_H__ */