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)
50 #define cfs_list_splice_init(list, head) list_splice_init(list, head)
52 #define cfs_list_entry(ptr, type, member) list_entry(ptr, type, member)
53 #define cfs_list_for_each(pos, head) list_for_each(pos, head)
54 #define cfs_list_for_each_safe(pos, n, head) list_for_each_safe(pos, n, head)
55 #define cfs_list_for_each_prev(pos, head) list_for_each_prev(pos, head)
56 #define cfs_list_for_each_entry(pos, head, member) \
57 list_for_each_entry(pos, head, member)
58 #define cfs_list_for_each_entry_reverse(pos, head, member) \
59 list_for_each_entry_reverse(pos, head, member)
60 #define cfs_list_for_each_entry_safe_reverse(pos, n, head, member) \
61 list_for_each_entry_safe_reverse(pos, n, head, member)
62 #define cfs_list_for_each_entry_safe(pos, n, head, member) \
63 list_for_each_entry_safe(pos, n, head, member)
64 #ifdef list_for_each_entry_safe_from
65 #define cfs_list_for_each_entry_safe_from(pos, n, head, member) \
66 list_for_each_entry_safe_from(pos, n, head, member)
67 #endif /* list_for_each_entry_safe_from */
68 #define cfs_list_for_each_entry_continue(pos, head, member) \
69 list_for_each_entry_continue(pos, head, member)
71 #define CFS_LIST_HEAD_INIT(n) LIST_HEAD_INIT(n)
72 #define CFS_LIST_HEAD(n) LIST_HEAD(n)
73 #define CFS_INIT_LIST_HEAD(p) INIT_LIST_HEAD(p)
75 typedef struct hlist_head cfs_hlist_head_t;
76 typedef struct hlist_node cfs_hlist_node_t;
78 #define cfs_hlist_unhashed(h) hlist_unhashed(h)
80 #define cfs_hlist_empty(h) hlist_empty(h)
82 #define __cfs_hlist_del(n) __hlist_del(n)
83 #define cfs_hlist_del(n) hlist_del(n)
84 #define cfs_hlist_del_init(n) hlist_del_init(n)
86 #define cfs_hlist_add_head(n, next) hlist_add_head(n, next)
87 #define cfs_hlist_add_before(n, next) hlist_add_before(n, next)
88 #define cfs_hlist_add_after(n, next) hlist_add_after(n, next)
90 #define cfs_hlist_entry(ptr, type, member) hlist_entry(ptr, type, member)
91 #define cfs_hlist_for_each(pos, head) hlist_for_each(pos, head)
92 #define cfs_hlist_for_each_safe(pos, n, head) \
93 hlist_for_each_safe(pos, n, head)
94 #ifdef HAVE_HLIST_FOR_EACH_3ARG
95 #define cfs_hlist_for_each_entry(tpos, pos, head, member) \
96 pos = NULL; hlist_for_each_entry(tpos, head, member)
98 #define cfs_hlist_for_each_entry(tpos, pos, head, member) \
99 hlist_for_each_entry(tpos, pos, head, member)
101 #define cfs_hlist_for_each_entry_continue(tpos, pos, member) \
102 hlist_for_each_entry_continue(tpos, pos, member)
103 #define cfs_hlist_for_each_entry_from(tpos, pos, member) \
104 hlist_for_each_entry_from(tpos, pos, member)
105 #ifdef HAVE_HLIST_FOR_EACH_3ARG
106 #define cfs_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
107 pos = NULL; hlist_for_each_entry_safe(tpos, n, head, member)
109 #define cfs_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
110 hlist_for_each_entry_safe(tpos, pos, n, head, member)
113 #define CFS_HLIST_HEAD_INIT HLIST_HEAD_INIT
114 #define CFS_HLIST_HEAD(n) HLIST_HEAD(n)
115 #define CFS_INIT_HLIST_HEAD(p) INIT_HLIST_HEAD(p)
116 #define CFS_INIT_HLIST_NODE(p) INIT_HLIST_NODE(p)
118 #else /* !defined (__linux__) || !defined(__KERNEL__) */
121 * Simple doubly linked list implementation.
123 * Some of the internal functions ("__xxx") are useful when
124 * manipulating whole lists rather than single entries, as
125 * sometimes we already know the next/prev entries and we can
126 * generate better code by using them directly rather than
127 * using the generic single-entry routines.
130 #define prefetch(a) ((void)a)
132 struct cfs_list_head {
133 struct cfs_list_head *next, *prev;
136 typedef struct cfs_list_head cfs_list_t;
138 #define CFS_LIST_HEAD_INIT(name) { &(name), &(name) }
140 #define CFS_LIST_HEAD(name) \
141 cfs_list_t name = CFS_LIST_HEAD_INIT(name)
143 #define CFS_INIT_LIST_HEAD(ptr) do { \
144 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
148 * Insert a new entry between two known consecutive entries.
150 * This is only for internal list manipulation where we know
151 * the prev/next entries already!
153 static inline void __cfs_list_add(cfs_list_t * new,
164 * Insert an entry at the start of a list.
165 * \param new new entry to be inserted
166 * \param head list to add it to
168 * Insert a new entry after the specified head.
169 * This is good for implementing stacks.
171 static inline void cfs_list_add(cfs_list_t *new,
174 __cfs_list_add(new, head, head->next);
178 * Insert an entry at the end of a list.
179 * \param new new entry to be inserted
180 * \param head list to add it to
182 * Insert a new entry before the specified head.
183 * This is useful for implementing queues.
185 static inline void cfs_list_add_tail(cfs_list_t *new,
188 __cfs_list_add(new, head->prev, head);
192 * Delete a list entry by making the prev/next entries
193 * point to each other.
195 * This is only for internal list manipulation where we know
196 * the prev/next entries already!
198 static inline void __cfs_list_del(cfs_list_t *prev,
206 * Remove an entry from the list it is currently in.
207 * \param entry the entry to remove
208 * Note: list_empty(entry) does not return true after this, the entry is in an
211 static inline void cfs_list_del(cfs_list_t *entry)
213 __cfs_list_del(entry->prev, entry->next);
217 * Remove an entry from the list it is currently in and reinitialize it.
218 * \param entry the entry to remove.
220 static inline void cfs_list_del_init(cfs_list_t *entry)
222 __cfs_list_del(entry->prev, entry->next);
223 CFS_INIT_LIST_HEAD(entry);
227 * Remove an entry from the list it is currently in and insert it at the start
229 * \param list the entry to move
230 * \param head the list to move it to
232 static inline void cfs_list_move(cfs_list_t *list,
235 __cfs_list_del(list->prev, list->next);
236 cfs_list_add(list, head);
240 * Remove an entry from the list it is currently in and insert it at the end of
242 * \param list the entry to move
243 * \param head the list to move it to
245 static inline void cfs_list_move_tail(cfs_list_t *list,
248 __cfs_list_del(list->prev, list->next);
249 cfs_list_add_tail(list, head);
253 * Test whether a list is empty
254 * \param head the list to test.
256 static inline int cfs_list_empty(cfs_list_t *head)
258 return head->next == head;
262 * Test whether a list is empty and not being modified
263 * \param head the list to test
265 * Tests whether a list is empty _and_ checks that no other CPU might be
266 * in the process of modifying either member (next or prev)
268 * NOTE: using cfs_list_empty_careful() without synchronization
269 * can only be safe if the only activity that can happen
270 * to the list entry is cfs_list_del_init(). Eg. it cannot be used
271 * if another CPU could re-list_add() it.
273 static inline int cfs_list_empty_careful(const cfs_list_t *head)
275 cfs_list_t *next = head->next;
276 return (next == head) && (next == head->prev);
279 static inline void __cfs_list_splice(cfs_list_t *list,
282 cfs_list_t *first = list->next;
283 cfs_list_t *last = list->prev;
284 cfs_list_t *at = head->next;
295 * \param list the new list to add.
296 * \param head the place to add it in the first list.
298 * The contents of \a list are added at the start of \a head. \a list is in an
299 * undefined state on return.
301 static inline void cfs_list_splice(cfs_list_t *list,
304 if (!cfs_list_empty(list))
305 __cfs_list_splice(list, head);
309 * Join two lists and reinitialise the emptied list.
310 * \param list the new list to add.
311 * \param head the place to add it in the first list.
313 * The contents of \a list are added at the start of \a head. \a list is empty
316 static inline void cfs_list_splice_init(cfs_list_t *list,
319 if (!cfs_list_empty(list)) {
320 __cfs_list_splice(list, head);
321 CFS_INIT_LIST_HEAD(list);
326 * Get the container of a list
327 * \param ptr the embedded list.
328 * \param type the type of the struct this is embedded in.
329 * \param member the member name of the list within the struct.
331 #define cfs_list_entry(ptr, type, member) \
332 ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
335 * Iterate over a list
336 * \param pos the iterator
337 * \param head the list to iterate over
339 * Behaviour is undefined if \a pos is removed from the list in the body of the
342 #define cfs_list_for_each(pos, head) \
343 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
344 pos = pos->next, prefetch(pos->next))
347 * Iterate over a list safely
348 * \param pos the iterator
349 * \param n temporary storage
350 * \param head the list to iterate over
352 * This is safe to use if \a pos could be removed from the list in the body of
355 #define cfs_list_for_each_safe(pos, n, head) \
356 for (pos = (head)->next, n = pos->next; pos != (head); \
357 pos = n, n = pos->next)
360 * Iterate over a list continuing after existing point
361 * \param pos the type * to use as a loop counter
362 * \param head the list head
363 * \param member the name of the list_struct within the struct
365 #define cfs_list_for_each_entry_continue(pos, head, member) \
366 for (pos = cfs_list_entry(pos->member.next, typeof(*pos), member); \
367 prefetch(pos->member.next), &pos->member != (head); \
368 pos = cfs_list_entry(pos->member.next, typeof(*pos), member))
371 * \defgroup hlist Hash List
372 * Double linked lists with a single pointer list head.
373 * Mostly useful for hash tables where the two pointer list head is too
374 * wasteful. You lose the ability to access the tail in O(1).
378 typedef struct cfs_hlist_node {
379 struct cfs_hlist_node *next, **pprev;
382 typedef struct cfs_hlist_head {
383 cfs_hlist_node_t *first;
389 * "NULL" might not be defined at this point
394 #define NULL_P ((void *)0)
402 #define CFS_HLIST_HEAD_INIT { NULL_P }
403 #define CFS_HLIST_HEAD(name) cfs_hlist_head_t name = { NULL_P }
404 #define CFS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL_P)
405 #define CFS_INIT_HLIST_NODE(ptr) ((ptr)->next = NULL_P, (ptr)->pprev = NULL_P)
407 static inline int cfs_hlist_unhashed(const cfs_hlist_node_t *h)
412 static inline int cfs_hlist_empty(const cfs_hlist_head_t *h)
417 static inline void __cfs_hlist_del(cfs_hlist_node_t *n)
419 cfs_hlist_node_t *next = n->next;
420 cfs_hlist_node_t **pprev = n->pprev;
426 static inline void cfs_hlist_del(cfs_hlist_node_t *n)
431 static inline void cfs_hlist_del_init(cfs_hlist_node_t *n)
435 CFS_INIT_HLIST_NODE(n);
439 static inline void cfs_hlist_add_head(cfs_hlist_node_t *n,
442 cfs_hlist_node_t *first = h->first;
445 first->pprev = &n->next;
447 n->pprev = &h->first;
450 /* next must be != NULL */
451 static inline void cfs_hlist_add_before(cfs_hlist_node_t *n,
452 cfs_hlist_node_t *next)
454 n->pprev = next->pprev;
456 next->pprev = &n->next;
460 static inline void cfs_hlist_add_after(cfs_hlist_node_t *n,
461 cfs_hlist_node_t *next)
463 next->next = n->next;
465 next->pprev = &n->next;
468 next->next->pprev = &next->next;
471 #define cfs_hlist_entry(ptr, type, member) container_of(ptr,type,member)
473 #define cfs_hlist_for_each(pos, head) \
474 for (pos = (head)->first; pos && (prefetch(pos->next), 1); \
477 #define cfs_hlist_for_each_safe(pos, n, head) \
478 for (pos = (head)->first; pos && (n = pos->next, 1); \
482 * Iterate over an hlist of given type
483 * \param tpos the type * to use as a loop counter.
484 * \param pos the &struct hlist_node to use as a loop counter.
485 * \param head the head for your list.
486 * \param member the name of the hlist_node within the struct.
488 #define cfs_hlist_for_each_entry(tpos, pos, head, member) \
489 for (pos = (head)->first; \
490 pos && ({ prefetch(pos->next); 1;}) && \
491 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
495 * Iterate over an hlist continuing after existing point
496 * \param tpos the type * to use as a loop counter.
497 * \param pos the &struct hlist_node to use as a loop counter.
498 * \param member the name of the hlist_node within the struct.
500 #define cfs_hlist_for_each_entry_continue(tpos, pos, member) \
501 for (pos = (pos)->next; \
502 pos && ({ prefetch(pos->next); 1;}) && \
503 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
507 * Iterate over an hlist continuing from an existing point
508 * \param tpos the type * to use as a loop counter.
509 * \param pos the &struct hlist_node to use as a loop counter.
510 * \param member the name of the hlist_node within the struct.
512 #define cfs_hlist_for_each_entry_from(tpos, pos, member) \
513 for (; pos && ({ prefetch(pos->next); 1;}) && \
514 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
518 * Iterate over an hlist of given type safe against removal of list entry
519 * \param tpos the type * to use as a loop counter.
520 * \param pos the &struct hlist_node to use as a loop counter.
521 * \param n another &struct hlist_node to use as temporary storage
522 * \param head the head for your list.
523 * \param member the name of the hlist_node within the struct.
525 #define cfs_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
526 for (pos = (head)->first; \
527 pos && ({ n = pos->next; 1; }) && \
528 ({ tpos = cfs_hlist_entry(pos, typeof(*tpos), member); 1;}); \
533 #endif /* __linux__ && __KERNEL__ */
535 #ifndef cfs_list_for_each_prev
537 * Iterate over a list in reverse order
538 * \param pos the &struct list_head to use as a loop counter.
539 * \param head the head for your list.
541 #define cfs_list_for_each_prev(pos, head) \
542 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
543 pos = pos->prev, prefetch(pos->prev))
545 #endif /* cfs_list_for_each_prev */
547 #ifndef cfs_list_for_each_entry
549 * Iterate over a list of given type
550 * \param pos the type * to use as a loop counter.
551 * \param head the head for your list.
552 * \param member the name of the list_struct within the struct.
554 #define cfs_list_for_each_entry(pos, head, member) \
555 for (pos = cfs_list_entry((head)->next, typeof(*pos), member), \
556 prefetch(pos->member.next); \
557 &pos->member != (head); \
558 pos = cfs_list_entry(pos->member.next, typeof(*pos), member), \
559 prefetch(pos->member.next))
560 #endif /* cfs_list_for_each_entry */
562 #ifndef cfs_list_for_each_entry_rcu
563 #define cfs_list_for_each_entry_rcu(pos, head, member) \
564 list_for_each_entry(pos, head, member)
567 #ifndef cfs_list_for_each_entry_rcu
568 #define cfs_list_for_each_entry_rcu(pos, head, member) \
569 list_for_each_entry(pos, head, member)
572 #ifndef cfs_list_for_each_entry_reverse
574 * Iterate backwards over a list of given type.
575 * \param pos the type * to use as a loop counter.
576 * \param head the head for your list.
577 * \param member the name of the list_struct within the struct.
579 #define cfs_list_for_each_entry_reverse(pos, head, member) \
580 for (pos = cfs_list_entry((head)->prev, typeof(*pos), member); \
581 prefetch(pos->member.prev), &pos->member != (head); \
582 pos = cfs_list_entry(pos->member.prev, typeof(*pos), member))
583 #endif /* cfs_list_for_each_entry_reverse */
585 #ifndef cfs_list_for_each_entry_safe
587 * Iterate over a list of given type safe against removal of list entry
588 * \param pos the type * to use as a loop counter.
589 * \param n another type * to use as temporary storage
590 * \param head the head for your list.
591 * \param member the name of the list_struct within the struct.
593 #define cfs_list_for_each_entry_safe(pos, n, head, member) \
594 for (pos = cfs_list_entry((head)->next, typeof(*pos), member), \
595 n = cfs_list_entry(pos->member.next, typeof(*pos), member); \
596 &pos->member != (head); \
597 pos = n, n = cfs_list_entry(n->member.next, typeof(*n), member))
599 #endif /* cfs_list_for_each_entry_safe */
601 #ifndef cfs_list_for_each_entry_safe_from
603 * Iterate over a list continuing from an existing point
604 * \param pos the type * to use as a loop cursor.
605 * \param n another type * to use as temporary storage
606 * \param head the head for your list.
607 * \param member the name of the list_struct within the struct.
609 * Iterate over list of given type from current point, safe against
610 * removal of list entry.
612 #define cfs_list_for_each_entry_safe_from(pos, n, head, member) \
613 for (n = cfs_list_entry(pos->member.next, typeof(*pos), member); \
614 &pos->member != (head); \
615 pos = n, n = cfs_list_entry(n->member.next, typeof(*n), member))
616 #endif /* cfs_list_for_each_entry_safe_from */
618 #define cfs_list_for_each_entry_typed(pos, head, type, member) \
619 for (pos = cfs_list_entry((head)->next, type, member), \
620 prefetch(pos->member.next); \
621 &pos->member != (head); \
622 pos = cfs_list_entry(pos->member.next, type, member), \
623 prefetch(pos->member.next))
625 #define cfs_list_for_each_entry_reverse_typed(pos, head, type, member) \
626 for (pos = cfs_list_entry((head)->prev, type, member); \
627 prefetch(pos->member.prev), &pos->member != (head); \
628 pos = cfs_list_entry(pos->member.prev, type, member))
630 #define cfs_list_for_each_entry_safe_typed(pos, n, head, type, member) \
631 for (pos = cfs_list_entry((head)->next, type, member), \
632 n = cfs_list_entry(pos->member.next, type, member); \
633 &pos->member != (head); \
634 pos = n, n = cfs_list_entry(n->member.next, type, member))
636 #define cfs_list_for_each_entry_safe_from_typed(pos, n, head, type, member) \
637 for (n = cfs_list_entry(pos->member.next, type, member); \
638 &pos->member != (head); \
639 pos = n, n = cfs_list_entry(n->member.next, type, member))
641 #define cfs_hlist_for_each_entry_typed(tpos, pos, head, type, member) \
642 for (pos = (head)->first; \
643 pos && (prefetch(pos->next), 1) && \
644 (tpos = cfs_hlist_entry(pos, type, member), 1); \
647 #define cfs_hlist_for_each_entry_safe_typed(tpos, pos, n, head, type, member) \
648 for (pos = (head)->first; \
649 pos && (n = pos->next, 1) && \
650 (tpos = cfs_hlist_entry(pos, type, member), 1); \
653 #endif /* __LIBCFS_LUSTRE_LIST_H__ */