1 #ifndef _LIST_H
2 #define _LIST_H
3
4 /* Stripped down implementation of linked list taken
5 * from the Linux Kernel.
6 */
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
INIT_LIST_HEAD(struct list_head * list)27 static inline void INIT_LIST_HEAD(struct list_head *list)
28 {
29 list->next = list;
30 list->prev = list;
31 }
32
33 /*
34 * Insert a new entry between two known consecutive entries.
35 *
36 * This is only for internal list manipulation where we know
37 * the prev/next entries already!
38 */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)39 static inline void __list_add(struct list_head *new,
40 struct list_head *prev,
41 struct list_head *next)
42 {
43 next->prev = new;
44 new->next = next;
45 new->prev = prev;
46 prev->next = new;
47 }
48
49 /**
50 * list_add - add a new entry
51 * @new: new entry to be added
52 * @head: list head to add it after
53 *
54 * Insert a new entry after the specified head.
55 * This is good for implementing stacks.
56 */
list_add(struct list_head * new,struct list_head * head)57 static inline void list_add(struct list_head *new, struct list_head *head)
58 {
59 __list_add(new, head, head->next);
60 }
61
62 /**
63 * list_add_tail - add a new entry
64 * @new: new entry to be added
65 * @head: list head to add it before
66 *
67 * Insert a new entry before the specified head.
68 * This is useful for implementing queues.
69 */
list_add_tail(struct list_head * new,struct list_head * head)70 static inline void list_add_tail(struct list_head *new, struct list_head *head)
71 {
72 __list_add(new, head->prev, head);
73 }
74
75 /*
76 * Delete a list entry by making the prev/next entries
77 * point to each other.
78 *
79 * This is only for internal list manipulation where we know
80 * the prev/next entries already!
81 */
__list_del(struct list_head * prev,struct list_head * next)82 static inline void __list_del(struct list_head * prev, struct list_head * next)
83 {
84 next->prev = prev;
85 prev->next = next;
86 }
87
88 #ifndef __clang_analyzer__
89 #define LIST_POISON1 ((void *) 0x00100100)
90 #define LIST_POISON2 ((void *) 0x00200200)
91 #else
92 #define LIST_POISON1 NULL
93 #define LIST_POISON2 NULL
94 #endif
95
96 /**
97 * list_empty - tests whether a list is empty
98 * @head: the list to test.
99 */
list_empty(const struct list_head * head)100 static inline int list_empty(const struct list_head *head)
101 {
102 return head->next == head;
103 }
104
105 /**
106 * list_del - deletes entry from list.
107 * @entry: the element to delete from the list.
108 * Note: list_empty() on entry does not return true after this, the entry is
109 * in an undefined state.
110 */
__list_del_entry(struct list_head * entry)111 static inline void __list_del_entry(struct list_head *entry)
112 {
113 __list_del(entry->prev, entry->next);
114 }
115
list_del(struct list_head * entry)116 static inline void list_del(struct list_head *entry)
117 {
118 __list_del(entry->prev, entry->next);
119 entry->next = LIST_POISON1;
120 entry->prev = LIST_POISON2;
121 }
122
123 /**
124 * list_entry - get the struct for this entry
125 * @ptr: the &struct list_head pointer.
126 * @type: the type of the struct this is embedded in.
127 * @member: the name of the list_struct within the struct.
128 */
129 #define list_entry(ptr, type, member) \
130 container_of(ptr, type, member)
131
132 /**
133 * list_first_entry - get the first element from a list
134 * @ptr: the list head to take the element from.
135 * @type: the type of the struct this is embedded in.
136 * @member: the name of the list_struct within the struct.
137 *
138 * Note, that list is expected to be not empty.
139 */
140 #define list_first_entry(ptr, type, member) \
141 list_entry((ptr)->next, type, member)
142
143 /**
144 * list_last_entry - get the last 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_last_entry(ptr, type, member) \
152 list_entry((ptr)->prev, type, member)
153
154 /**
155 * list_first_entry_or_null - get the first 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_struct within the struct.
159 *
160 * Note that if the list is empty, it returns NULL.
161 */
162 #define list_first_entry_or_null(ptr, type, member) \
163 (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
164
165 /**
166 * list_last_entry_or_null - get the last element from a list
167 * @ptr: the list head to take the element from.
168 * @type: the type of the struct this is embedded in.
169 * @member: the name of the list_struct within the struct.
170 *
171 * Note that if the list is empty, it returns NULL.
172 */
173 #define list_last_entry_or_null(ptr, type, member) \
174 (!list_empty(ptr) ? list_last_entry(ptr, type, member) : NULL)
175
176 /**
177 * list_next_entry - get the next element in list
178 * @pos: the type * to cursor
179 * @member: the name of the list_struct within the struct.
180 */
181 #define list_next_entry(pos, member) \
182 list_entry((pos)->member.next, typeof(*(pos)), member)
183
184 /**
185 * list_prev_entry - get the prev element in list
186 * @pos: the type * to cursor
187 * @member: the name of the list_struct within the struct.
188 */
189 #define list_prev_entry(pos, member) \
190 list_entry((pos)->member.prev, typeof(*(pos)), member)
191
192 /**
193 * list_for_each - iterate over a list
194 * @pos: the &struct list_head to use as a loop cursor.
195 * @head: the head for your list.
196 */
197 #define list_for_each(pos, head) \
198 for (pos = (head)->next; pos != (head); pos = pos->next)
199
200 /**
201 * list_for_each_prev - iterate over a list backwards
202 * @pos: the &struct list_head to use as a loop cursor.
203 * @head: the head for your list.
204 */
205 #define list_for_each_prev(pos, head) \
206 for (pos = (head)->prev; pos != (head); pos = pos->prev)
207
208 /**
209 * list_for_each_safe - iterate over a list safe against removal of list entry
210 * @pos: the &struct list_head to use as a loop cursor.
211 * @n: another &struct list_head to use as temporary storage
212 * @head: the head for your list.
213 */
214 #define list_for_each_safe(pos, n, head) \
215 for (pos = (head)->next, n = pos->next; pos != (head); \
216 pos = n, n = pos->next)
217
218 /**
219 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
220 * @pos: the &struct list_head to use as a loop cursor.
221 * @n: another &struct list_head to use as temporary storage
222 * @head: the head for your list.
223 */
224 #define list_for_each_prev_safe(pos, n, head) \
225 for (pos = (head)->prev, n = pos->prev; \
226 pos != (head); \
227 pos = n, n = pos->prev)
228
229 /**
230 * list_for_each_entry - iterate over list of given type
231 * @pos: the type * to use as a loop cursor.
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(pos, head, member) \
236 for (pos = list_first_entry(head, typeof(*pos), member); \
237 &pos->member != (head); \
238 pos = list_next_entry(pos, member))
239
240 /**
241 * list_for_each_entry_reverse - iterate backwards over list of given type.
242 * @pos: the type * to use as a loop cursor.
243 * @head: the head for your list.
244 * @member: the name of the list_struct within the struct.
245 */
246 #define list_for_each_entry_reverse(pos, head, member) \
247 for (pos = list_last_entry(head, typeof(*pos), member); \
248 &pos->member != (head); \
249 pos = list_prev_entry(pos, member))
250
251 /**
252 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
253 * @pos: the type * to use as a start point
254 * @head: the head of the list
255 * @member: the name of the list_struct within the struct.
256 *
257 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
258 */
259 #define list_prepare_entry(pos, head, member) \
260 ((pos) ? : list_entry(head, typeof(*pos), member))
261
262 /**
263 * list_for_each_entry_continue - continue iteration over list of given type
264 * @pos: the type * to use as a loop cursor.
265 * @head: the head for your list.
266 * @member: the name of the list_struct within the struct.
267 *
268 * Continue to iterate over list of given type, continuing after
269 * the current position.
270 */
271 #define list_for_each_entry_continue(pos, head, member) \
272 for (pos = list_next_entry(pos, member); \
273 &pos->member != (head); \
274 pos = list_next_entry(pos, member))
275
276 /**
277 * list_for_each_entry_continue_reverse - iterate backwards from the given point
278 * @pos: the type * to use as a loop cursor.
279 * @head: the head for your list.
280 * @member: the name of the list_struct within the struct.
281 *
282 * Start to iterate over list of given type backwards, continuing after
283 * the current position.
284 */
285 #define list_for_each_entry_continue_reverse(pos, head, member) \
286 for (pos = list_prev_entry(pos, member); \
287 &pos->member != (head); \
288 pos = list_prev_entry(pos, member))
289
290 /**
291 * list_for_each_entry_from - iterate over list of given type from the current point
292 * @pos: the type * to use as a loop cursor.
293 * @head: the head for your list.
294 * @member: the name of the list_struct within the struct.
295 *
296 * Iterate over list of given type, continuing from current position.
297 */
298 #define list_for_each_entry_from(pos, head, member) \
299 for (; &pos->member != (head); \
300 pos = list_next_entry(pos, member))
301
302 /**
303 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
304 * @pos: the type * to use as a loop cursor.
305 * @n: another type * to use as temporary storage
306 * @head: the head for your list.
307 * @member: the name of the list_struct within the struct.
308 */
309 #define list_for_each_entry_safe(pos, n, head, member) \
310 for (pos = list_first_entry(head, typeof(*pos), member), \
311 n = list_next_entry(pos, member); \
312 &pos->member != (head); \
313 pos = n, n = list_next_entry(n, member))
314
315 /**
316 * list_for_each_entry_safe_continue - continue list iteration safe against removal
317 * @pos: the type * to use as a loop cursor.
318 * @n: another type * to use as temporary storage
319 * @head: the head for your list.
320 * @member: the name of the list_struct within the struct.
321 *
322 * Iterate over list of given type, continuing after current point,
323 * safe against removal of list entry.
324 */
325 #define list_for_each_entry_safe_continue(pos, n, head, member) \
326 for (pos = list_next_entry(pos, member), \
327 n = list_next_entry(pos, member); \
328 &pos->member != (head); \
329 pos = n, n = list_next_entry(n, member))
330
331 /**
332 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
333 * @pos: the type * to use as a loop cursor.
334 * @n: another type * to use as temporary storage
335 * @head: the head for your list.
336 * @member: the name of the list_struct within the struct.
337 *
338 * Iterate over list of given type from current point, safe against
339 * removal of list entry.
340 */
341 #define list_for_each_entry_safe_from(pos, n, head, member) \
342 for (n = list_next_entry(pos, member); \
343 &pos->member != (head); \
344 pos = n, n = list_next_entry(n, member))
345
346 /**
347 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
348 * @pos: the type * to use as a loop cursor.
349 * @n: another type * to use as temporary storage
350 * @head: the head for your list.
351 * @member: the name of the list_struct within the struct.
352 *
353 * Iterate backwards over list of given type, safe against removal
354 * of list entry.
355 */
356 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
357 for (pos = list_last_entry(head, typeof(*pos), member), \
358 n = list_prev_entry(pos, member); \
359 &pos->member != (head); \
360 pos = n, n = list_prev_entry(n, member))
361
362 /**
363 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
364 * @pos: the loop cursor used in the list_for_each_entry_safe loop
365 * @n: temporary storage used in list_for_each_entry_safe
366 * @member: the name of the list_struct within the struct.
367 *
368 * list_safe_reset_next is not safe to use in general if the list may be
369 * modified concurrently (eg. the lock is dropped in the loop body). An
370 * exception to this is if the cursor element (pos) is pinned in the list,
371 * and list_safe_reset_next is called after re-taking the lock and before
372 * completing the current iteration of the loop body.
373 */
374 #define list_safe_reset_next(pos, n, member) \
375 n = list_next_entry(pos, member)
376
377 #ifndef offsetof
378 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
379 #endif
380
381 /**
382 * container_of - cast a member of a structure out to the containing structure
383 * @ptr: the pointer to the member.
384 * @type: the type of the container struct this is embedded in.
385 * @member: the name of the member within the struct.
386 *
387 */
388 #ifndef __clang_analyzer__
389 #define container_of(ptr, type, member) __extension__({ \
390 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
391 (type *)( (char *)__mptr - offsetof(type,member) );})
392 #else
393 #define container_of(ptr, type, member) __extension__({ \
394 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
395 (type *)( (char *)__mptr - (char *)offsetof(type,member) );})
396 #endif
397 #endif
398