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;
160 * list_empty_careful - tests whether a list is empty and not being modified
161 * @head: the list to test
164 * tests whether a list is empty _and_ checks that no other CPU might be
165 * in the process of modifying either member (next or prev)
167 * NOTE: using list_empty_careful() without synchronization
168 * can only be safe if the only activity that can happen
169 * to the list entry is list_del_init(). Eg. it cannot be used
170 * if another CPU could re-list_add() it.
172 static inline int list_empty_careful(const struct list_head *head)
174 struct list_head *next = head->next;
175 return (next == head) && (next == head->prev);
178 static inline void __list_splice(struct list_head *list,
179 struct list_head *head)
181 struct list_head *first = list->next;
182 struct list_head *last = list->prev;
183 struct list_head *at = head->next;
193 * list_splice - join two lists
194 * @list: the new list to add.
195 * @head: the place to add it in the first list.
197 static inline void list_splice(struct list_head *list, struct list_head *head)
199 if (!list_empty(list))
200 __list_splice(list, head);
204 * list_splice_init - join two lists and reinitialise the emptied list.
205 * @list: the new list to add.
206 * @head: the place to add it in the first list.
208 * The list at @list is reinitialised
210 static inline void list_splice_init(struct list_head *list,
211 struct list_head *head)
213 if (!list_empty(list)) {
214 __list_splice(list, head);
215 CFS_INIT_LIST_HEAD(list);
220 * list_entry - get the struct for this entry
221 * @ptr: the &struct list_head pointer.
222 * @type: the type of the struct this is embedded in.
223 * @member: the name of the list_struct within the struct.
225 #define list_entry(ptr, type, member) \
226 ((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
229 * list_for_each - iterate over a list
230 * @pos: the &struct list_head to use as a loop counter.
231 * @head: the head for your list.
233 #define list_for_each(pos, head) \
234 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
235 pos = pos->next, prefetch(pos->next))
238 * list_for_each_safe - iterate over a list safe against removal of list entry
239 * @pos: the &struct list_head to use as a loop counter.
240 * @n: another &struct list_head to use as temporary storage
241 * @head: the head for your list.
243 #define list_for_each_safe(pos, n, head) \
244 for (pos = (head)->next, n = pos->next; pos != (head); \
245 pos = n, n = pos->next)
248 * Double linked lists with a single pointer list head.
249 * Mostly useful for hash tables where the two pointer list head is
251 * You lose the ability to access the tail in O(1).
255 struct hlist_node *first;
259 struct hlist_node *next, **pprev;
263 * "NULL" might not be defined at this point
268 #define NULL_P ((void *)0)
271 #define CFS_HLIST_HEAD_INIT { NULL_P }
272 #define CFS_HLIST_HEAD(name) struct hlist_head name = { NULL_P }
273 #define CFS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL_P)
274 #define CFS_INIT_HLIST_NODE(ptr) ((ptr)->next = NULL_P, (ptr)->pprev = NULL_P)
276 #define HLIST_HEAD_INIT CFS_HLIST_HEAD_INIT
277 #define HLIST_HEAD(n) CFS_HLIST_HEAD(n)
278 #define INIT_HLIST_HEAD(p) CFS_INIT_HLIST_HEAD(p)
279 #define INIT_HLIST_NODE(p) CFS_INIT_HLIST_NODE(p)
281 static inline int hlist_unhashed(const struct hlist_node *h)
286 static inline int hlist_empty(const struct hlist_head *h)
291 static inline void __hlist_del(struct hlist_node *n)
293 struct hlist_node *next = n->next;
294 struct hlist_node **pprev = n->pprev;
300 static inline void hlist_del(struct hlist_node *n)
305 static inline void hlist_del_init(struct hlist_node *n)
313 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
315 struct hlist_node *first = h->first;
318 first->pprev = &n->next;
320 n->pprev = &h->first;
323 /* next must be != NULL */
324 static inline void hlist_add_before(struct hlist_node *n,
325 struct hlist_node *next)
327 n->pprev = next->pprev;
329 next->pprev = &n->next;
333 static inline void hlist_add_after(struct hlist_node *n,
334 struct hlist_node *next)
336 next->next = n->next;
338 next->pprev = &n->next;
341 next->next->pprev = &next->next;
344 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
346 #define hlist_for_each(pos, head) \
347 for (pos = (head)->first; pos && (prefetch(pos->next), 1); \
350 #define hlist_for_each_safe(pos, n, head) \
351 for (pos = (head)->first; pos && (n = pos->next, 1); \
355 * hlist_for_each_entry - iterate over list of given type
356 * @tpos: the type * to use as a loop counter.
357 * @pos: the &struct hlist_node to use as a loop counter.
358 * @head: the head for your list.
359 * @member: the name of the hlist_node within the struct.
361 #define hlist_for_each_entry(tpos, pos, head, member) \
362 for (pos = (head)->first; \
363 pos && ({ prefetch(pos->next); 1;}) && \
364 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
368 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
369 * @tpos: the type * to use as a loop counter.
370 * @pos: the &struct hlist_node to use as a loop counter.
371 * @member: the name of the hlist_node within the struct.
373 #define hlist_for_each_entry_continue(tpos, pos, member) \
374 for (pos = (pos)->next; \
375 pos && ({ prefetch(pos->next); 1;}) && \
376 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
380 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
381 * @tpos: the type * to use as a loop counter.
382 * @pos: the &struct hlist_node to use as a loop counter.
383 * @member: the name of the hlist_node within the struct.
385 #define hlist_for_each_entry_from(tpos, pos, member) \
386 for (; pos && ({ prefetch(pos->next); 1;}) && \
387 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
391 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
392 * @tpos: the type * to use as a loop counter.
393 * @pos: the &struct hlist_node to use as a loop counter.
394 * @n: another &struct hlist_node to use as temporary storage
395 * @head: the head for your list.
396 * @member: the name of the hlist_node within the struct.
398 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
399 for (pos = (head)->first; \
400 pos && ({ n = pos->next; 1; }) && \
401 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
404 #endif /* __linux__ && __KERNEL__ */
406 #ifndef list_for_each_prev
408 * list_for_each_prev - iterate over a list in reverse order
409 * @pos: the &struct list_head to use as a loop counter.
410 * @head: the head for your list.
412 #define list_for_each_prev(pos, head) \
413 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
414 pos = pos->prev, prefetch(pos->prev))
416 #endif /* list_for_each_prev */
418 #ifndef list_for_each_entry
420 * list_for_each_entry - iterate over list of given type
421 * @pos: the type * to use as a loop counter.
422 * @head: the head for your list.
423 * @member: the name of the list_struct within the struct.
425 #define list_for_each_entry(pos, head, member) \
426 for (pos = list_entry((head)->next, typeof(*pos), member), \
427 prefetch(pos->member.next); \
428 &pos->member != (head); \
429 pos = list_entry(pos->member.next, typeof(*pos), member), \
430 prefetch(pos->member.next))
431 #endif /* list_for_each_entry */
433 #ifndef list_for_each_entry_reverse
435 * list_for_each_entry_reverse - iterate backwards over list of given type.
436 * @pos: the type * to use as a loop counter.
437 * @head: the head for your list.
438 * @member: the name of the list_struct within the struct.
440 #define list_for_each_entry_reverse(pos, head, member) \
441 for (pos = list_entry((head)->prev, typeof(*pos), member); \
442 prefetch(pos->member.prev), &pos->member != (head); \
443 pos = list_entry(pos->member.prev, typeof(*pos), member))
444 #endif /* list_for_each_entry_reverse */
446 #ifndef list_for_each_entry_safe
448 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
449 * @pos: the type * to use as a loop counter.
450 * @n: another type * to use as temporary storage
451 * @head: the head for your list.
452 * @member: the name of the list_struct within the struct.
454 #define list_for_each_entry_safe(pos, n, head, member) \
455 for (pos = list_entry((head)->next, typeof(*pos), member), \
456 n = list_entry(pos->member.next, typeof(*pos), member); \
457 &pos->member != (head); \
458 pos = n, n = list_entry(n->member.next, typeof(*n), member))
460 #endif /* list_for_each_entry_safe */
462 #ifndef list_for_each_entry_safe_from
464 * list_for_each_entry_safe_from
465 * @pos: the type * to use as a loop cursor.
466 * @n: another type * to use as temporary storage
467 * @head: the head for your list.
468 * @member: the name of the list_struct within the struct.
470 * Iterate over list of given type from current point, safe against
471 * removal of list entry.
473 #define list_for_each_entry_safe_from(pos, n, head, member) \
474 for (n = list_entry(pos->member.next, typeof(*pos), member); \
475 &pos->member != (head); \
476 pos = n, n = list_entry(n->member.next, typeof(*n), member))
477 #endif /* list_for_each_entry_safe_from */
479 #define cfs_list_for_each_entry_typed(pos, head, type, member) \
480 for (pos = list_entry((head)->next, type, member), \
481 prefetch(pos->member.next); \
482 &pos->member != (head); \
483 pos = list_entry(pos->member.next, type, member), \
484 prefetch(pos->member.next))
486 #define cfs_list_for_each_entry_reverse_typed(pos, head, type, member) \
487 for (pos = list_entry((head)->prev, type, member); \
488 prefetch(pos->member.prev), &pos->member != (head); \
489 pos = list_entry(pos->member.prev, type, member))
491 #define cfs_list_for_each_entry_safe_typed(pos, n, head, type, member) \
492 for (pos = list_entry((head)->next, type, member), \
493 n = list_entry(pos->member.next, type, member); \
494 &pos->member != (head); \
495 pos = n, n = list_entry(n->member.next, type, member))
497 #define cfs_list_for_each_entry_safe_from_typed(pos, n, head, type, member) \
498 for (n = list_entry(pos->member.next, type, member); \
499 &pos->member != (head); \
500 pos = n, n = list_entry(n->member.next, type, member))
501 #define cfs_hlist_for_each_entry_typed(tpos, pos, head, type, member) \
502 for (pos = (head)->first; \
503 pos && (prefetch(pos->next), 1) && \
504 (tpos = hlist_entry(pos, type, member), 1); \
507 #define cfs_hlist_for_each_entry_safe_typed(tpos, pos, n, head, type, member)\
508 for (pos = (head)->first; \
509 pos && (n = pos->next, 1) && \
510 (tpos = hlist_entry(pos, type, member), 1); \
513 #endif /* __LIBCFS_LUSTRE_LIST_H__ */