1 #ifndef _SYS_LIST_H
2 #define _SYS_LIST_H
3 
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7 
8 #include "compiler.h"
9 
10 /*
11  * Simple doubly linked list implementation.
12  *
13  * Some of the internal functions ("__xxx") are useful when
14  * manipulating whole lists rather than single entries, as
15  * sometimes we already know the next/prev entries and we can
16  * generate better code by using them directly rather than
17  * using the generic single-entry routines.
18  */
19 
20 struct list_head {
21     struct list_head *next, *prev;
22 };
23 
24 #define LIST_HEAD_INIT(name) { &(name), &(name) }
25 
26 #define LIST_HEAD_DEF(name) \
27     struct list_head name = LIST_HEAD_INIT(name)
28 
INIT_LIST_HEAD(struct list_head * list)29 static __always_inline void INIT_LIST_HEAD(struct list_head *list)
30 {
31     list->next = list;
32     list->prev = list;
33 }
34 
35 /*
36  * Insert a new entry between two known consecutive entries.
37  *
38  * This is only for internal list manipulation where we know
39  * the prev/next entries already!
40  */
__list_add(struct list_head * newl,struct list_head * prev,struct list_head * next)41 static __always_inline void __list_add(struct list_head *newl,
42                   struct list_head *prev,
43                   struct list_head *next)
44 {
45     next->prev = newl;
46     newl->next = next;
47     newl->prev = prev;
48     prev->next = newl;
49 }
50 
51 /**
52  * list_add - add a new entry
53  * @new: new entry to be added
54  * @head: list head to add it after
55  *
56  * Insert a new entry after the specified head.
57  * This is good for implementing stacks.
58  */
list_add(struct list_head * newl,struct list_head * head)59 static __always_inline void list_add(struct list_head *newl, struct list_head *head)
60 {
61     __list_add(newl, head, head->next);
62 }
63 
64 
65 /**
66  * list_add_tail - add a new entry
67  * @new: new entry to be added
68  * @head: list head to add it before
69  *
70  * Insert a new entry before the specified head.
71  * This is useful for implementing queues.
72  */
list_add_tail(struct list_head * newl,struct list_head * head)73 static __always_inline void list_add_tail(struct list_head *newl, struct list_head *head)
74 {
75     __list_add(newl, head->prev, head);
76 }
77 
78 /*
79  * Delete a list entry by making the prev/next entries
80  * point to each other.
81  *
82  * This is only for internal list manipulation where we know
83  * the prev/next entries already!
84  */
__list_del(struct list_head * prev,struct list_head * next)85 static __always_inline void __list_del(struct list_head * prev, struct list_head * next)
86 {
87     next->prev = prev;
88     prev->next = next;
89 }
90 
91 /**
92  * list_del - deletes entry from list.
93  * @entry: the element to delete from the list.
94  * Note: list_empty() on entry does not return true after this, the entry is
95  * in an undefined state.
96  */
__list_del_entry(struct list_head * entry)97 static __always_inline void __list_del_entry(struct list_head *entry)
98 {
99     __list_del(entry->prev, entry->next);
100 }
101 
list_del(struct list_head * entry)102 static __always_inline void list_del(struct list_head *entry)
103 {
104     __list_del(entry->prev, entry->next);
105     entry->next = entry;
106     entry->prev = entry;
107 }
108 
109 /**
110  * list_replace - replace old entry by new one
111  * @old : the element to be replaced
112  * @new : the new element to insert
113  *
114  * If @old was empty, it will be overwritten.
115  */
list_replace(struct list_head * old,struct list_head * newl)116 static __always_inline void list_replace(struct list_head *old, struct list_head *newl)
117 {
118     newl->next = old->next;
119     newl->next->prev = newl;
120     newl->prev = old->prev;
121     newl->prev->next = newl;
122 }
123 
list_replace_init(struct list_head * old,struct list_head * newl)124 static __always_inline void list_replace_init(struct list_head *old,
125                      struct list_head *newl)
126 {
127     list_replace(old, newl);
128     INIT_LIST_HEAD(old);
129 }
130 
131 /**
132  * list_del_init - deletes entry from list and reinitialize it.
133  * @entry: the element to delete from the list.
134  */
list_del_init(struct list_head * entry)135 static __always_inline void list_del_init(struct list_head *entry)
136 {
137     __list_del_entry(entry);
138     INIT_LIST_HEAD(entry);
139 }
140 
141 /**
142  * list_move - delete from one list and add as another's head
143  * @list: the entry to move
144  * @head: the head that will precede our entry
145  */
list_move(struct list_head * list,struct list_head * head)146 static __always_inline void list_move(struct list_head *list, struct list_head *head)
147 {
148     __list_del_entry(list);
149     list_add(list, head);
150 }
151 
152 /**
153  * list_move_tail - delete from one list and add as another's tail
154  * @list: the entry to move
155  * @head: the head that will follow our entry
156  */
list_move_tail(struct list_head * list,struct list_head * head)157 static __always_inline void list_move_tail(struct list_head *list,
158                   struct list_head *head)
159 {
160     __list_del_entry(list);
161     list_add_tail(list, head);
162 }
163 
164 /**
165  * list_is_last - tests whether @list is the last entry in list @head
166  * @list: the entry to test
167  * @head: the head of the list
168  */
list_is_last(const struct list_head * list,const struct list_head * head)169 static __always_inline int list_is_last(const struct list_head *list,
170                 const struct list_head *head)
171 {
172     return list->next == head;
173 }
174 
175 /**
176  * list_empty - tests whether a list is empty
177  * @head: the list to test.
178  */
list_empty(const struct list_head * head)179 static __always_inline int list_empty(const struct list_head *head)
180 {
181     return head->next == head;
182 }
183 
184 /**
185  * list_empty_careful - tests whether a list is empty and not being modified
186  * @head: the list to test
187  *
188  * Description:
189  * tests whether a list is empty _and_ checks that no other CPU might be
190  * in the process of modifying either member (next or prev)
191  *
192  * NOTE: using list_empty_careful() without synchronization
193  * can only be safe if the only activity that can happen
194  * to the list entry is list_del_init(). Eg. it cannot be used
195  * if another CPU could re-list_add() it.
196  */
list_empty_careful(const struct list_head * head)197 static __always_inline int list_empty_careful(const struct list_head *head)
198 {
199     struct list_head *next = head->next;
200     return (next == head) && (next == head->prev);
201 }
202 
203 /**
204  * list_rotate_left - rotate the list to the left
205  * @head: the head of the list
206  */
list_rotate_left(struct list_head * head)207 static __always_inline void list_rotate_left(struct list_head *head)
208 {
209     struct list_head *first;
210 
211     if (!list_empty(head)) {
212         first = head->next;
213         list_move_tail(first, head);
214     }
215 }
216 
217 /**
218  * list_is_singular - tests whether a list has just one entry.
219  * @head: the list to test.
220  */
list_is_singular(const struct list_head * head)221 static __always_inline int list_is_singular(const struct list_head *head)
222 {
223     return !list_empty(head) && (head->next == head->prev);
224 }
225 
__list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)226 static __always_inline void __list_cut_position(struct list_head *list,
227         struct list_head *head, struct list_head *entry)
228 {
229     struct list_head *new_first = entry->next;
230     list->next = head->next;
231     list->next->prev = list;
232     list->prev = entry;
233     entry->next = list;
234     head->next = new_first;
235     new_first->prev = head;
236 }
237 
238 /**
239  * list_cut_position - cut a list into two
240  * @list: a new list to add all removed entries
241  * @head: a list with entries
242  * @entry: an entry within head, could be the head itself
243  *  and if so we won't cut the list
244  *
245  * This helper moves the initial part of @head, up to and
246  * including @entry, from @head to @list. You should
247  * pass on @entry an element you know is on @head. @list
248  * should be an empty list or a list you do not care about
249  * losing its data.
250  *
251  */
list_cut_position(struct list_head * list,struct list_head * head,struct list_head * entry)252 static __always_inline void list_cut_position(struct list_head *list,
253         struct list_head *head, struct list_head *entry)
254 {
255     if (list_empty(head))
256         return;
257     if (list_is_singular(head) &&
258         (head->next != entry && head != entry))
259         return;
260     if (entry == head)
261         INIT_LIST_HEAD(list);
262     else
263         __list_cut_position(list, head, entry);
264 }
265 
__list_splice(const struct list_head * list,struct list_head * prev,struct list_head * next)266 static __always_inline void __list_splice(const struct list_head *list,
267                  struct list_head *prev,
268                  struct list_head *next)
269 {
270     struct list_head *first = list->next;
271     struct list_head *last = list->prev;
272 
273     first->prev = prev;
274     prev->next = first;
275 
276     last->next = next;
277     next->prev = last;
278 }
279 
280 /**
281  * list_splice - join two lists, this is designed for stacks
282  * @list: the new list to add.
283  * @head: the place to add it in the first list.
284  */
list_splice(const struct list_head * list,struct list_head * head)285 static __always_inline void list_splice(const struct list_head *list,
286                 struct list_head *head)
287 {
288     if (!list_empty(list))
289         __list_splice(list, head, head->next);
290 }
291 
292 /**
293  * list_splice_tail - join two lists, each list being a queue
294  * @list: the new list to add.
295  * @head: the place to add it in the first list.
296  */
list_splice_tail(struct list_head * list,struct list_head * head)297 static __always_inline void list_splice_tail(struct list_head *list,
298                 struct list_head *head)
299 {
300     if (!list_empty(list))
301         __list_splice(list, head->prev, head);
302 }
303 
304 /**
305  * list_splice_init - join two lists and reinitialise the emptied list.
306  * @list: the new list to add.
307  * @head: the place to add it in the first list.
308  *
309  * The list at @list is reinitialised
310  */
list_splice_init(struct list_head * list,struct list_head * head)311 static __always_inline void list_splice_init(struct list_head *list,
312                     struct list_head *head)
313 {
314     if (!list_empty(list)) {
315         __list_splice(list, head, head->next);
316         INIT_LIST_HEAD(list);
317     }
318 }
319 
320 /**
321  * list_splice_tail_init - join two lists and reinitialise the emptied list
322  * @list: the new list to add.
323  * @head: the place to add it in the first list.
324  *
325  * Each of the lists is a queue.
326  * The list at @list is reinitialised
327  */
list_splice_tail_init(struct list_head * list,struct list_head * head)328 static __always_inline void list_splice_tail_init(struct list_head *list,
329                      struct list_head *head)
330 {
331     if (!list_empty(list)) {
332         __list_splice(list, head->prev, head);
333         INIT_LIST_HEAD(list);
334     }
335 }
336 
337 /**
338  * list_entry - get the struct for this entry
339  * @ptr:    the &struct list_head pointer.
340  * @type:   the type of the struct this is embedded in.
341  * @member: the name of the list_head within the struct.
342  */
343 #define list_entry(ptr, type, member) \
344     container_of(ptr, type, member)
345 
346 /**
347  * list_first_entry - get the first element from a list
348  * @ptr:    the list head to take the element from.
349  * @type:   the type of the struct this is embedded in.
350  * @member: the name of the list_head within the struct.
351  *
352  * Note, that list is expected to be not empty.
353  */
354 #define list_first_entry(ptr, type, member) \
355     list_entry((ptr)->next, type, member)
356 
357 /**
358  * list_last_entry - get the last element from a list
359  * @ptr:    the list head to take the element from.
360  * @type:   the type of the struct this is embedded in.
361  * @member: the name of the list_head within the struct.
362  *
363  * Note, that list is expected to be not empty.
364  */
365 #define list_last_entry(ptr, type, member) \
366     list_entry((ptr)->prev, type, member)
367 
368 /**
369  * list_first_entry_or_null - get the first element from a list
370  * @ptr:    the list head to take the element from.
371  * @type:   the type of the struct this is embedded in.
372  * @member: the name of the list_head within the struct.
373  *
374  * Note that if the list is empty, it returns NULL.
375  */
376 #define list_first_entry_or_null(ptr, type, member) \
377     (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
378 
379 /**
380  * list_next_entry - get the next element in list
381  * @pos:    the type * to cursor
382  * @member: the name of the list_head within the struct.
383  */
384 #define list_next_entry(pos, member) \
385     list_entry((pos)->member.next, typeof(*(pos)), member)
386 
387 /**
388  * list_prev_entry - get the prev element in list
389  * @pos:    the type * to cursor
390  * @member: the name of the list_head within the struct.
391  */
392 #define list_prev_entry(pos, member) \
393     list_entry((pos)->member.prev, typeof(*(pos)), member)
394 
395 /**
396  * list_for_each    -   iterate over a list
397  * @pos:    the &struct list_head to use as a loop cursor.
398  * @head:   the head for your list.
399  */
400 #define list_for_each(pos, head) \
401     for (pos = (head)->next; pos != (head); pos = pos->next)
402 
403 /**
404  * list_for_each_prev   -   iterate over a list backwards
405  * @pos:    the &struct list_head to use as a loop cursor.
406  * @head:   the head for your list.
407  */
408 #define list_for_each_prev(pos, head) \
409     for (pos = (head)->prev; pos != (head); pos = pos->prev)
410 
411 /**
412  * list_for_each_safe - iterate over a list safe against removal of list entry
413  * @pos:    the &struct list_head to use as a loop cursor.
414  * @n:      another &struct list_head to use as temporary storage
415  * @head:   the head for your list.
416  */
417 #define list_for_each_safe(pos, n, head) \
418     for (pos = (head)->next, n = pos->next; pos != (head); \
419         pos = n, n = pos->next)
420 
421 /**
422  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
423  * @pos:    the &struct list_head to use as a loop cursor.
424  * @n:      another &struct list_head to use as temporary storage
425  * @head:   the head for your list.
426  */
427 #define list_for_each_prev_safe(pos, n, head) \
428     for (pos = (head)->prev, n = pos->prev; \
429          pos != (head); \
430          pos = n, n = pos->prev)
431 
432 /**
433  * list_for_each_entry  -   iterate over list of given type
434  * @pos:    the type * to use as a loop cursor.
435  * @head:   the head for your list.
436  * @member: the name of the list_head within the struct.
437  */
438 #define list_for_each_entry(pos, head, member)              \
439     for (pos = list_first_entry(head, typeof(*pos), member);    \
440          &pos->member != (head);                    \
441          pos = list_next_entry(pos, member))
442 
443 /**
444  * list_for_each_entry_reverse - iterate backwards over list of given type.
445  * @pos:    the type * to use as a loop cursor.
446  * @head:   the head for your list.
447  * @member: the name of the list_head within the struct.
448  */
449 #define list_for_each_entry_reverse(pos, head, member)          \
450     for (pos = list_last_entry(head, typeof(*pos), member);     \
451          &pos->member != (head);                    \
452          pos = list_prev_entry(pos, member))
453 
454 /**
455  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
456  * @pos:    the type * to use as a start point
457  * @head:   the head of the list
458  * @member: the name of the list_head within the struct.
459  *
460  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
461  */
462 #define list_prepare_entry(pos, head, member) \
463     ((pos) ? : list_entry(head, typeof(*pos), member))
464 
465 /**
466  * list_for_each_entry_continue - continue iteration over list of given type
467  * @pos:    the type * to use as a loop cursor.
468  * @head:   the head for your list.
469  * @member: the name of the list_head within the struct.
470  *
471  * Continue to iterate over list of given type, continuing after
472  * the current position.
473  */
474 #define list_for_each_entry_continue(pos, head, member)         \
475     for (pos = list_next_entry(pos, member);            \
476          &pos->member != (head);                    \
477          pos = list_next_entry(pos, member))
478 
479 /**
480  * list_for_each_entry_continue_reverse - iterate backwards from the given point
481  * @pos:    the type * to use as a loop cursor.
482  * @head:   the head for your list.
483  * @member: the name of the list_head within the struct.
484  *
485  * Start to iterate over list of given type backwards, continuing after
486  * the current position.
487  */
488 #define list_for_each_entry_continue_reverse(pos, head, member)     \
489     for (pos = list_prev_entry(pos, member);            \
490          &pos->member != (head);                    \
491          pos = list_prev_entry(pos, member))
492 
493 /**
494  * list_for_each_entry_from - iterate over list of given type from the current point
495  * @pos:    the type * to use as a loop cursor.
496  * @head:   the head for your list.
497  * @member: the name of the list_head within the struct.
498  *
499  * Iterate over list of given type, continuing from current position.
500  */
501 #define list_for_each_entry_from(pos, head, member)             \
502     for (; &pos->member != (head);                  \
503          pos = list_next_entry(pos, member))
504 
505 /**
506  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
507  * @pos:    the type * to use as a loop cursor.
508  * @n:      another type * to use as temporary storage
509  * @head:   the head for your list.
510  * @member: the name of the list_head within the struct.
511  */
512 #define list_for_each_entry_safe(pos, n, head, member)          \
513     for (pos = list_first_entry(head, typeof(*pos), member),    \
514         n = list_next_entry(pos, member);           \
515          &pos->member != (head);                    \
516          pos = n, n = list_next_entry(n, member))
517 
518 /**
519  * list_for_each_entry_safe_continue - continue list iteration safe against removal
520  * @pos:    the type * to use as a loop cursor.
521  * @n:      another type * to use as temporary storage
522  * @head:   the head for your list.
523  * @member: the name of the list_head within the struct.
524  *
525  * Iterate over list of given type, continuing after current point,
526  * safe against removal of list entry.
527  */
528 #define list_for_each_entry_safe_continue(pos, n, head, member)         \
529     for (pos = list_next_entry(pos, member),                \
530         n = list_next_entry(pos, member);               \
531          &pos->member != (head);                        \
532          pos = n, n = list_next_entry(n, member))
533 
534 /**
535  * list_for_each_entry_safe_from - iterate over list from current point safe against removal
536  * @pos:    the type * to use as a loop cursor.
537  * @n:      another type * to use as temporary storage
538  * @head:   the head for your list.
539  * @member: the name of the list_head within the struct.
540  *
541  * Iterate over list of given type from current point, safe against
542  * removal of list entry.
543  */
544 #define list_for_each_entry_safe_from(pos, n, head, member)             \
545     for (n = list_next_entry(pos, member);                  \
546          &pos->member != (head);                        \
547          pos = n, n = list_next_entry(n, member))
548 
549 /**
550  * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
551  * @pos:    the type * to use as a loop cursor.
552  * @n:      another type * to use as temporary storage
553  * @head:   the head for your list.
554  * @member: the name of the list_head within the struct.
555  *
556  * Iterate backwards over list of given type, safe against removal
557  * of list entry.
558  */
559 #define list_for_each_entry_safe_reverse(pos, n, head, member)      \
560     for (pos = list_last_entry(head, typeof(*pos), member),     \
561         n = list_prev_entry(pos, member);           \
562          &pos->member != (head);                    \
563          pos = n, n = list_prev_entry(n, member))
564 
565 /**
566  * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
567  * @pos:    the loop cursor used in the list_for_each_entry_safe loop
568  * @n:      temporary storage used in list_for_each_entry_safe
569  * @member: the name of the list_head within the struct.
570  *
571  * list_safe_reset_next is not safe to use in general if the list may be
572  * modified concurrently (eg. the lock is dropped in the loop body). An
573  * exception to this is if the cursor element (pos) is pinned in the list,
574  * and list_safe_reset_next is called after re-taking the lock and before
575  * completing the current iteration of the loop body.
576  */
577 #define list_safe_reset_next(pos, n, member)                \
578     n = list_next_entry(pos, member)
579 
580 #ifdef __cplusplus
581 }
582 #endif
583 
584 #endif /* _SYS_LIST_H */
585