1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
3 
4 #if defined(__KERNEL__) || defined(_LVM_H_INCLUDE)
5 
6 #include <linux/prefetch.h>
7 
8 /*
9  * Simple doubly linked list implementation.
10  *
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.
16  */
17 
18 struct list_head {
19 	struct list_head *next, *prev;
20 };
21 
22 #define LIST_HEAD_INIT(name) { &(name), &(name) }
23 
24 #define LIST_HEAD(name) \
25 	struct list_head name = LIST_HEAD_INIT(name)
26 
27 #define INIT_LIST_HEAD(ptr) do { \
28 	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
29 } while (0)
30 
31 /*
32  * Insert a new entry between two known consecutive entries.
33  *
34  * This is only for internal list manipulation where we know
35  * the prev/next entries already!
36  */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)37 static inline void __list_add(struct list_head *new,
38 			      struct list_head *prev,
39 			      struct list_head *next)
40 {
41 	next->prev = new;
42 	new->next = next;
43 	new->prev = prev;
44 	prev->next = new;
45 }
46 
47 /**
48  * list_add - add a new entry
49  * @new: new entry to be added
50  * @head: list head to add it after
51  *
52  * Insert a new entry after the specified head.
53  * This is good for implementing stacks.
54  */
list_add(struct list_head * new,struct list_head * head)55 static inline void list_add(struct list_head *new, struct list_head *head)
56 {
57 	__list_add(new, head, head->next);
58 }
59 
60 /**
61  * list_add_tail - add a new entry
62  * @new: new entry to be added
63  * @head: list head to add it before
64  *
65  * Insert a new entry before the specified head.
66  * This is useful for implementing queues.
67  */
list_add_tail(struct list_head * new,struct list_head * head)68 static inline void list_add_tail(struct list_head *new, struct list_head *head)
69 {
70 	__list_add(new, head->prev, head);
71 }
72 
73 /*
74  * Delete a list entry by making the prev/next entries
75  * point to each other.
76  *
77  * This is only for internal list manipulation where we know
78  * the prev/next entries already!
79  */
__list_del(struct list_head * prev,struct list_head * next)80 static inline void __list_del(struct list_head *prev, struct list_head *next)
81 {
82 	next->prev = prev;
83 	prev->next = next;
84 }
85 
86 /**
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.
90  */
list_del(struct list_head * entry)91 static inline void list_del(struct list_head *entry)
92 {
93 	__list_del(entry->prev, entry->next);
94 	entry->next = (void *) 0;
95 	entry->prev = (void *) 0;
96 }
97 
98 /**
99  * list_del_init - deletes entry from list and reinitialize it.
100  * @entry: the element to delete from the list.
101  */
list_del_init(struct list_head * entry)102 static inline void list_del_init(struct list_head *entry)
103 {
104 	__list_del(entry->prev, entry->next);
105 	INIT_LIST_HEAD(entry);
106 }
107 
108 /**
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
112  */
list_move(struct list_head * list,struct list_head * head)113 static inline void list_move(struct list_head *list, struct list_head *head)
114 {
115         __list_del(list->prev, list->next);
116         list_add(list, head);
117 }
118 
119 /**
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
123  */
list_move_tail(struct list_head * list,struct list_head * head)124 static inline void list_move_tail(struct list_head *list,
125 				  struct list_head *head)
126 {
127         __list_del(list->prev, list->next);
128         list_add_tail(list, head);
129 }
130 
131 /**
132  * list_empty - tests whether a list is empty
133  * @head: the list to test.
134  */
list_empty(struct list_head * head)135 static inline int list_empty(struct list_head *head)
136 {
137 	return head->next == head;
138 }
139 
__list_splice(struct list_head * list,struct list_head * head)140 static inline void __list_splice(struct list_head *list,
141 				 struct list_head *head)
142 {
143 	struct list_head *first = list->next;
144 	struct list_head *last = list->prev;
145 	struct list_head *at = head->next;
146 
147 	first->prev = head;
148 	head->next = first;
149 
150 	last->next = at;
151 	at->prev = last;
152 }
153 
154 /**
155  * list_splice - join two lists
156  * @list: the new list to add.
157  * @head: the place to add it in the first list.
158  */
list_splice(struct list_head * list,struct list_head * head)159 static inline void list_splice(struct list_head *list, struct list_head *head)
160 {
161 	if (!list_empty(list))
162 		__list_splice(list, head);
163 }
164 
165 /**
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.
169  *
170  * The list at @list is reinitialised
171  */
list_splice_init(struct list_head * list,struct list_head * head)172 static inline void list_splice_init(struct list_head *list,
173 				    struct list_head *head)
174 {
175 	if (!list_empty(list)) {
176 		__list_splice(list, head);
177 		INIT_LIST_HEAD(list);
178 	}
179 }
180 
181 /**
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.
186  */
187 #define list_entry(ptr, type, member) \
188 	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
189 
190 /**
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.
194  */
195 #define list_for_each(pos, head) \
196 	for (pos = (head)->next, prefetch(pos->next); pos != (head); \
197         	pos = pos->next, prefetch(pos->next))
198 /**
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.
202  */
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))
206 
207 /**
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.
212  */
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)
216 
217 /**
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.
222  */
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))
229 
230 /**
231  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
232  * @pos:	the type * to use as a loop counter.
233  * @n:		another type * to use as temporary storage
234  * @head:	the head for your list.
235  * @member:	the name of the list_struct within the struct.
236  */
237 #define list_for_each_entry_safe(pos, n, head, member)			\
238 	for (pos = list_entry((head)->next, typeof(*pos), member),	\
239 		n = list_entry(pos->member.next, typeof(*pos), member);	\
240 	     &pos->member != (head); 					\
241 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
242 
243 /**
244  * list_for_each_entry_continue -       iterate over list of given type
245  *                      continuing after existing point
246  * @pos:        the type * to use as a loop counter.
247  * @head:       the head for your list.
248  * @member:     the name of the list_struct within the struct.
249  */
250 #define list_for_each_entry_continue(pos, head, member)			\
251 	for (pos = list_entry(pos->member.next, typeof(*pos), member),	\
252 		     prefetch(pos->member.next);			\
253 	     &pos->member != (head);					\
254 	     pos = list_entry(pos->member.next, typeof(*pos), member),	\
255 		     prefetch(pos->member.next))
256 
257 #endif /* __KERNEL__ || _LVM_H_INCLUDE */
258 
259 #endif
260