1 /* Stripped down version of the Linux 2.6.30 list.h */
2 
3 #include <stddef.h>
4 
5 /*
6  * These are non-NULL pointers that will result in page faults
7  * under normal circumstances, used to verify that nobody uses
8  * non-initialized list entries.
9  */
10 #define LIST_POISON1  ((void *) 0x00100100)
11 #define LIST_POISON2  ((void *) 0x00200200)
12 
13 /**
14  * container_of - cast a member of a structure out to the containing structure
15  * @ptr:	the pointer to the member.
16  * @type:	the type of the container struct this is embedded in.
17  * @member:	the name of the member within the struct.
18  *
19  */
20 #define container_of(ptr, type, member) ({			\
21 	const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
22 	(type *)( (char *)__mptr - offsetof(type,member) );})
23 
24 struct list_head {
25 	struct list_head *next, *prev;
26 };
27 
28 #define LIST_HEAD_INIT(name) { &(name), &(name) }
29 
30 #define LIST_HEAD(name) \
31 	struct list_head name = LIST_HEAD_INIT(name)
32 
INIT_LIST_HEAD(struct list_head * list)33 static inline void INIT_LIST_HEAD(struct list_head *list)
34 {
35 	list->next = list;
36 	list->prev = list;
37 }
38 
39 /*
40  * Insert a new entry between two known consecutive entries.
41  *
42  * This is only for internal list manipulation where we know
43  * the prev/next entries already!
44  */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)45 static inline void __list_add(struct list_head *new,
46 			      struct list_head *prev,
47 			      struct list_head *next)
48 {
49 	next->prev = new;
50 	new->next = next;
51 	new->prev = prev;
52 	prev->next = new;
53 }
54 
55 /**
56  * list_add - add a new entry
57  * @new: new entry to be added
58  * @head: list head to add it after
59  *
60  * Insert a new entry after the specified head.
61  * This is good for implementing stacks.
62  */
list_add(struct list_head * new,struct list_head * head)63 static inline void list_add(struct list_head *new, struct list_head *head)
64 {
65 	__list_add(new, head, head->next);
66 }
67 
68 
69 /**
70  * list_add_tail - add a new entry
71  * @new: new entry to be added
72  * @head: list head to add it before
73  *
74  * Insert a new entry before the specified head.
75  * This is useful for implementing queues.
76  */
list_add_tail(struct list_head * new,struct list_head * head)77 static inline void list_add_tail(struct list_head *new, struct list_head *head)
78 {
79 	__list_add(new, head->prev, head);
80 }
81 
82 /*
83  * Delete a list entry by making the prev/next entries
84  * point to each other.
85  *
86  * This is only for internal list manipulation where we know
87  * the prev/next entries already!
88  */
__list_del(struct list_head * prev,struct list_head * next)89 static inline void __list_del(struct list_head * prev, struct list_head * next)
90 {
91 	next->prev = prev;
92 	prev->next = next;
93 }
94 
list_del(struct list_head * entry)95 static inline void list_del(struct list_head *entry)
96 {
97 	__list_del(entry->prev, entry->next);
98 	entry->next = LIST_POISON1;
99 	entry->prev = LIST_POISON2;
100 }
101 
102 /**
103  * list_is_last - tests whether @list is the last entry in list @head
104  * @list: the entry to test
105  * @head: the head of the list
106  */
list_is_last(const struct list_head * list,const struct list_head * head)107 static inline int list_is_last(const struct list_head *list,
108 				const struct list_head *head)
109 {
110 	return list->next == head;
111 }
112 
113 /**
114  * list_is_first - tests whether @list is the first entry in list @head
115  * @list: the entry to test
116  * @head: the head of the list
117  */
list_is_first(const struct list_head * list,const struct list_head * head)118 static inline int list_is_first(const struct list_head *list,
119 				const struct list_head *head)
120 {
121 	return list == head->next;
122 }
123 
124 /**
125  * list_empty - tests whether a list is empty
126  * @head: the list to test.
127  */
list_empty(const struct list_head * head)128 static inline int list_empty(const struct list_head *head)
129 {
130 	return head->next == head;
131 }
132 
133 
134 /**
135  * list_entry - get the struct for this entry
136  * @ptr:	the &struct list_head pointer.
137  * @type:	the type of the struct this is embedded in.
138  * @member:	the name of the list_struct within the struct.
139  */
140 #define list_entry(ptr, type, member) \
141 	container_of(ptr, type, member)
142 
143 /**
144  * list_first_entry - get the first element from a list
145  * @ptr:	the list head to take the element from.
146  * @type:	the type of the struct this is embedded in.
147  * @member:	the name of the list_struct within the struct.
148  *
149  * Note, that list is expected to be not empty.
150  */
151 #define list_first_entry(ptr, type, member) \
152 	list_entry((ptr)->next, type, member)
153 
154 /**
155  * list_last_entry - get the last element from a list
156  * @ptr:        the list head to take the element from.
157  * @type:       the type of the struct this is embedded in.
158  * @member:     the name of the list_head within the struct.
159  *
160  * Note, that list is expected to be not empty.
161  */
162 #define list_last_entry(ptr, type, member) \
163 	list_entry((ptr)->prev, type, member)
164 
165 /**
166  * list_for_each	-	iterate over a list
167  * @pos:	the &struct list_head to use as a loop cursor.
168  * @head:	the head for your list.
169  */
170 #define list_for_each(pos, head) \
171 	for (pos = (head)->next; pos != (head); \
172         	pos = pos->next)
173 
174 /**
175  * list_for_each_prev	-	iterate over a list backwards
176  * @pos:	the &struct list_head to use as a loop cursor.
177  * @head:	the head for your list.
178  */
179 #define list_for_each_prev(pos, head) \
180 	for (pos = (head)->prev; pos != (head); \
181         	pos = pos->prev)
182 
183 /**
184  * list_for_each_safe - iterate over a list safe against removal of list entry
185  * @pos:	the &struct list_head to use as a loop cursor.
186  * @n:		another &struct list_head to use as temporary storage
187  * @head:	the head for your list.
188  */
189 #define list_for_each_safe(pos, n, head) \
190 	for (pos = (head)->next, n = pos->next; pos != (head); \
191 		pos = n, n = pos->next)
192 
193 
194 /**
195  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
196  * @pos:	the &struct list_head to use as a loop cursor.
197  * @n:		another &struct list_head to use as temporary storage
198  * @head:	the head for your list.
199  */
200 #define list_for_each_prev_safe(pos, n, head) \
201 	for (pos = (head)->prev, n = pos->prev; \
202 	     pos != (head); \
203 	     pos = n, n = pos->prev)
204 
205 /**
206  * list_for_each_entry	-	iterate over list of given type
207  * @pos:	the type * to use as a loop cursor.
208  * @head:	the head for your list.
209  * @member:	the name of the list_struct within the struct.
210  */
211 #define list_for_each_entry(pos, head, member)				\
212 	for (pos = list_entry((head)->next, typeof(*pos), member);	\
213 	     &pos->member != (head); 	\
214 	     pos = list_entry(pos->member.next, typeof(*pos), member))
215 
216 
217 /**
218  * list_for_each_entry_reverse - iterate backwards over list of given type.
219  * @pos:	the type * to use as a loop cursor.
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_reverse(pos, head, member)			\
224 	for (pos = list_entry((head)->prev, typeof(*pos), member);	\
225 	     &pos->member != (head); 	\
226 	     pos = list_entry(pos->member.prev, typeof(*pos), member))
227 
228 /**
229  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
230  * @pos:	the type * to use as a loop cursor.
231  * @n:		another type * to use as temporary storage
232  * @head:	the head for your list.
233  * @member:	the name of the list_struct within the struct.
234  */
235 #define list_for_each_entry_safe(pos, n, head, member)			\
236 	for (pos = list_entry((head)->next, typeof(*pos), member),	\
237 		n = list_entry(pos->member.next, typeof(*pos), member);	\
238 	     &pos->member != (head); 					\
239 	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
240