1 /* Doubly linked list macros compatible with Linux kernel's version
2 * Copyright (c) 2015 by Takashi Iwai <tiwai@suse.de>
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2.1 of
7 * the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 */
14
15 #ifndef _LIST_H
16 #define _LIST_H
17
18 #include <stddef.h>
19
20 struct list_head {
21 struct list_head *next;
22 struct list_head *prev;
23 };
24
25 /* one-shot definition of a list head */
26 #define LIST_HEAD(x) \
27 struct list_head x = { &x, &x }
28
29 /* initialize a list head explicitly */
INIT_LIST_HEAD(struct list_head * p)30 static inline void INIT_LIST_HEAD(struct list_head *p)
31 {
32 p->next = p->prev = p;
33 }
34
35 #define list_entry_offset(p, type, offset) \
36 ((type *)((char *)(p) - (offset)))
37
38 /* list_entry - retrieve the original struct from list_head
39 * @p: list_head pointer
40 * @type: struct type
41 * @member: struct field member containing the list_head
42 */
43 #define list_entry(p, type, member) \
44 list_entry_offset(p, type, offsetof(type, member))
45
46 /* list_for_each - iterate over the linked list
47 * @p: iterator, a list_head pointer variable
48 * @list: list_head pointer containing the list
49 */
50 #define list_for_each(p, list) \
51 for (p = (list)->next; p != (list); p = p->next)
52
53 /* list_for_each_safe - iterate over the linked list, safe to delete
54 * @p: iterator, a list_head pointer variable
55 * @s: a temporary variable to keep the next, a list_head pointer, too
56 * @list: list_head pointer containing the list
57 */
58 #define list_for_each_safe(p, s, list) \
59 for (p = (list)->next; s = p->next, p != (list); p = s)
60
61 /* list_add - prepend a list entry at the head
62 * @p: the new list entry to add
63 * @list: the list head
64 */
list_add(struct list_head * p,struct list_head * list)65 static inline void list_add(struct list_head *p, struct list_head *list)
66 {
67 struct list_head *first = list->next;
68
69 p->next = first;
70 first->prev = p;
71 list->next = p;
72 p->prev = list;
73 }
74
75 /* list_add_tail - append a list entry at the tail
76 * @p: the new list entry to add
77 * @list: the list head
78 */
list_add_tail(struct list_head * p,struct list_head * list)79 static inline void list_add_tail(struct list_head *p, struct list_head *list)
80 {
81 struct list_head *last = list->prev;
82
83 last->next = p;
84 p->prev = last;
85 p->next = list;
86 list->prev = p;
87 }
88
89 /* list_insert - insert a new list entry between two known consecutive entries
90 * @p: the new entry to be inserted between prev and next
91 * @prev: the left-side entry
92 * @next: the right-side entry
93 */
list_insert(struct list_head * p,struct list_head * prev,struct list_head * next)94 static inline void list_insert(struct list_head *p,
95 struct list_head *prev,
96 struct list_head *next)
97 {
98 next->prev = p;
99 p->next = next;
100 p->prev = prev;
101 prev->next = p;
102 }
103
104 /* list_del - delete the given list entry */
list_del(struct list_head * p)105 static inline void list_del(struct list_head *p)
106 {
107 p->prev->next = p->next;
108 p->next->prev = p->prev;
109 }
110
111 /* list_empty - returns 1 if the given list is empty */
list_empty(const struct list_head * p)112 static inline int list_empty(const struct list_head *p)
113 {
114 return p->next == p;
115 }
116
117 #endif /* _LIST_H */
118