1 #ifndef __LIST_H__
2 #define __LIST_H__
3 
4 /* GCS: I ripped this off from the linux kernel.  Minor modifications. */
5 
6 #if defined (WIN32)
7 #define inline __inline
8 #endif
9 
10 /* #define prefetch(a) (a) */
11 #define prefetch(a) 0
12 
13 /*
14  * Simple doubly linked list implementation.
15  *
16  * Some of the internal functions ("__xxx") are useful when
17  * manipulating whole lists rather than single entries, as
18  * sometimes we already know the next/prev entries and we can
19  * generate better code by using them directly rather than
20  * using the generic single-entry routines.
21  */
22 
23 struct list_head {
24 	struct list_head *next, *prev;
25 };
26 typedef struct list_head LIST;
27 
28 
29 #define LIST_HEAD_INIT(name) { &(name), &(name) }
30 
31 #define LIST_HEAD(name) \
32 	struct list_head name = LIST_HEAD_INIT(name)
33 
34 #define INIT_LIST_HEAD(ptr) do { \
35 	(ptr)->next = (ptr); (ptr)->prev = (ptr); \
36 } while (0)
37 
38 /*
39  * Insert a new entry between two known consecutive entries.
40  *
41  * This is only for internal list manipulation where we know
42  * the prev/next entries already!
43  */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)44 static inline void __list_add(struct list_head *new,
45 			      struct list_head *prev,
46 			      struct list_head *next)
47 {
48 	next->prev = new;
49 	new->next = next;
50 	new->prev = prev;
51 	prev->next = new;
52 }
53 
54 /**
55  * list_add - add a new entry
56  * @new: new entry to be added
57  * @head: list head to add it after
58  *
59  * Insert a new entry after the specified head.
60  * This is good for implementing stacks.
61  */
list_add(struct list_head * new,struct list_head * head)62 static inline void list_add(struct list_head *new, struct list_head *head)
63 {
64 	__list_add(new, head, head->next);
65 }
66 
67 /**
68  * list_add_tail - add a new entry
69  * @new: new entry to be added
70  * @head: list head to add it before
71  *
72  * Insert a new entry before the specified head.
73  * This is useful for implementing queues.
74  */
list_add_tail(struct list_head * new,struct list_head * head)75 static inline void list_add_tail(struct list_head *new, struct list_head *head)
76 {
77 	__list_add(new, head->prev, head);
78 }
79 
80 /*
81  * Delete a list entry by making the prev/next entries
82  * point to each other.
83  *
84  * This is only for internal list manipulation where we know
85  * the prev/next entries already!
86  */
__list_del(struct list_head * prev,struct list_head * next)87 static inline void __list_del(struct list_head *prev, struct list_head *next)
88 {
89 	next->prev = prev;
90 	prev->next = next;
91 }
92 
93 /**
94  * list_del - deletes entry from list.
95  * @entry: the element to delete from the list.
96  * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
97  */
list_del(struct list_head * entry)98 static inline void list_del(struct list_head *entry)
99 {
100 	__list_del(entry->prev, entry->next);
101 	entry->next = (void *) 0;
102 	entry->prev = (void *) 0;
103 }
104 
105 /**
106  * list_del_init - deletes entry from list and reinitialize it.
107  * @entry: the element to delete from the list.
108  */
list_del_init(struct list_head * entry)109 static inline void list_del_init(struct list_head *entry)
110 {
111 	__list_del(entry->prev, entry->next);
112 	INIT_LIST_HEAD(entry);
113 }
114 
115 /**
116  * list_move - delete from one list and add as another's head
117  * @list: the entry to move
118  * @head: the head that will precede our entry
119  */
list_move(struct list_head * list,struct list_head * head)120 static inline void list_move(struct list_head *list, struct list_head *head)
121 {
122         __list_del(list->prev, list->next);
123         list_add(list, head);
124 }
125 
126 /**
127  * list_move_tail - delete from one list and add as another's tail
128  * @list: the entry to move
129  * @head: the head that will follow our entry
130  */
list_move_tail(struct list_head * list,struct list_head * head)131 static inline void list_move_tail(struct list_head *list,
132 				  struct list_head *head)
133 {
134         __list_del(list->prev, list->next);
135         list_add_tail(list, head);
136 }
137 
138 /**
139  * list_empty - tests whether a list is empty
140  * @head: the list to test.
141  */
list_empty(struct list_head * head)142 static inline int list_empty(struct list_head *head)
143 {
144 	return head->next == head;
145 }
146 
__list_splice(struct list_head * list,struct list_head * head)147 static inline void __list_splice(struct list_head *list,
148 				 struct list_head *head)
149 {
150 	struct list_head *first = list->next;
151 	struct list_head *last = list->prev;
152 	struct list_head *at = head->next;
153 
154 	first->prev = head;
155 	head->next = first;
156 
157 	last->next = at;
158 	at->prev = last;
159 }
160 
161 /**
162  * list_splice - join two lists
163  * @list: the new list to add.
164  * @head: the place to add it in the first list.
165  */
list_splice(struct list_head * list,struct list_head * head)166 static inline void list_splice(struct list_head *list, struct list_head *head)
167 {
168 	if (!list_empty(list))
169 		__list_splice(list, head);
170 }
171 
172 /**
173  * list_splice_init - join two lists and reinitialise the emptied list.
174  * @list: the new list to add.
175  * @head: the place to add it in the first list.
176  *
177  * The list at @list is reinitialised
178  */
list_splice_init(struct list_head * list,struct list_head * head)179 static inline void list_splice_init(struct list_head *list,
180 				    struct list_head *head)
181 {
182 	if (!list_empty(list)) {
183 		__list_splice(list, head);
184 		INIT_LIST_HEAD(list);
185 	}
186 }
187 
188 /**
189  * list_entry - get the struct for this entry
190  * @ptr:	the &struct list_head pointer.
191  * @type:	the type of the struct this is embedded in.
192  * @member:	the name of the list_struct within the struct.
193  */
194 #define list_entry(ptr, type, member) \
195 	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
196 
197 /**
198  * list_for_each	-	iterate over a list
199  * @pos:	the &struct list_head to use as a loop counter.
200  * @head:	the head for your list.
201  */
202 #define list_for_each(pos, head) \
203 	for (pos = (head)->next, prefetch(pos->next); pos != (head); \
204         	pos = pos->next, prefetch(pos->next))
205 /**
206  * list_for_each_prev	-	iterate over a list backwards
207  * @pos:	the &struct list_head to use as a loop counter.
208  * @head:	the head for your list.
209  */
210 #define list_for_each_prev(pos, head) \
211 	for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
212         	pos = pos->prev, prefetch(pos->prev))
213 
214 /**
215  * list_for_each_safe	-	iterate over a list safe against removal of list entry
216  * @pos:	the &struct list_head to use as a loop counter.
217  * @n:		another &struct list_head to use as temporary storage
218  * @head:	the head for your list.
219  */
220 #define list_for_each_safe(pos, n, head) \
221 	for (pos = (head)->next, n = pos->next; pos != (head); \
222 		pos = n, n = pos->next)
223 
224 /**
225  * list_for_each_entry	-	iterate over list of given type
226  * @pos:	the type * to use as a loop counter.
227  * @type:	the type of the struct this is embedded in.
228  * @head:	the head for your list.
229  * @member:	the name of the list_struct within the struct.
230  */
231 #if defined (commentout)
232 #define list_for_each_entry(pos, head, member)				\
233 	for (pos = list_entry((head)->next, typeof(*pos), member),	\
234 		     prefetch(pos->member.next);			\
235 	     &pos->member != (head); 					\
236 	     pos = list_entry(pos->member.next, typeof(*pos), member),	\
237 		     prefetch(pos->member.next))
238 #endif
239 #define list_for_each_entry(pos, type, head, member)				\
240 	for (pos = list_entry((head)->next, type, member),	\
241 		     prefetch(pos->member.next);			\
242 	     &pos->member != (head); 					\
243 	     pos = list_entry(pos->member.next, type, member),	\
244 		     prefetch(pos->member.next))
245 
246 /**
247  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
248  * @pos:	the type * to use as a loop counter.
249  * @type:	the type of the struct this is embedded in.
250  * @n:		another type * to use as temporary storage
251  * @head:	the head for your list.
252  * @member:	the name of the list_struct within the struct.
253  */
254 #if defined (commentout)
255 #define list_for_each_entry_safe(pos, n, head, member)			\
256 	for (pos = list_entry((head)->next, typeof(*pos), member),	\
257 		n = list_entry(pos->member.next, typeof(*pos), member);	\
258 	     &pos->member != (head); 					\
259 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
260 #endif
261 #define list_for_each_entry_safe(pos, type, n, head, member)			\
262 	for (pos = list_entry((head)->next, type, member),	\
263 		n = list_entry(pos->member.next, type, member);	\
264 	     &pos->member != (head); 					\
265 	     pos = n, n = list_entry(n->member.next, type, member))
266 
267 /**
268  * list_for_each_entry_continue -       iterate over list of given type
269  *                      continuing after existing point
270  * @pos:        the type * to use as a loop counter.
271  * @head:       the head for your list.
272  * @member:     the name of the list_struct within the struct.
273  */
274 #define list_for_each_entry_continue(pos, head, member)			\
275 	for (pos = list_entry(pos->member.next, typeof(*pos), member),	\
276 		     prefetch(pos->member.next);			\
277 	     &pos->member != (head);					\
278 	     pos = list_entry(pos->member.next, typeof(*pos), member),	\
279 		     prefetch(pos->member.next))
280 
281 #endif
282