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__
27 # include <linux/list.h>
28 #else /* __KERNEL__ */
31 * Simple doubly linked list implementation.
33 * Some of the internal functions ("__xxx") are useful when
34 * manipulating whole lists rather than single entries, as
35 * sometimes we already know the next/prev entries and we can
36 * generate better code by using them directly rather than
37 * using the generic single-entry routines.
40 #define prefetch(a) ((void)a)
43 struct list_head *next, *prev;
46 #define LIST_HEAD_INIT(name) { &(name), &(name) }
48 #define INIT_LIST_HEAD(ptr) do { \
49 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
53 * Insert a new entry between two known consecutive entries.
55 * This is only for internal list manipulation where we know
56 * the prev/next entries already!
58 static inline void __list_add(struct list_head * new,
59 struct list_head * prev,
60 struct list_head * next)
69 * Insert an entry at the start of a list.
70 * \param new new entry to be inserted
71 * \param head list to add it to
73 * Insert a new entry after the specified head.
74 * This is good for implementing stacks.
76 static inline void list_add(struct list_head *new,
77 struct list_head *head)
79 __list_add(new, head, head->next);
83 * Insert an entry at the end of a list.
84 * \param new new entry to be inserted
85 * \param head list to add it to
87 * Insert a new entry before the specified head.
88 * This is useful for implementing queues.
90 static inline void list_add_tail(struct list_head *new,
91 struct list_head *head)
93 __list_add(new, head->prev, head);
97 * Delete a list entry by making the prev/next entries
98 * point to each other.
100 * This is only for internal list manipulation where we know
101 * the prev/next entries already!
103 static inline void __list_del(struct list_head *prev,
104 struct list_head *next)
111 * Remove an entry from the list it is currently in.
112 * \param entry the entry to remove
113 * Note: list_empty(entry) does not return true after this, the entry is in an
116 static inline void list_del(struct list_head *entry)
118 __list_del(entry->prev, entry->next);
122 * Remove an entry from the list it is currently in and reinitialize it.
123 * \param entry the entry to remove.
125 static inline void list_del_init(struct list_head *entry)
127 __list_del(entry->prev, entry->next);
128 INIT_LIST_HEAD(entry);
132 * Remove an entry from the list it is currently in and insert it at the start
134 * \param list the entry to move
135 * \param head the list to move it to
137 static inline void list_move(struct list_head *list,
138 struct list_head *head)
140 __list_del(list->prev, list->next);
141 list_add(list, head);
145 * Remove an entry from the list it is currently in and insert it at the end of
147 * \param list the entry to move
148 * \param head the list to move it to
150 static inline void list_move_tail(struct list_head *list,
151 struct list_head *head)
153 __list_del(list->prev, list->next);
154 list_add_tail(list, head);
158 * Test whether a list is empty
159 * \param head the list to test.
161 static inline int list_empty(struct list_head *head)
163 return head->next == head;
167 * Test whether a list is empty and not being modified
168 * \param head the list to test
170 * Tests whether a list is empty _and_ checks that no other CPU might be
171 * in the process of modifying either member (next or prev)
173 * NOTE: using list_empty_careful() without synchronization
174 * can only be safe if the only activity that can happen
175 * to the list entry is list_del_init(). Eg. it cannot be used
176 * if another CPU could re-list_add() it.
178 static inline int list_empty_careful(const struct list_head *head)
180 struct list_head *next = head->next;
181 return (next == head) && (next == head->prev);
184 static inline void __list_splice(struct list_head *list,
185 struct list_head *head)
187 struct list_head *first = list->next;
188 struct list_head *last = list->prev;
189 struct list_head *at = head->next;
200 * \param list the new list to add.
201 * \param head the place to add it in the first list.
203 * The contents of \a list are added at the start of \a head. \a list is in an
204 * undefined state on return.
206 static inline void list_splice(struct list_head *list,
207 struct list_head *head)
209 if (!list_empty(list))
210 __list_splice(list, head);
213 static inline void list_splice_tail(struct list_head *list, struct list_head *head)
215 if (!list_empty(list))
216 __list_splice(list, head->prev);
220 * Join two lists and reinitialise the emptied list.
221 * \param list the new list to add.
222 * \param head the place to add it in the first list.
224 * The contents of \a list are added at the start of \a head. \a list is empty
227 static inline void list_splice_init(struct list_head *list,
228 struct list_head *head)
230 if (!list_empty(list)) {
231 __list_splice(list, head);
232 INIT_LIST_HEAD(list);
237 * Get the container of a list
238 * \param ptr the embedded list.
239 * \param type the type of the struct this is embedded in.
240 * \param member the member name of the list within the struct.
242 #define list_entry(ptr, type, member) \
243 ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
246 * Iterate over a list
247 * \param pos the iterator
248 * \param head the list to iterate over
250 * Behaviour is undefined if \a pos is removed from the list in the body of the
253 #define list_for_each(pos, head) \
254 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
255 pos = pos->next, prefetch(pos->next))
258 * Iterate over a list safely
259 * \param pos the iterator
260 * \param n temporary storage
261 * \param head the list to iterate over
263 * This is safe to use if \a pos could be removed from the list in the body of
266 #define list_for_each_safe(pos, n, head) \
267 for (pos = (head)->next, n = pos->next; pos != (head); \
268 pos = n, n = pos->next)
271 * Iterate over a list continuing after existing point
272 * \param pos the type * to use as a loop counter
273 * \param head the list head
274 * \param member the name of the list_struct within the struct
276 #define list_for_each_entry_continue(pos, head, member) \
277 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
278 prefetch(pos->member.next), &pos->member != (head); \
279 pos = list_entry(pos->member.next, typeof(*pos), member))
282 * \defgroup hlist Hash List
283 * Double linked lists with a single pointer list head.
284 * Mostly useful for hash tables where the two pointer list head is too
285 * wasteful. You lose the ability to access the tail in O(1).
290 struct hlist_node *next, **pprev;
294 struct hlist_node *first;
300 * "NULL" might not be defined at this point
305 #define NULL_P ((void *)0)
313 #define HLIST_HEAD_INIT { NULL_P }
314 #define HLIST_HEAD(name) struct hlist_head name = { NULL_P }
315 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL_P)
316 #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL_P, (ptr)->pprev = NULL_P)
318 static inline int hlist_unhashed(const struct hlist_node *h)
323 static inline int hlist_empty(const struct hlist_head *h)
328 static inline void __hlist_del(struct hlist_node *n)
330 struct hlist_node *next = n->next;
331 struct hlist_node **pprev = n->pprev;
337 static inline void hlist_del(struct hlist_node *n)
342 static inline void hlist_del_init(struct hlist_node *n)
350 static inline void hlist_add_head(struct hlist_node *n,
351 struct hlist_head *h)
353 struct hlist_node *first = h->first;
356 first->pprev = &n->next;
358 n->pprev = &h->first;
361 /* next must be != NULL */
362 static inline void hlist_add_before(struct hlist_node *n,
363 struct hlist_node *next)
365 n->pprev = next->pprev;
367 next->pprev = &n->next;
371 static inline void hlist_add_after(struct hlist_node *n,
372 struct hlist_node *next)
374 next->next = n->next;
376 next->pprev = &n->next;
379 next->next->pprev = &next->next;
382 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
384 #define hlist_for_each(pos, head) \
385 for (pos = (head)->first; pos && (prefetch(pos->next), 1); \
388 #define hlist_for_each_safe(pos, n, head) \
389 for (pos = (head)->first; pos && (n = pos->next, 1); \
393 * Iterate over an hlist of given type
394 * \param tpos the type * to use as a loop counter.
395 * \param pos the &struct hlist_node to use as a loop counter.
396 * \param head the head for your list.
397 * \param member the name of the hlist_node within the struct.
399 #define hlist_for_each_entry(tpos, pos, head, member) \
400 for (pos = (head)->first; \
401 pos && ({ prefetch(pos->next); 1;}) && \
402 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
406 * Iterate over an hlist continuing after existing point
407 * \param tpos the type * to use as a loop counter.
408 * \param pos the &struct hlist_node to use as a loop counter.
409 * \param member the name of the hlist_node within the struct.
411 #define hlist_for_each_entry_continue(tpos, pos, member) \
412 for (pos = (pos)->next; \
413 pos && ({ prefetch(pos->next); 1;}) && \
414 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
418 * Iterate over an hlist continuing from an existing point
419 * \param tpos the type * to use as a loop counter.
420 * \param pos the &struct hlist_node to use as a loop counter.
421 * \param member the name of the hlist_node within the struct.
423 #define hlist_for_each_entry_from(tpos, pos, member) \
424 for (; pos && ({ prefetch(pos->next); 1;}) && \
425 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
429 * Iterate over an hlist of given type safe against removal of list entry
430 * \param tpos the type * to use as a loop counter.
431 * \param pos the &struct hlist_node to use as a loop counter.
432 * \param n another &struct hlist_node to use as temporary storage
433 * \param head the head for your list.
434 * \param member the name of the hlist_node within the struct.
436 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
437 for (pos = (head)->first; \
438 pos && ({ n = pos->next; 1; }) && \
439 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
444 #endif /* __KERNEL__ */
446 #ifndef list_for_each_prev
448 * Iterate over a list in reverse order
449 * \param pos the &struct list_head to use as a loop counter.
450 * \param head the head for your list.
452 #define list_for_each_prev(pos, head) \
453 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
454 pos = pos->prev, prefetch(pos->prev))
456 #endif /* list_for_each_prev */
458 #ifndef list_for_each_entry
460 * Iterate over a list of given type
461 * \param pos the type * to use as a loop counter.
462 * \param head the head for your list.
463 * \param member the name of the list_struct within the struct.
465 #define list_for_each_entry(pos, head, member) \
466 for (pos = list_entry((head)->next, typeof(*pos), member), \
467 prefetch(pos->member.next); \
468 &pos->member != (head); \
469 pos = list_entry(pos->member.next, typeof(*pos), member), \
470 prefetch(pos->member.next))
471 #endif /* list_for_each_entry */
473 #ifndef list_for_each_entry_reverse
475 * Iterate backwards over a list of given type.
476 * \param pos the type * to use as a loop counter.
477 * \param head the head for your list.
478 * \param member the name of the list_struct within the struct.
480 #define list_for_each_entry_reverse(pos, head, member) \
481 for (pos = list_entry((head)->prev, typeof(*pos), member); \
482 prefetch(pos->member.prev), &pos->member != (head); \
483 pos = list_entry(pos->member.prev, typeof(*pos), member))
484 #endif /* list_for_each_entry_reverse */
486 #ifndef list_for_each_entry_safe
488 * Iterate over a list of given type safe against removal of list entry
489 * \param pos the type * to use as a loop counter.
490 * \param n another type * to use as temporary storage
491 * \param head the head for your list.
492 * \param member the name of the list_struct within the struct.
494 #define list_for_each_entry_safe(pos, n, head, member) \
495 for (pos = list_entry((head)->next, typeof(*pos), member), \
496 n = list_entry(pos->member.next, typeof(*pos), member); \
497 &pos->member != (head); \
498 pos = n, n = list_entry(n->member.next, typeof(*n), member))
500 #endif /* list_for_each_entry_safe */
502 /* Temporary until everything is moved over to linux api */
503 typedef struct list_head cfs_list_t;
505 #define __cfs_list_add(new, prev, next) __list_add(new, prev, next)
506 #define cfs_list_add(new, head) list_add(new, head)
508 #define cfs_list_add_tail(new, head) list_add_tail(new, head)
510 #define __cfs_list_del(prev, next) __list_del(prev, next)
511 #define cfs_list_del(entry) list_del(entry)
512 #define cfs_list_del_init(entry) list_del_init(entry)
514 #define cfs_list_move(list, head) list_move(list, head)
515 #define cfs_list_move_tail(list, head) list_move_tail(list, head)
517 #define cfs_list_empty(head) list_empty(head)
518 #define cfs_list_empty_careful(head) list_empty_careful(head)
520 #define __cfs_list_splice(list, head) __list_splice(list, head)
521 #define cfs_list_splice(list, head) list_splice(list, head)
522 #define cfs_list_splice_tail(list, head) list_splice_tail(list, head)
524 #define cfs_list_splice_init(list, head) list_splice_init(list, head)
526 #define cfs_list_entry(ptr, type, member) list_entry(ptr, type, member)
527 #define cfs_list_for_each(pos, head) list_for_each(pos, head)
528 #define cfs_list_for_each_safe(pos, n, head) list_for_each_safe(pos, n, head)
530 #define cfs_list_for_each_prev(pos, head) list_for_each_prev(pos, head)
531 #define cfs_list_for_each_entry(pos, head, member) \
532 list_for_each_entry(pos, head, member)
533 #define cfs_list_for_each_entry_reverse(pos, head, member) \
534 list_for_each_entry_reverse(pos, head, member)
535 #define cfs_list_for_each_entry_safe_reverse(pos, n, head, member) \
536 list_for_each_entry_safe_reverse(pos, n, head, member)
537 #define cfs_list_for_each_entry_safe(pos, n, head, member) \
538 list_for_each_entry_safe(pos, n, head, member)
539 #ifdef list_for_each_entry_safe_from
540 #define cfs_list_for_each_entry_safe_from(pos, n, head, member) \
541 list_for_each_entry_safe_from(pos, n, head, member)
542 #endif /* list_for_each_entry_safe_from */
543 #define cfs_list_for_each_entry_continue(pos, head, member) \
544 list_for_each_entry_continue(pos, head, member)
546 #define CFS_LIST_HEAD_INIT(n) LIST_HEAD_INIT(n)
547 #define CFS_INIT_LIST_HEAD(p) INIT_LIST_HEAD(p)
549 typedef struct hlist_head cfs_hlist_head_t;
550 typedef struct hlist_node cfs_hlist_node_t;
552 #define cfs_hlist_unhashed(h) hlist_unhashed(h)
554 #define cfs_hlist_empty(h) hlist_empty(h)
556 #define __cfs_hlist_del(n) __hlist_del(n)
557 #define cfs_hlist_del(n) hlist_del(n)
558 #define cfs_hlist_del_init(n) hlist_del_init(n)
560 #define cfs_hlist_add_head(n, next) hlist_add_head(n, next)
561 #define cfs_hlist_add_before(n, next) hlist_add_before(n, next)
562 #define cfs_hlist_add_after(n, next) hlist_add_after(n, next)
564 #define cfs_hlist_entry(ptr, type, member) hlist_entry(ptr, type, member)
565 #define cfs_hlist_for_each(pos, head) hlist_for_each(pos, head)
566 #define cfs_hlist_for_each_safe(pos, n, head) \
567 hlist_for_each_safe(pos, n, head)
568 #ifdef HAVE_HLIST_FOR_EACH_3ARG
569 #define cfs_hlist_for_each_entry(tpos, pos, head, member) \
570 hlist_for_each_entry(tpos, head, member)
571 #define cfs_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
572 hlist_for_each_entry_safe(tpos, n, head, member)
573 #define cfs_hlist_for_each_entry_continue(tpos, pos, member) \
574 hlist_for_each_entry_continue(tpos, member)
575 #define cfs_hlist_for_each_entry_from(tpos, pos, member) \
576 hlist_for_each_entry_from(tpos, member)
578 #define cfs_hlist_for_each_entry(tpos, pos, head, member) \
579 hlist_for_each_entry(tpos, pos, head, member)
580 #define cfs_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
581 hlist_for_each_entry_safe(tpos, pos, n, head, member)
582 #define cfs_hlist_for_each_entry_continue(tpos, pos, member) \
583 hlist_for_each_entry_continue(tpos, pos, member)
584 #define cfs_hlist_for_each_entry_from(tpos, pos, member) \
585 hlist_for_each_entry_from(tpos, pos, member)
588 #define cfs_list_for_each_entry_typed(pos, head, type, member) \
589 for (pos = list_entry((head)->next, type, member), \
590 prefetch(pos->member.next); \
591 &pos->member != (head); \
592 pos = list_entry(pos->member.next, type, member), \
593 prefetch(pos->member.next))
595 #define cfs_list_for_each_entry_safe_typed(pos, n, head, type, member) \
596 for (pos = list_entry((head)->next, type, member), \
597 n = list_entry(pos->member.next, type, member); \
598 &pos->member != (head); \
599 pos = n, n = list_entry(n->member.next, type, member))
601 #define CFS_HLIST_HEAD_INIT HLIST_HEAD_INIT
602 #define CFS_HLIST_HEAD(n) HLIST_HEAD(n)
603 #define CFS_INIT_HLIST_HEAD(p) INIT_HLIST_HEAD(p)
604 #define CFS_INIT_HLIST_NODE(p) INIT_HLIST_NODE(p)
606 #define CFS_LIST_HEAD(name) \
607 struct list_head name = LIST_HEAD_INIT(name)
609 #endif /* __LIBCFS_LUSTRE_LIST_H__ */