4 #if defined(__KERNEL__) || defined(_LVM_H_INCLUDE)
6 #include <linux/prefetch.h>
9 * Simple doubly linked list implementation.
11 * Some of the internal functions ("__xxx") are useful when
12 * manipulating whole lists rather than single entries, as
13 * sometimes we already know the next/prev entries and we can
14 * generate better code by using them directly rather than
15 * using the generic single-entry routines.
19 struct list_head *next, *prev;
22 #define LIST_HEAD_INIT(name) { &(name), &(name) }
24 #define LIST_HEAD(name) \
25 struct list_head name = LIST_HEAD_INIT(name)
27 #define INIT_LIST_HEAD(ptr) do { \
28 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
32 * Insert a new entry between two known consecutive entries.
34 * This is only for internal list manipulation where we know
35 * the prev/next entries already!
37 static inline void __list_add(struct list_head *new,
38 struct list_head *prev,
39 struct list_head *next)
48 * list_add - add a new entry
49 * @new: new entry to be added
50 * @head: list head to add it after
52 * Insert a new entry after the specified head.
53 * This is good for implementing stacks.
55 static inline void list_add(struct list_head *new, struct list_head *head)
57 __list_add(new, head, head->next);
61 * list_add_tail - add a new entry
62 * @new: new entry to be added
63 * @head: list head to add it before
65 * Insert a new entry before the specified head.
66 * This is useful for implementing queues.
68 static inline void list_add_tail(struct list_head *new, struct list_head *head)
70 __list_add(new, head->prev, head);
74 * Delete a list entry by making the prev/next entries
75 * point to each other.
77 * This is only for internal list manipulation where we know
78 * the prev/next entries already!
80 static inline void __list_del(struct list_head *prev, struct list_head *next)
87 * list_del - deletes entry from list.
88 * @entry: the element to delete from the list.
89 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
91 static inline void list_del(struct list_head *entry)
93 __list_del(entry->prev, entry->next);
94 entry->next = (void *) 0;
95 entry->prev = (void *) 0;
99 * list_del_init - deletes entry from list and reinitialize it.
100 * @entry: the element to delete from the list.
102 static inline void list_del_init(struct list_head *entry)
104 __list_del(entry->prev, entry->next);
105 INIT_LIST_HEAD(entry);
109 * list_move - delete from one list and add as another's head
110 * @list: the entry to move
111 * @head: the head that will precede our entry
113 static inline void list_move(struct list_head *list, struct list_head *head)
115 __list_del(list->prev, list->next);
116 list_add(list, head);
120 * list_move_tail - delete from one list and add as another's tail
121 * @list: the entry to move
122 * @head: the head that will follow our entry
124 static inline void list_move_tail(struct list_head *list,
125 struct list_head *head)
127 __list_del(list->prev, list->next);
128 list_add_tail(list, head);
132 * list_empty - tests whether a list is empty
133 * @head: the list to test.
135 static inline int list_empty(struct list_head *head)
137 return head->next == head;
140 static inline void __list_splice(struct list_head *list,
141 struct list_head *head)
143 struct list_head *first = list->next;
144 struct list_head *last = list->prev;
145 struct list_head *at = head->next;
155 * list_splice - join two lists
156 * @list: the new list to add.
157 * @head: the place to add it in the first list.
159 static inline void list_splice(struct list_head *list, struct list_head *head)
161 if (!list_empty(list))
162 __list_splice(list, head);
166 * list_splice_init - join two lists and reinitialise the emptied list.
167 * @list: the new list to add.
168 * @head: the place to add it in the first list.
170 * The list at @list is reinitialised
172 static inline void list_splice_init(struct list_head *list,
173 struct list_head *head)
175 if (!list_empty(list)) {
176 __list_splice(list, head);
177 INIT_LIST_HEAD(list);
182 * list_entry - get the struct for this entry
183 * @ptr: the &struct list_head pointer.
184 * @type: the type of the struct this is embedded in.
185 * @member: the name of the list_struct within the struct.
187 #define list_entry(ptr, type, member) \
188 ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
191 * list_for_each - iterate over a list
192 * @pos: the &struct list_head to use as a loop counter.
193 * @head: the head for your list.
195 #define list_for_each(pos, head) \
196 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
197 pos = pos->next, prefetch(pos->next))
199 * list_for_each_prev - iterate over a list backwards
200 * @pos: the &struct list_head to use as a loop counter.
201 * @head: the head for your list.
203 #define list_for_each_prev(pos, head) \
204 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
205 pos = pos->prev, prefetch(pos->prev))
208 * list_for_each_safe - iterate over a list safe against removal of list entry
209 * @pos: the &struct list_head to use as a loop counter.
210 * @n: another &struct list_head to use as temporary storage
211 * @head: the head for your list.
213 #define list_for_each_safe(pos, n, head) \
214 for (pos = (head)->next, n = pos->next; pos != (head); \
215 pos = n, n = pos->next)
218 * list_for_each_entry - iterate over list of given type
219 * @pos: the type * to use as a loop counter.
220 * @head: the head for your list.
221 * @member: the name of the list_struct within the struct.
223 #define list_for_each_entry(pos, head, member) \
224 for (pos = list_entry((head)->next, typeof(*pos), member), \
225 prefetch(pos->member.next); \
226 &pos->member != (head); \
227 pos = list_entry(pos->member.next, typeof(*pos), member), \
228 prefetch(pos->member.next))
230 #endif /* __KERNEL__ || _LVM_H_INCLUDE */