1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see [sun.com URL with a
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #ifndef __LIBCFS_LIST_H__
38 #define __LIBCFS_LIST_H__
40 #if defined (__linux__) && defined(__KERNEL__)
42 #include <linux/list.h>
44 #define CFS_LIST_HEAD_INIT(n) LIST_HEAD_INIT(n)
45 #define CFS_LIST_HEAD(n) LIST_HEAD(n)
46 #define CFS_INIT_LIST_HEAD(p) INIT_LIST_HEAD(p)
48 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
49 #define CFS_HLIST_HEAD_INIT HLIST_HEAD_INIT
50 #define CFS_HLIST_HEAD(n) HLIST_HEAD(n)
51 #define CFS_INIT_HLIST_HEAD(p) INIT_HLIST_HEAD(p)
52 #define CFS_INIT_HLIST_NODE(p) INIT_HLIST_NODE(p)
55 #else /* !defined (__linux__) || !defined(__KERNEL__) */
58 * Simple doubly linked list implementation.
60 * Some of the internal functions ("__xxx") are useful when
61 * manipulating whole lists rather than single entries, as
62 * sometimes we already know the next/prev entries and we can
63 * generate better code by using them directly rather than
64 * using the generic single-entry routines.
68 #define prefetch(a) ((void)a)
70 #define prefetch(a) ((void *)a)
74 struct list_head *next, *prev;
77 typedef struct list_head list_t;
79 #define CFS_LIST_HEAD_INIT(name) { &(name), &(name) }
81 #define CFS_LIST_HEAD(name) \
82 struct list_head name = CFS_LIST_HEAD_INIT(name)
84 #define CFS_INIT_LIST_HEAD(ptr) do { \
85 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
89 * Insert a new entry between two known consecutive entries.
91 * This is only for internal list manipulation where we know
92 * the prev/next entries already!
94 static inline void __list_add(struct list_head * new,
95 struct list_head * prev,
96 struct list_head * next)
105 * list_add - add a new entry
106 * @new: new entry to be added
107 * @head: list head to add it after
109 * Insert a new entry after the specified head.
110 * This is good for implementing stacks.
112 static inline void list_add(struct list_head *new, struct list_head *head)
114 __list_add(new, head, head->next);
118 * list_add_tail - add a new entry
119 * @new: new entry to be added
120 * @head: list head to add it before
122 * Insert a new entry before the specified head.
123 * This is useful for implementing queues.
125 static inline void list_add_tail(struct list_head *new, struct list_head *head)
127 __list_add(new, head->prev, head);
131 * Delete a list entry by making the prev/next entries
132 * point to each other.
134 * This is only for internal list manipulation where we know
135 * the prev/next entries already!
137 static inline void __list_del(struct list_head * prev, struct list_head * next)
144 * list_del - deletes entry from list.
145 * @entry: the element to delete from the list.
146 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
148 static inline void list_del(struct list_head *entry)
150 __list_del(entry->prev, entry->next);
154 * list_del_init - deletes entry from list and reinitialize it.
155 * @entry: the element to delete from the list.
157 static inline void list_del_init(struct list_head *entry)
159 __list_del(entry->prev, entry->next);
160 CFS_INIT_LIST_HEAD(entry);
164 * list_move - delete from one list and add as another's head
165 * @list: the entry to move
166 * @head: the head that will precede our entry
168 * This is not safe to use if @list is already on the same list as @head.
170 static inline void list_move(struct list_head *list, struct list_head *head)
172 __list_del(list->prev, list->next);
173 list_add(list, head);
177 * list_move_tail - delete from one list and add as another's tail
178 * @list: the entry to move
179 * @head: the head that will follow our entry
181 * This is not safe to use if @list is already on the same list as @head.
183 static inline void list_move_tail(struct list_head *list,
184 struct list_head *head)
186 __list_del(list->prev, list->next);
187 list_add_tail(list, head);
191 * list_empty - tests whether a list is empty
192 * @head: the list to test.
194 static inline int list_empty(struct list_head *head)
196 return head->next == head;
199 static inline void __list_splice(struct list_head *list,
200 struct list_head *head)
202 struct list_head *first = list->next;
203 struct list_head *last = list->prev;
204 struct list_head *at = head->next;
214 * list_splice - join two lists
215 * @list: the new list to add.
216 * @head: the place to add it in the first list.
218 static inline void list_splice(struct list_head *list, struct list_head *head)
220 if (!list_empty(list))
221 __list_splice(list, head);
225 * list_splice_init - join two lists and reinitialise the emptied list.
226 * @list: the new list to add.
227 * @head: the place to add it in the first list.
229 * The list at @list is reinitialised
231 static inline void list_splice_init(struct list_head *list,
232 struct list_head *head)
234 if (!list_empty(list)) {
235 __list_splice(list, head);
236 CFS_INIT_LIST_HEAD(list);
241 * list_entry - get the struct for this entry
242 * @ptr: the &struct list_head pointer.
243 * @type: the type of the struct this is embedded in.
244 * @member: the name of the list_struct within the struct.
246 #define list_entry(ptr, type, member) \
247 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
250 * list_for_each - iterate over a list
251 * @pos: the &struct list_head to use as a loop counter.
252 * @head: the head for your list.
254 #define list_for_each(pos, head) \
255 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
256 pos = pos->next, prefetch(pos->next))
259 * list_for_each_safe - iterate over a list safe against removal of list entry
260 * @pos: the &struct list_head to use as a loop counter.
261 * @n: another &struct list_head to use as temporary storage
262 * @head: the head for your list.
264 #define list_for_each_safe(pos, n, head) \
265 for (pos = (head)->next, n = pos->next; pos != (head); \
266 pos = n, n = pos->next)
269 * Double linked lists with a single pointer list head.
270 * Mostly useful for hash tables where the two pointer list head is
272 * You lose the ability to access the tail in O(1).
276 struct hlist_node *first;
280 struct hlist_node *next, **pprev;
284 * "NULL" might not be defined at this point
289 #define NULL_P ((void *)0)
292 #define CFS_HLIST_HEAD_INIT { .first = NULL_P }
293 #define CFS_HLIST_HEAD(name) struct hlist_head name = { .first = NULL_P }
294 #define CFS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL_P)
295 #define CFS_INIT_HLIST_NODE(ptr) ((ptr)->next = NULL_P, (ptr)->pprev = NULL_P)
297 #define HLIST_HEAD_INIT CFS_HLIST_HEAD_INIT
298 #define HLIST_HEAD(n) CFS_HLIST_HEAD(n)
299 #define INIT_HLIST_HEAD(p) CFS_INIT_HLIST_HEAD(p)
300 #define INIT_HLIST_NODE(p) CFS_INIT_HLIST_NODE(p)
302 static inline int hlist_unhashed(const struct hlist_node *h)
307 static inline int hlist_empty(const struct hlist_head *h)
312 static inline void __hlist_del(struct hlist_node *n)
314 struct hlist_node *next = n->next;
315 struct hlist_node **pprev = n->pprev;
321 static inline void hlist_del(struct hlist_node *n)
326 static inline void hlist_del_init(struct hlist_node *n)
334 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
336 struct hlist_node *first = h->first;
339 first->pprev = &n->next;
341 n->pprev = &h->first;
344 /* next must be != NULL */
345 static inline void hlist_add_before(struct hlist_node *n,
346 struct hlist_node *next)
348 n->pprev = next->pprev;
350 next->pprev = &n->next;
354 static inline void hlist_add_after(struct hlist_node *n,
355 struct hlist_node *next)
357 next->next = n->next;
359 next->pprev = &n->next;
362 next->next->pprev = &next->next;
365 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
367 #define hlist_for_each(pos, head) \
368 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
371 #define hlist_for_each_safe(pos, n, head) \
372 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
376 * hlist_for_each_entry - iterate over list of given type
377 * @tpos: the type * to use as a loop counter.
378 * @pos: the &struct hlist_node to use as a loop counter.
379 * @head: the head for your list.
380 * @member: the name of the hlist_node within the struct.
382 #define hlist_for_each_entry(tpos, pos, head, member) \
383 for (pos = (head)->first; \
384 pos && ({ prefetch(pos->next); 1;}) && \
385 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
389 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
390 * @tpos: the type * to use as a loop counter.
391 * @pos: the &struct hlist_node to use as a loop counter.
392 * @member: the name of the hlist_node within the struct.
394 #define hlist_for_each_entry_continue(tpos, pos, member) \
395 for (pos = (pos)->next; \
396 pos && ({ prefetch(pos->next); 1;}) && \
397 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
401 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
402 * @tpos: the type * to use as a loop counter.
403 * @pos: the &struct hlist_node to use as a loop counter.
404 * @member: the name of the hlist_node within the struct.
406 #define hlist_for_each_entry_from(tpos, pos, member) \
407 for (; pos && ({ prefetch(pos->next); 1;}) && \
408 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
412 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
413 * @tpos: the type * to use as a loop counter.
414 * @pos: the &struct hlist_node to use as a loop counter.
415 * @n: another &struct hlist_node to use as temporary storage
416 * @head: the head for your list.
417 * @member: the name of the hlist_node within the struct.
419 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
420 for (pos = (head)->first; \
421 pos && ({ n = pos->next; 1; }) && \
422 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
425 #endif /* __linux__ && __KERNEL__ */
427 #ifndef list_for_each_prev
429 * list_for_each_prev - iterate over a list in reverse order
430 * @pos: the &struct list_head to use as a loop counter.
431 * @head: the head for your list.
433 #define list_for_each_prev(pos, head) \
434 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
435 pos = pos->prev, prefetch(pos->prev))
437 #endif /* list_for_each_prev */
439 #ifndef list_for_each_entry
441 * list_for_each_entry - iterate over list of given type
442 * @pos: the type * to use as a loop counter.
443 * @head: the head for your list.
444 * @member: the name of the list_struct within the struct.
446 #define list_for_each_entry(pos, head, member) \
447 for (pos = list_entry((head)->next, typeof(*pos), member), \
448 prefetch(pos->member.next); \
449 &pos->member != (head); \
450 pos = list_entry(pos->member.next, typeof(*pos), member), \
451 prefetch(pos->member.next))
452 #endif /* list_for_each_entry */
454 #ifndef list_for_each_entry_reverse
456 * list_for_each_entry_reverse - iterate backwards over list of given type.
457 * @pos: the type * to use as a loop counter.
458 * @head: the head for your list.
459 * @member: the name of the list_struct within the struct.
461 #define list_for_each_entry_reverse(pos, head, member) \
462 for (pos = list_entry((head)->prev, typeof(*pos), member); \
463 prefetch(pos->member.prev), &pos->member != (head); \
464 pos = list_entry(pos->member.prev, typeof(*pos), member))
465 #endif /* list_for_each_entry_reverse */
467 #ifndef list_for_each_entry_safe
469 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
470 * @pos: the type * to use as a loop counter.
471 * @n: another type * to use as temporary storage
472 * @head: the head for your list.
473 * @member: the name of the list_struct within the struct.
475 #define list_for_each_entry_safe(pos, n, head, member) \
476 for (pos = list_entry((head)->next, typeof(*pos), member), \
477 n = list_entry(pos->member.next, typeof(*pos), member); \
478 &pos->member != (head); \
479 pos = n, n = list_entry(n->member.next, typeof(*n), member))
480 #endif /* list_for_each_entry_safe */
482 #ifndef list_for_each_entry_safe_from
484 * list_for_each_entry_safe_from
485 * @pos: the type * to use as a loop cursor.
486 * @n: another type * to use as temporary storage
487 * @head: the head for your list.
488 * @member: the name of the list_struct within the struct.
490 * Iterate over list of given type from current point, safe against
491 * removal of list entry.
493 #define list_for_each_entry_safe_from(pos, n, head, member) \
494 for (n = list_entry(pos->member.next, typeof(*pos), member); \
495 &pos->member != (head); \
496 pos = n, n = list_entry(n->member.next, typeof(*n), member))
497 #endif /* list_for_each_entry_safe_from */
499 #endif /* __LIBCFS_LUSTRE_LIST_H__ */