1 #ifndef __LINUX_LIST_H__
2 #define __LINUX_LIST_H__
3 
4 /*
5  * Simple doubly linked list implementation.
6  *
7  * Some of the internal functions ("__xxx") are useful when
8  * manipulating whole lists rather than single entries, as
9  * sometimes we already know the next/prev entries and we can
10  * generate better code by using them directly rather than
11  * using the generic single-entry routines.
12  */
13 
14 #define prefetch(a) ((void *)a)
15 
16 struct list_head {
17     struct list_head *next, *prev;
18 };
19 
20 #define LIST_HEAD_INIT(name) { &(name), &(name) }
21 
22 #define LIST_HEAD(name) \
23 	struct list_head name = LIST_HEAD_INIT(name)
24 
25 static inline void INIT_LIST_HEAD(struct list_head *list)
26 {
27     list->next = list;
28     list->prev = list;
29 }
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  */
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  */
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  */
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  */
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  */
91 static inline void list_del(struct list_head *entry)
92 {
93     __list_del(entry->prev, entry->next);
94 }
95 
96 /**
97  * list_del_init - deletes entry from list and reinitialize it.
98  * @entry: the element to delete from the list.
99  */
100 static inline void list_del_init(struct list_head *entry)
101 {
102     __list_del(entry->prev, entry->next);
103     INIT_LIST_HEAD(entry);
104 }
105 
106 /**
107  * list_move - delete from one list and add as another's head
108  * @list: the entry to move
109  * @head: the head that will precede our entry
110  */
111 static inline void list_move(struct list_head *list, struct list_head *head)
112 {
113     __list_del(list->prev, list->next);
114     list_add(list, head);
115 }
116 
117 /**
118  * list_move_tail - delete from one list and add as another's tail
119  * @list: the entry to move
120  * @head: the head that will follow our entry
121  */
122 static inline void list_move_tail(struct list_head *list,
123                                   struct list_head *head)
124 {
125     __list_del(list->prev, list->next);
126     list_add_tail(list, head);
127 }
128 
129 /**
130  * list_empty - tests whether a list is empty
131  * @head: the list to test.
132  */
133 static inline int list_empty(struct list_head *head)
134 {
135     return head->next == head;
136 }
137 
138 static inline int list_empty_careful(const struct list_head *head)
139 {
140     struct list_head *next = head->next;
141     return (next == head) && (next == head->prev);
142 }
143 
144 static inline void __list_splice(struct list_head *list,
145                                  struct list_head *head)
146 {
147     struct list_head *first = list->next;
148     struct list_head *last = list->prev;
149     struct list_head *at = head->next;
150 
151     first->prev = head;
152     head->next = first;
153 
154     last->next = at;
155     at->prev = last;
156 }
157 
158 /**
159  * list_splice - join two lists
160  * @list: the new list to add.
161  * @head: the place to add it in the first list.
162  */
163 static inline void list_splice(struct list_head *list, struct list_head *head)
164 {
165     if (!list_empty(list))
166         __list_splice(list, head);
167 }
168 
169 /**
170  * list_splice_init - join two lists and reinitialise the emptied list.
171  * @list: the new list to add.
172  * @head: the place to add it in the first list.
173  *
174  * The list at @list is reinitialised
175  */
176 static inline void list_splice_init(struct list_head *list,
177                                     struct list_head *head)
178 {
179     if (!list_empty(list)) {
180         __list_splice(list, head);
181         INIT_LIST_HEAD(list);
182     }
183 }
184 
185 /**
186  * list_entry - get the struct for this entry
187  * @ptr:	the &struct list_head pointer.
188  * @type:	the type of the struct this is embedded in.
189  * @member:	the name of the list_struct within the struct.
190  */
191 #define list_entry(ptr, type, member) \
192 	((type *)((char *)(ptr)-(char *)(&((type *)0)->member)))
193 
194 /**
195  * list_for_each	-	iterate over a list
196  * @pos:	the &struct list_head to use as a loop counter.
197  * @head:	the head for your list.
198  */
199 #define list_for_each(pos, head) \
200 	for (pos = (head)->next, prefetch(pos->next); pos != (head); \
201 		pos = pos->next, prefetch(pos->next))
202 
203 /**
204  * list_for_each_safe	-	iterate over a list safe against removal of list entry
205  * @pos:	the &struct list_head to use as a loop counter.
206  * @n:		another &struct list_head to use as temporary storage
207  * @head:	the head for your list.
208  */
209 #define list_for_each_safe(pos, n, head) \
210 	for (pos = (head)->next, n = pos->next; pos != (head); \
211 		pos = n, n = pos->next)
212 
213 #ifndef list_for_each_prev
214 /**
215  * list_for_each_prev	-	iterate over a list in reverse order
216  * @pos:	the &struct list_head to use as a loop counter.
217  * @head:	the head for your list.
218  */
219 #define list_for_each_prev(pos, head) \
220 	for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
221 		pos = pos->prev, prefetch(pos->prev))
222 
223 #endif /* list_for_each_prev */
224 
225 #ifndef list_for_each_entry
226 /**
227  * list_for_each_entry  -       iterate over list of given type
228  * @pos:        the type * to use as a loop counter.
229  * @head:       the head for your list.
230  * @member:     the name of the list_struct within the struct.
231  */
232 #define list_for_each_entry(pos, head, type, member)		\
233         for (pos = list_entry((head)->next, type, member),	\
234 		     prefetch(pos->member.next);			\
235 	     &pos->member != (head);					\
236 	     pos = list_entry(pos->member.next, type, member),	\
237 	     prefetch(pos->member.next))
238 #endif /* list_for_each_entry */
239 
240 #ifndef list_for_each_entry_safe
241 /**
242  * list_for_each_entry_safe  -       iterate over list of given type safe against removal of list entry
243  * @pos:        the type * to use as a loop counter.
244  * @n:          another type * to use as temporary storage
245  * @head:       the head for your list.
246  * @member:     the name of the list_struct within the struct.
247  */
248 #define list_for_each_entry_safe(pos, n, head, type, member)			\
249         for (pos = list_entry((head)->next, type, member),	\
250 		n = list_entry(pos->member.next, type, member);	\
251 	     &pos->member != (head);					\
252 	     pos = n, n = list_entry(n->member.next, type, member))
253 #endif /* list_for_each_entry_safe */
254 
255 #endif /* __LINUX_LIST_H__ */
256