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 = LIST_HEAD_INIT(name)
48 #define CFS_INIT_LIST_HEAD(ptr) do { \
49 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
53 #define LIST_HEAD(n) CFS_LIST_HEAD(n)
56 #define LIST_HEAD_INIT(n) CFS_LIST_HEAD_INIT(n)
57 #define INIT_LIST_HEAD(p) CFS_INIT_LIST_HEAD(p)
60 * Insert a new entry between two known consecutive entries.
62 * This is only for internal list manipulation where we know
63 * the prev/next entries already!
65 static inline void __list_add(struct list_head * new,
66 struct list_head * prev,
67 struct list_head * next)
76 * list_add - add a new entry
77 * @new: new entry to be added
78 * @head: list head to add it after
80 * Insert a new entry after the specified head.
81 * This is good for implementing stacks.
83 static inline void list_add(struct list_head *new, struct list_head *head)
85 __list_add(new, head, head->next);
89 * list_add_tail - add a new entry
90 * @new: new entry to be added
91 * @head: list head to add it before
93 * Insert a new entry before the specified head.
94 * This is useful for implementing queues.
96 static inline void list_add_tail(struct list_head *new, struct list_head *head)
98 __list_add(new, head->prev, head);
102 * Delete a list entry by making the prev/next entries
103 * point to each other.
105 * This is only for internal list manipulation where we know
106 * the prev/next entries already!
108 static inline void __list_del(struct list_head * prev, struct list_head * next)
115 * list_del - deletes entry from list.
116 * @entry: the element to delete from the list.
117 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
119 static inline void list_del(struct list_head *entry)
121 __list_del(entry->prev, entry->next);
125 * list_del_init - deletes entry from list and reinitialize it.
126 * @entry: the element to delete from the list.
128 static inline void list_del_init(struct list_head *entry)
130 __list_del(entry->prev, entry->next);
131 CFS_INIT_LIST_HEAD(entry);
135 * list_move - delete from one list and add as another's head
136 * @list: the entry to move
137 * @head: the head that will precede our entry
139 * This is not safe to use if @list is already on the same list as @head.
141 static inline void list_move(struct list_head *list, struct list_head *head)
143 __list_del(list->prev, list->next);
144 list_add(list, head);
148 * list_move_tail - delete from one list and add as another's tail
149 * @list: the entry to move
150 * @head: the head that will follow our entry
152 * This is not safe to use if @list is already on the same list as @head.
154 static inline void list_move_tail(struct list_head *list,
155 struct list_head *head)
157 __list_del(list->prev, list->next);
158 list_add_tail(list, head);
162 * list_empty - tests whether a list is empty
163 * @head: the list to test.
165 static inline int list_empty(struct list_head *head)
167 return head->next == head;
170 static inline void __list_splice(struct list_head *list,
171 struct list_head *head)
173 struct list_head *first = list->next;
174 struct list_head *last = list->prev;
175 struct list_head *at = head->next;
185 * list_splice - join two lists
186 * @list: the new list to add.
187 * @head: the place to add it in the first list.
189 static inline void list_splice(struct list_head *list, struct list_head *head)
191 if (!list_empty(list))
192 __list_splice(list, head);
196 * list_splice_init - join two lists and reinitialise the emptied list.
197 * @list: the new list to add.
198 * @head: the place to add it in the first list.
200 * The list at @list is reinitialised
202 static inline void list_splice_init(struct list_head *list,
203 struct list_head *head)
205 if (!list_empty(list)) {
206 __list_splice(list, head);
207 CFS_INIT_LIST_HEAD(list);
212 * list_entry - get the struct for this entry
213 * @ptr: the &struct list_head pointer.
214 * @type: the type of the struct this is embedded in.
215 * @member: the name of the list_struct within the struct.
217 #define list_entry(ptr, type, member) \
218 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
221 * list_for_each - iterate over a list
222 * @pos: the &struct list_head to use as a loop counter.
223 * @head: the head for your list.
225 #define list_for_each(pos, head) \
226 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
227 pos = pos->next, prefetch(pos->next))
230 * list_for_each_safe - iterate over a list safe against removal of list entry
231 * @pos: the &struct list_head to use as a loop counter.
232 * @n: another &struct list_head to use as temporary storage
233 * @head: the head for your list.
235 #define list_for_each_safe(pos, n, head) \
236 for (pos = (head)->next, n = pos->next; pos != (head); \
237 pos = n, n = pos->next)
240 * Double linked lists with a single pointer list head.
241 * Mostly useful for hash tables where the two pointer list head is
243 * You lose the ability to access the tail in O(1).
247 struct hlist_node *first;
251 struct hlist_node *next, **pprev;
255 * "NULL" might not be defined at this point
260 #define NULL_P ((void *)0)
263 #define CFS_HLIST_HEAD_INIT { .first = NULL_P }
264 #define CFS_HLIST_HEAD(name) struct hlist_head name = { .first = NULL_P }
265 #define CFS_INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL_P)
266 #define CFS_INIT_HLIST_NODE(ptr) ((ptr)->next = NULL_P, (ptr)->pprev = NULL_P)
268 #define HLIST_HEAD_INIT CFS_HLIST_HEAD_INIT
269 #define HLIST_HEAD(n) CFS_HLIST_HEAD(n)
270 #define INIT_HLIST_HEAD(p) CFS_INIT_HLIST_HEAD(p)
271 #define INIT_HLIST_NODE(p) CFS_INIT_HLIST_NODE(p)
273 static inline int hlist_unhashed(const struct hlist_node *h)
278 static inline int hlist_empty(const struct hlist_head *h)
283 static inline void __hlist_del(struct hlist_node *n)
285 struct hlist_node *next = n->next;
286 struct hlist_node **pprev = n->pprev;
292 static inline void hlist_del(struct hlist_node *n)
297 static inline void hlist_del_init(struct hlist_node *n)
305 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
307 struct hlist_node *first = h->first;
310 first->pprev = &n->next;
312 n->pprev = &h->first;
315 /* next must be != NULL */
316 static inline void hlist_add_before(struct hlist_node *n,
317 struct hlist_node *next)
319 n->pprev = next->pprev;
321 next->pprev = &n->next;
325 static inline void hlist_add_after(struct hlist_node *n,
326 struct hlist_node *next)
328 next->next = n->next;
330 next->pprev = &n->next;
333 next->next->pprev = &next->next;
336 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
338 #define hlist_for_each(pos, head) \
339 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
342 #define hlist_for_each_safe(pos, n, head) \
343 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
347 * hlist_for_each_entry - iterate over list of given type
348 * @tpos: the type * to use as a loop counter.
349 * @pos: the &struct hlist_node to use as a loop counter.
350 * @head: the head for your list.
351 * @member: the name of the hlist_node within the struct.
353 #define hlist_for_each_entry(tpos, pos, head, member) \
354 for (pos = (head)->first; \
355 pos && ({ prefetch(pos->next); 1;}) && \
356 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
360 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
361 * @tpos: the type * to use as a loop counter.
362 * @pos: the &struct hlist_node to use as a loop counter.
363 * @member: the name of the hlist_node within the struct.
365 #define hlist_for_each_entry_continue(tpos, pos, member) \
366 for (pos = (pos)->next; \
367 pos && ({ prefetch(pos->next); 1;}) && \
368 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
372 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
373 * @tpos: the type * to use as a loop counter.
374 * @pos: the &struct hlist_node to use as a loop counter.
375 * @member: the name of the hlist_node within the struct.
377 #define hlist_for_each_entry_from(tpos, pos, member) \
378 for (; pos && ({ prefetch(pos->next); 1;}) && \
379 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
383 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
384 * @tpos: the type * to use as a loop counter.
385 * @pos: the &struct hlist_node to use as a loop counter.
386 * @n: another &struct hlist_node to use as temporary storage
387 * @head: the head for your list.
388 * @member: the name of the hlist_node within the struct.
390 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
391 for (pos = (head)->first; \
392 pos && ({ n = pos->next; 1; }) && \
393 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
396 #endif /* __linux__ && __KERNEL__ */
398 #ifndef list_for_each_prev
400 * list_for_each_prev - iterate over a list in reverse order
401 * @pos: the &struct list_head to use as a loop counter.
402 * @head: the head for your list.
404 #define list_for_each_prev(pos, head) \
405 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
406 pos = pos->prev, prefetch(pos->prev))
408 #endif /* list_for_each_prev */
410 #ifndef list_for_each_entry
412 * list_for_each_entry - iterate over list of given type
413 * @pos: the type * to use as a loop counter.
414 * @head: the head for your list.
415 * @member: the name of the list_struct within the struct.
417 #define list_for_each_entry(pos, head, member) \
418 for (pos = list_entry((head)->next, typeof(*pos), member), \
419 prefetch(pos->member.next); \
420 &pos->member != (head); \
421 pos = list_entry(pos->member.next, typeof(*pos), member), \
422 prefetch(pos->member.next))
423 #endif /* list_for_each_entry */
425 #ifndef list_for_each_entry_reverse
427 * list_for_each_entry_reverse - iterate backwards over list of given type.
428 * @pos: the type * to use as a loop counter.
429 * @head: the head for your list.
430 * @member: the name of the list_struct within the struct.
432 #define list_for_each_entry_reverse(pos, head, member) \
433 for (pos = list_entry((head)->prev, typeof(*pos), member); \
434 prefetch(pos->member.prev), &pos->member != (head); \
435 pos = list_entry(pos->member.prev, typeof(*pos), member))
436 #endif /* list_for_each_entry_reverse */
438 #ifndef list_for_each_entry_safe
440 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
441 * @pos: the type * to use as a loop counter.
442 * @n: another type * to use as temporary storage
443 * @head: the head for your list.
444 * @member: the name of the list_struct within the struct.
446 #define list_for_each_entry_safe(pos, n, head, member) \
447 for (pos = list_entry((head)->next, typeof(*pos), member), \
448 n = list_entry(pos->member.next, typeof(*pos), member); \
449 &pos->member != (head); \
450 pos = n, n = list_entry(n->member.next, typeof(*n), member))
451 #endif /* list_for_each_entry_safe */
453 #endif /* __LIBCFS_LUSTRE_LIST_H__ */