1 #ifndef __LIBCFS_LIST_H__
2 #define __LIBCFS_LIST_H__
4 #if defined (__linux__) && defined(__KERNEL__)
6 #include <linux/list.h>
8 #define CFS_LIST_HEAD_INIT(n) LIST_HEAD_INIT(n)
9 #define CFS_LIST_HEAD(n) LIST_HEAD(n)
10 #define CFS_INIT_LIST_HEAD(p) INIT_LIST_HEAD(p)
12 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
13 #define CFS_HLIST_HEAD_INIT HLIST_HEAD_INIT
14 #define CFS_HLIST_HEAD(n) HLIST_HEAD(n)
15 #define CFS_INIT_HLIST_HEAD(p) INIT_HLIST_HEAD(p)
16 #define CFS_INIT_HLIST_NODE(p) INIT_HLIST_NODE(p)
19 #else /* !defined (__linux__) || !defined(__KERNEL__) */
22 * Simple doubly linked list implementation.
24 * Some of the internal functions ("__xxx") are useful when
25 * manipulating whole lists rather than single entries, as
26 * sometimes we already know the next/prev entries and we can
27 * generate better code by using them directly rather than
28 * using the generic single-entry routines.
31 #define prefetch(a) ((void)a)
34 struct list_head *next, *prev;
37 typedef struct list_head list_t;
39 #define CFS_LIST_HEAD_INIT(name) { &(name), &(name) }
41 #define CFS_LIST_HEAD(name) \
42 struct list_head name = CFS_LIST_HEAD_INIT(name)
44 #define CFS_INIT_LIST_HEAD(ptr) do { \
45 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
49 * Insert a new entry between two known consecutive entries.
51 * This is only for internal list manipulation where we know
52 * the prev/next entries already!
54 static inline void __list_add(struct list_head * new,
55 struct list_head * prev,
56 struct list_head * next)
65 * list_add - add a new entry
66 * @new: new entry to be added
67 * @head: list head to add it after
69 * Insert a new entry after the specified head.
70 * This is good for implementing stacks.
72 static inline void list_add(struct list_head *new, struct list_head *head)
74 __list_add(new, head, head->next);
78 * list_add_tail - add a new entry
79 * @new: new entry to be added
80 * @head: list head to add it before
82 * Insert a new entry before the specified head.
83 * This is useful for implementing queues.
85 static inline void list_add_tail(struct list_head *new, struct list_head *head)
87 __list_add(new, head->prev, head);
91 * Delete a list entry by making the prev/next entries
92 * point to each other.
94 * This is only for internal list manipulation where we know
95 * the prev/next entries already!
97 static inline void __list_del(struct list_head * prev, struct list_head * next)
104 * list_del - deletes entry from list.
105 * @entry: the element to delete from the list.
106 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
108 static inline void list_del(struct list_head *entry)
110 __list_del(entry->prev, entry->next);
114 * list_del_init - deletes entry from list and reinitialize it.
115 * @entry: the element to delete from the list.
117 static inline void list_del_init(struct list_head *entry)
119 __list_del(entry->prev, entry->next);
120 CFS_INIT_LIST_HEAD(entry);
124 * list_move - delete from one list and add as another's head
125 * @list: the entry to move
126 * @head: the head that will precede our entry
128 * This is not safe to use if @list is already on the same list as @head.
130 static inline void list_move(struct list_head *list, struct list_head *head)
132 __list_del(list->prev, list->next);
133 list_add(list, head);
137 * list_move_tail - delete from one list and add as another's tail
138 * @list: the entry to move
139 * @head: the head that will follow our entry
141 * This is not safe to use if @list is already on the same list as @head.
143 static inline void list_move_tail(struct list_head *list,
144 struct list_head *head)
146 __list_del(list->prev, list->next);
147 list_add_tail(list, head);
151 * list_empty - tests whether a list is empty
152 * @head: the list to test.
154 static inline int list_empty(struct list_head *head)
156 return head->next == head;
159 static inline void __list_splice(struct list_head *list,
160 struct list_head *head)
162 struct list_head *first = list->next;
163 struct list_head *last = list->prev;
164 struct list_head *at = head->next;
174 * list_splice - join two lists
175 * @list: the new list to add.
176 * @head: the place to add it in the first list.
178 static inline void list_splice(struct list_head *list, struct list_head *head)
180 if (!list_empty(list))
181 __list_splice(list, head);
185 * list_splice_init - join two lists and reinitialise the emptied list.
186 * @list: the new list to add.
187 * @head: the place to add it in the first list.
189 * The list at @list is reinitialised
191 static inline void list_splice_init(struct list_head *list,
192 struct list_head *head)
194 if (!list_empty(list)) {
195 __list_splice(list, head);
196 CFS_INIT_LIST_HEAD(list);
201 * list_entry - get the struct for this entry
202 * @ptr: the &struct list_head pointer.
203 * @type: the type of the struct this is embedded in.
204 * @member: the name of the list_struct within the struct.
206 #define list_entry(ptr, type, member) \
207 ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
210 * list_for_each - iterate over a list
211 * @pos: the &struct list_head to use as a loop counter.
212 * @head: the head for your list.
214 #define list_for_each(pos, head) \
215 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
216 pos = pos->next, prefetch(pos->next))
219 * list_for_each_safe - iterate over a list safe against removal of list entry
220 * @pos: the &struct list_head to use as a loop counter.
221 * @n: another &struct list_head to use as temporary storage
222 * @head: the head for your list.
224 #define list_for_each_safe(pos, n, head) \
225 for (pos = (head)->next, n = pos->next; pos != (head); \
226 pos = n, n = pos->next)
229 * Double linked lists with a single pointer list head.
230 * Mostly useful for hash tables where the two pointer list head is
232 * You lose the ability to access the tail in O(1).
236 struct hlist_node *first;
240 struct hlist_node *next, **pprev;
244 * "NULL" might not be defined at this point
249 #define NULL_P ((void *)0)
252 #define CFS_HLIST_HEAD_INIT { NULL_P }
253 #define CFS_HLIST_HEAD(name) struct hlist_head name = { NULL_P }
254 #define CFS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL_P)
255 #define CFS_INIT_HLIST_NODE(ptr) ((ptr)->next = NULL_P, (ptr)->pprev = NULL_P)
257 #define HLIST_HEAD_INIT CFS_HLIST_HEAD_INIT
258 #define HLIST_HEAD(n) CFS_HLIST_HEAD(n)
259 #define INIT_HLIST_HEAD(p) CFS_INIT_HLIST_HEAD(p)
260 #define INIT_HLIST_NODE(p) CFS_INIT_HLIST_NODE(p)
262 static inline int hlist_unhashed(const struct hlist_node *h)
267 static inline int hlist_empty(const struct hlist_head *h)
272 static inline void __hlist_del(struct hlist_node *n)
274 struct hlist_node *next = n->next;
275 struct hlist_node **pprev = n->pprev;
281 static inline void hlist_del(struct hlist_node *n)
286 static inline void hlist_del_init(struct hlist_node *n)
294 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
296 struct hlist_node *first = h->first;
299 first->pprev = &n->next;
301 n->pprev = &h->first;
304 /* next must be != NULL */
305 static inline void hlist_add_before(struct hlist_node *n,
306 struct hlist_node *next)
308 n->pprev = next->pprev;
310 next->pprev = &n->next;
314 static inline void hlist_add_after(struct hlist_node *n,
315 struct hlist_node *next)
317 next->next = n->next;
319 next->pprev = &n->next;
322 next->next->pprev = &next->next;
325 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
327 #define hlist_for_each(pos, head) \
328 for (pos = (head)->first; pos && (prefetch(pos->next), 1); \
331 #define hlist_for_each_safe(pos, n, head) \
332 for (pos = (head)->first; pos && (n = pos->next, 1); \
336 * hlist_for_each_entry - iterate over list of given type
337 * @tpos: the type * to use as a loop counter.
338 * @pos: the &struct hlist_node to use as a loop counter.
339 * @head: the head for your list.
340 * @member: the name of the hlist_node within the struct.
342 #define hlist_for_each_entry(tpos, pos, head, member) \
343 for (pos = (head)->first; \
344 pos && ({ prefetch(pos->next); 1;}) && \
345 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
349 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
350 * @tpos: the type * to use as a loop counter.
351 * @pos: the &struct hlist_node to use as a loop counter.
352 * @member: the name of the hlist_node within the struct.
354 #define hlist_for_each_entry_continue(tpos, pos, member) \
355 for (pos = (pos)->next; \
356 pos && ({ prefetch(pos->next); 1;}) && \
357 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
361 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
362 * @tpos: the type * to use as a loop counter.
363 * @pos: the &struct hlist_node to use as a loop counter.
364 * @member: the name of the hlist_node within the struct.
366 #define hlist_for_each_entry_from(tpos, pos, member) \
367 for (; pos && ({ prefetch(pos->next); 1;}) && \
368 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
372 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
373 * @tpos: the type * to use as a loop counter.
374 * @pos: the &struct hlist_node to use as a loop counter.
375 * @n: another &struct hlist_node to use as temporary storage
376 * @head: the head for your list.
377 * @member: the name of the hlist_node within the struct.
379 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
380 for (pos = (head)->first; \
381 pos && ({ n = pos->next; 1; }) && \
382 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
385 #endif /* __linux__ && __KERNEL__ */
387 #ifndef list_for_each_prev
389 * list_for_each_prev - iterate over a list in reverse order
390 * @pos: the &struct list_head to use as a loop counter.
391 * @head: the head for your list.
393 #define list_for_each_prev(pos, head) \
394 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
395 pos = pos->prev, prefetch(pos->prev))
397 #endif /* list_for_each_prev */
399 #ifndef list_for_each_entry
401 * list_for_each_entry - iterate over list of given type
402 * @pos: the type * to use as a loop counter.
403 * @head: the head for your list.
404 * @member: the name of the list_struct within the struct.
406 #define list_for_each_entry(pos, head, member) \
407 for (pos = list_entry((head)->next, typeof(*pos), member), \
408 prefetch(pos->member.next); \
409 &pos->member != (head); \
410 pos = list_entry(pos->member.next, typeof(*pos), member), \
411 prefetch(pos->member.next))
412 #endif /* list_for_each_entry */
414 #ifndef list_for_each_entry_reverse
416 * list_for_each_entry_reverse - iterate backwards over list of given type.
417 * @pos: the type * to use as a loop counter.
418 * @head: the head for your list.
419 * @member: the name of the list_struct within the struct.
421 #define list_for_each_entry_reverse(pos, head, member) \
422 for (pos = list_entry((head)->prev, typeof(*pos), member); \
423 prefetch(pos->member.prev), &pos->member != (head); \
424 pos = list_entry(pos->member.prev, typeof(*pos), member))
425 #endif /* list_for_each_entry_reverse */
427 #ifndef list_for_each_entry_safe
429 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
430 * @pos: the type * to use as a loop counter.
431 * @n: another type * to use as temporary storage
432 * @head: the head for your list.
433 * @member: the name of the list_struct within the struct.
435 #define list_for_each_entry_safe(pos, n, head, member) \
436 for (pos = list_entry((head)->next, typeof(*pos), member), \
437 n = list_entry(pos->member.next, typeof(*pos), member); \
438 &pos->member != (head); \
439 pos = n, n = list_entry(n->member.next, typeof(*n), member))
441 #endif /* list_for_each_entry_safe */
443 #ifndef list_for_each_entry_safe_from
445 * list_for_each_entry_safe_from
446 * @pos: the type * to use as a loop cursor.
447 * @n: another type * to use as temporary storage
448 * @head: the head for your list.
449 * @member: the name of the list_struct within the struct.
451 * Iterate over list of given type from current point, safe against
452 * removal of list entry.
454 #define list_for_each_entry_safe_from(pos, n, head, member) \
455 for (n = list_entry(pos->member.next, typeof(*pos), member); \
456 &pos->member != (head); \
457 pos = n, n = list_entry(n->member.next, typeof(*n), member))
458 #endif /* list_for_each_entry_safe_from */
460 #define cfs_list_for_each_entry_typed(pos, head, type, member) \
461 for (pos = list_entry((head)->next, type, member), \
462 prefetch(pos->member.next); \
463 &pos->member != (head); \
464 pos = list_entry(pos->member.next, type, member), \
465 prefetch(pos->member.next))
467 #define cfs_list_for_each_entry_reverse_typed(pos, head, type, member) \
468 for (pos = list_entry((head)->prev, type, member); \
469 prefetch(pos->member.prev), &pos->member != (head); \
470 pos = list_entry(pos->member.prev, type, member))
472 #define cfs_list_for_each_entry_safe_typed(pos, n, head, type, member) \
473 for (pos = list_entry((head)->next, type, member), \
474 n = list_entry(pos->member.next, type, member); \
475 &pos->member != (head); \
476 pos = n, n = list_entry(n->member.next, type, member))
478 #define cfs_list_for_each_entry_safe_from_typed(pos, n, head, type, member) \
479 for (n = list_entry(pos->member.next, type, member); \
480 &pos->member != (head); \
481 pos = n, n = list_entry(n->member.next, type, member))
482 #define cfs_hlist_for_each_entry_typed(tpos, pos, head, type, member) \
483 for (pos = (head)->first; \
484 pos && (prefetch(pos->next), 1) && \
485 (tpos = hlist_entry(pos, type, member), 1); \
488 #define cfs_hlist_for_each_entry_safe_typed(tpos, pos, n, head, type, member)\
489 for (pos = (head)->first; \
490 pos && (n = pos->next, 1) && \
491 (tpos = hlist_entry(pos, type, member), 1); \
494 #endif /* __LIBCFS_LUSTRE_LIST_H__ */