blob: bb9a3aa74f59d73273866dcafd94f2c2ba2457b5 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001#ifndef _ZXIC_LIST_H__
2#define _ZXIC_LIST_H__
3
4#undef offsetof
5#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
6
7/**
8 * container_of - cast a member of a structure out to the containing structure
9 *
10 * @ptr: the pointer to the member.
11 * @type: the type of the container struct this is embedded in.
12 * @member: the name of the member within the struct.
13 *
14 */
15#define container_of(ptr, type, member) ({ \
16 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
17 (type *)( (char *)__mptr - offsetof(type,member) );})
18
19
20struct list_head {
21 struct list_head *next, *prev;
22};
23
24
25#define LIST_HEAD_INIT(name) { &(name), &(name) }
26
27#define LIST_HEAD(name) \
28 struct list_head name = LIST_HEAD_INIT(name)
29
30static inline void INIT_LIST_HEAD(struct list_head *list)
31{
32 list->next = list;
33 list->prev = list;
34}
35
36/*
37 * Insert a new entry between two known consecutive entries.
38 *
39 * This is only for internal list manipulation where we know
40 * the prev/next entries already!
41 */
42static inline void __list_add(struct list_head *new,
43 struct list_head *prev,
44 struct list_head *next)
45{
46 next->prev = new;
47 new->next = next;
48 new->prev = prev;
49 prev->next = new;
50}
51
52/**
53 * list_add - add a new entry
54 * @new: new entry to be added
55 * @head: list head to add it after
56 *
57 * Insert a new entry after the specified head.
58 * This is good for implementing stacks.
59 */
60static inline void list_add(struct list_head *new, struct list_head *head)
61{
62 __list_add(new, head, head->next);
63}
64
65
66/**
67 * list_add_tail - add a new entry
68 * @new: new entry to be added
69 * @head: list head to add it before
70 *
71 * Insert a new entry before the specified head.
72 * This is useful for implementing queues.
73 */
74static inline void list_add_tail(struct list_head *new, struct list_head *head)
75{
76 __list_add(new, head->prev, head);
77}
78
79/*
80 * Delete a list entry by making the prev/next entries
81 * point to each other.
82 *
83 * This is only for internal list manipulation where we know
84 * the prev/next entries already!
85 */
86static inline void __list_del(struct list_head * prev, struct list_head * next)
87{
88 next->prev = prev;
89 prev->next = next;
90}
91
92/**
93 * list_del - deletes entry from list.
94 * @entry: the element to delete from the list.
95 * Note: list_empty() on entry does not return true after this, the entry is
96 * in an undefined state.
97 */
98static inline void __list_del_entry(struct list_head *entry)
99{
100 __list_del(entry->prev, entry->next);
101}
102
103#define LIST_POISON1 0
104#define LIST_POISON2 0
105
106static inline void list_del(struct list_head *entry)
107{
108 __list_del(entry->prev, entry->next);
109 entry->next = LIST_POISON1;
110 entry->prev = LIST_POISON2;
111}
112
113/**
114 * list_replace - replace old entry by new one
115 * @old : the element to be replaced
116 * @new : the new element to insert
117 *
118 * If @old was empty, it will be overwritten.
119 */
120static inline void list_replace(struct list_head *old,
121 struct list_head *new)
122{
123 new->next = old->next;
124 new->next->prev = new;
125 new->prev = old->prev;
126 new->prev->next = new;
127}
128
129static inline void list_replace_init(struct list_head *old,
130 struct list_head *new)
131{
132 list_replace(old, new);
133 INIT_LIST_HEAD(old);
134}
135
136/**
137 * list_del_init - deletes entry from list and reinitialize it.
138 * @entry: the element to delete from the list.
139 */
140static inline void list_del_init(struct list_head *entry)
141{
142 __list_del_entry(entry);
143 INIT_LIST_HEAD(entry);
144}
145
146/**
147 * list_move - delete from one list and add as another's head
148 * @list: the entry to move
149 * @head: the head that will precede our entry
150 */
151static inline void list_move(struct list_head *list, struct list_head *head)
152{
153 __list_del_entry(list);
154 list_add(list, head);
155}
156
157/**
158 * list_move_tail - delete from one list and add as another's tail
159 * @list: the entry to move
160 * @head: the head that will follow our entry
161 */
162static inline void list_move_tail(struct list_head *list,
163 struct list_head *head)
164{
165 __list_del_entry(list);
166 list_add_tail(list, head);
167}
168
169/**
170 * list_is_last - tests whether @list is the last entry in list @head
171 * @list: the entry to test
172 * @head: the head of the list
173 */
174static inline int list_is_last(const struct list_head *list,
175 const struct list_head *head)
176{
177 return list->next == head;
178}
179
180/**
181 * list_empty - tests whether a list is empty
182 * @head: the list to test.
183 */
184static inline int list_empty(const struct list_head *head)
185{
186 return head->next == head;
187}
188
189/**
190 * list_is_singular - tests whether a list has just one entry.
191 * @head: the list to test.
192 */
193static inline int list_is_singular(const struct list_head *head)
194{
195 return !list_empty(head) && (head->next == head->prev);
196}
197
198/**
199 * list_entry - get the struct for this entry
200 * @ptr: the &struct list_head pointer.
201 * @type: the type of the struct this is embedded in.
202 * @member: the name of the list_struct within the struct.
203 */
204#define list_entry(ptr, type, member) \
205 container_of(ptr, type, member)
206
207
208/**
209 * list_for_each - iterate over a list
210 * @pos: the &struct list_head to use as a loop cursor.
211 * @head: the head for your list.
212 *
213 * This variant doesn't differ from list_for_each() any more.
214 * We don't do prefetching in either case.
215 */
216#define list_for_each(pos, head) \
217 for (pos = (head)->next; pos != (head); pos = pos->next)
218
219/**
220 * list_for_each_entry - iterate over list of given type
221 * @pos: the type * to use as a loop counter.
222 * @head: the head for your list.
223 * @member: the name of the list_struct within the struct.
224 */
225#define list_for_each_entry(pos, head, member) \
226 for (pos = list_entry((head)->next, typeof(*pos), member); \
227 &pos->member != (head); \
228 pos = list_entry(pos->member.next, typeof(*pos), member))
229
230/**
231 * list_for_each_safe - iterate over a list safe against removal of list entry
232 * @pos: the &struct list_head to use as a loop counter.
233 * @n: another &struct list_head to use as temporary storage
234 * @head: the head for your list.
235 */
236#define list_for_each_safe(pos, n, head) \
237 for (pos = (head)->next, n = pos->next; pos != (head); \
238 pos = n, n = pos->next)
239
240/**
241 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
242 * @pos: the type * to use as a loop counter.
243 * @n: another type * to use as temporary storage
244 * @head: the head for your list.
245 * @member: the name of the list_struct within the struct.
246 */
247#define list_for_each_entry_safe(pos, n, head, member) \
248 for (pos = list_entry((head)->next, typeof(*pos), member), \
249 n = list_entry(pos->member.next, typeof(*pos), member); \
250 &pos->member != (head); \
251 pos = n, n = list_entry(n->member.next, typeof(*n), member))
252
253#endif