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.
32 #define prefetch(a) ((void)a)
34 #define prefetch(a) ((void *)a)
38 struct list_head *next, *prev;
41 typedef struct list_head list_t;
43 #define CFS_LIST_HEAD_INIT(name) { &(name), &(name) }
45 #define CFS_LIST_HEAD(name) \
46 struct list_head name = CFS_LIST_HEAD_INIT(name)
48 #define CFS_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 * list_add - add a new entry
70 * @new: new entry to be added
71 * @head: list head to add it after
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, struct list_head *head)
78 __list_add(new, head, head->next);
82 * list_add_tail - add a new entry
83 * @new: new entry to be added
84 * @head: list head to add it before
86 * Insert a new entry before the specified head.
87 * This is useful for implementing queues.
89 static inline void list_add_tail(struct list_head *new, struct list_head *head)
91 __list_add(new, head->prev, head);
95 * Delete a list entry by making the prev/next entries
96 * point to each other.
98 * This is only for internal list manipulation where we know
99 * the prev/next entries already!
101 static inline void __list_del(struct list_head * prev, struct list_head * next)
108 * list_del - deletes entry from list.
109 * @entry: the element to delete from the list.
110 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
112 static inline void list_del(struct list_head *entry)
114 __list_del(entry->prev, entry->next);
118 * list_del_init - deletes entry from list and reinitialize it.
119 * @entry: the element to delete from the list.
121 static inline void list_del_init(struct list_head *entry)
123 __list_del(entry->prev, entry->next);
124 CFS_INIT_LIST_HEAD(entry);
128 * list_move - delete from one list and add as another's head
129 * @list: the entry to move
130 * @head: the head that will precede our entry
132 * This is not safe to use if @list is already on the same list as @head.
134 static inline void list_move(struct list_head *list, struct list_head *head)
136 __list_del(list->prev, list->next);
137 list_add(list, head);
141 * list_move_tail - delete from one list and add as another's tail
142 * @list: the entry to move
143 * @head: the head that will follow our entry
145 * This is not safe to use if @list is already on the same list as @head.
147 static inline void list_move_tail(struct list_head *list,
148 struct list_head *head)
150 __list_del(list->prev, list->next);
151 list_add_tail(list, head);
155 * list_empty - tests whether a list is empty
156 * @head: the list to test.
158 static inline int list_empty(struct list_head *head)
160 return head->next == head;
163 static inline void __list_splice(struct list_head *list,
164 struct list_head *head)
166 struct list_head *first = list->next;
167 struct list_head *last = list->prev;
168 struct list_head *at = head->next;
178 * list_splice - join two lists
179 * @list: the new list to add.
180 * @head: the place to add it in the first list.
182 static inline void list_splice(struct list_head *list, struct list_head *head)
184 if (!list_empty(list))
185 __list_splice(list, head);
189 * list_splice_init - join two lists and reinitialise the emptied list.
190 * @list: the new list to add.
191 * @head: the place to add it in the first list.
193 * The list at @list is reinitialised
195 static inline void list_splice_init(struct list_head *list,
196 struct list_head *head)
198 if (!list_empty(list)) {
199 __list_splice(list, head);
200 CFS_INIT_LIST_HEAD(list);
205 * list_entry - get the struct for this entry
206 * @ptr: the &struct list_head pointer.
207 * @type: the type of the struct this is embedded in.
208 * @member: the name of the list_struct within the struct.
210 #define list_entry(ptr, type, member) \
211 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
214 * list_for_each - iterate over a list
215 * @pos: the &struct list_head to use as a loop counter.
216 * @head: the head for your list.
218 #define list_for_each(pos, head) \
219 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
220 pos = pos->next, prefetch(pos->next))
223 * list_for_each_safe - iterate over a list safe against removal of list entry
224 * @pos: the &struct list_head to use as a loop counter.
225 * @n: another &struct list_head to use as temporary storage
226 * @head: the head for your list.
228 #define list_for_each_safe(pos, n, head) \
229 for (pos = (head)->next, n = pos->next; pos != (head); \
230 pos = n, n = pos->next)
233 * Double linked lists with a single pointer list head.
234 * Mostly useful for hash tables where the two pointer list head is
236 * You lose the ability to access the tail in O(1).
240 struct hlist_node *first;
244 struct hlist_node *next, **pprev;
248 * "NULL" might not be defined at this point
253 #define NULL_P ((void *)0)
256 #define CFS_HLIST_HEAD_INIT { .first = NULL_P }
257 #define CFS_HLIST_HEAD(name) struct hlist_head name = { .first = NULL_P }
258 #define CFS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL_P)
259 #define CFS_INIT_HLIST_NODE(ptr) ((ptr)->next = NULL_P, (ptr)->pprev = NULL_P)
261 #define HLIST_HEAD_INIT CFS_HLIST_HEAD_INIT
262 #define HLIST_HEAD(n) CFS_HLIST_HEAD(n)
263 #define INIT_HLIST_HEAD(p) CFS_INIT_HLIST_HEAD(p)
264 #define INIT_HLIST_NODE(p) CFS_INIT_HLIST_NODE(p)
266 static inline int hlist_unhashed(const struct hlist_node *h)
271 static inline int hlist_empty(const struct hlist_head *h)
276 static inline void __hlist_del(struct hlist_node *n)
278 struct hlist_node *next = n->next;
279 struct hlist_node **pprev = n->pprev;
285 static inline void hlist_del(struct hlist_node *n)
290 static inline void hlist_del_init(struct hlist_node *n)
298 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
300 struct hlist_node *first = h->first;
303 first->pprev = &n->next;
305 n->pprev = &h->first;
308 /* next must be != NULL */
309 static inline void hlist_add_before(struct hlist_node *n,
310 struct hlist_node *next)
312 n->pprev = next->pprev;
314 next->pprev = &n->next;
318 static inline void hlist_add_after(struct hlist_node *n,
319 struct hlist_node *next)
321 next->next = n->next;
323 next->pprev = &n->next;
326 next->next->pprev = &next->next;
329 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
331 #define hlist_for_each(pos, head) \
332 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
335 #define hlist_for_each_safe(pos, n, head) \
336 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
340 * hlist_for_each_entry - iterate over list of given type
341 * @tpos: the type * to use as a loop counter.
342 * @pos: the &struct hlist_node to use as a loop counter.
343 * @head: the head for your list.
344 * @member: the name of the hlist_node within the struct.
346 #define hlist_for_each_entry(tpos, pos, head, member) \
347 for (pos = (head)->first; \
348 pos && ({ prefetch(pos->next); 1;}) && \
349 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
353 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
354 * @tpos: the type * to use as a loop counter.
355 * @pos: the &struct hlist_node to use as a loop counter.
356 * @member: the name of the hlist_node within the struct.
358 #define hlist_for_each_entry_continue(tpos, pos, member) \
359 for (pos = (pos)->next; \
360 pos && ({ prefetch(pos->next); 1;}) && \
361 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
365 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
366 * @tpos: the type * to use as a loop counter.
367 * @pos: the &struct hlist_node to use as a loop counter.
368 * @member: the name of the hlist_node within the struct.
370 #define hlist_for_each_entry_from(tpos, pos, member) \
371 for (; pos && ({ prefetch(pos->next); 1;}) && \
372 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
376 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
377 * @tpos: the type * to use as a loop counter.
378 * @pos: the &struct hlist_node to use as a loop counter.
379 * @n: another &struct hlist_node to use as temporary storage
380 * @head: the head for your list.
381 * @member: the name of the hlist_node within the struct.
383 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
384 for (pos = (head)->first; \
385 pos && ({ n = pos->next; 1; }) && \
386 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
389 #endif /* __linux__ && __KERNEL__ */
391 #ifndef list_for_each_prev
393 * list_for_each_prev - iterate over a list in reverse order
394 * @pos: the &struct list_head to use as a loop counter.
395 * @head: the head for your list.
397 #define list_for_each_prev(pos, head) \
398 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
399 pos = pos->prev, prefetch(pos->prev))
401 #endif /* list_for_each_prev */
403 #ifndef list_for_each_entry
405 * list_for_each_entry - iterate over list of given type
406 * @pos: the type * to use as a loop counter.
407 * @head: the head for your list.
408 * @member: the name of the list_struct within the struct.
410 #define list_for_each_entry(pos, head, member) \
411 for (pos = list_entry((head)->next, typeof(*pos), member), \
412 prefetch(pos->member.next); \
413 &pos->member != (head); \
414 pos = list_entry(pos->member.next, typeof(*pos), member), \
415 prefetch(pos->member.next))
416 #endif /* list_for_each_entry */
418 #ifndef list_for_each_entry_reverse
420 * list_for_each_entry_reverse - iterate backwards 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_reverse(pos, head, member) \
426 for (pos = list_entry((head)->prev, typeof(*pos), member); \
427 prefetch(pos->member.prev), &pos->member != (head); \
428 pos = list_entry(pos->member.prev, typeof(*pos), member))
429 #endif /* list_for_each_entry_reverse */
431 #ifndef list_for_each_entry_safe
433 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
434 * @pos: the type * to use as a loop counter.
435 * @n: another type * to use as temporary storage
436 * @head: the head for your list.
437 * @member: the name of the list_struct within the struct.
439 #define list_for_each_entry_safe(pos, n, head, member) \
440 for (pos = list_entry((head)->next, typeof(*pos), member), \
441 n = list_entry(pos->member.next, typeof(*pos), member); \
442 &pos->member != (head); \
443 pos = n, n = list_entry(n->member.next, typeof(*n), member))
444 #endif /* list_for_each_entry_safe */
446 #ifndef list_for_each_entry_safe_from
448 * list_for_each_entry_safe_from
449 * @pos: the type * to use as a loop cursor.
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 * Iterate over list of given type from current point, safe against
455 * removal of list entry.
457 #define list_for_each_entry_safe_from(pos, n, head, member) \
458 for (n = list_entry(pos->member.next, typeof(*pos), member); \
459 &pos->member != (head); \
460 pos = n, n = list_entry(n->member.next, typeof(*n), member))
461 #endif /* list_for_each_entry_safe_from */
463 #endif /* __LIBCFS_LUSTRE_LIST_H__ */