blob: 6af86277dcd3444e4b44e869490a9b6ea2055a20 [file] [log] [blame]
hualing chenb31a6c62020-01-13 17:27:00 +08001/*
2* Copyright (c) 2014 Amlogic, Inc. All rights reserved.
3*
4* This source code is subject to the terms and conditions defined in the
5* file 'LICENSE' which is part of this source code package. *
6* Description:
7*/
hualing chen0888c032020-12-18 17:54:57 +08008
hualing chenb31a6c62020-01-13 17:27:00 +08009#ifndef _LINUX_LIST_H
10#define _LINUX_LIST_H
11
hualing chen0888c032020-12-18 17:54:57 +080012#ifdef __cplusplus
13extern "C"
14{
15#endif
16
hualing chenb31a6c62020-01-13 17:27:00 +080017#ifndef offsetof
18#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
19#endif
20
21#ifndef container_of
22/**
23 * container_of - cast a member of a structure out to the containing structure
hualing chen002e5b92022-02-23 17:51:21 +080024 * @ptr: the pointer to the member.
25 * @type: the type of the container struct this is embedded in.
26 * @member: the name of the member within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +080027 *
28 */
hualing chen002e5b92022-02-23 17:51:21 +080029#define container_of(ptr, type, member) ({ \
30 const typeof(((type *)0)->member) * __mptr = (ptr); \
31 (type *)((char *)__mptr - offsetof(type, member)); })
hualing chenb31a6c62020-01-13 17:27:00 +080032#endif
33
34#define LIST_POISON1 ((void *) 0x00100100)
35#define LIST_POISON2 ((void *) 0x00200200)
36
hualing chena540a7e2020-03-27 16:44:05 +080037#define prefetch(x) ((x) = (x))
hualing chenb31a6c62020-01-13 17:27:00 +080038
39
40/*
41 * Simple doubly linked list implementation.
42 *
43 * Some of the internal functions ("__xxx") are useful when
44 * manipulating whole lists rather than single entries, as
Wentao MA9a164002022-08-29 11:20:24 +080045 * sometimes we already know the c_next/c_prev entries and we can
hualing chenb31a6c62020-01-13 17:27:00 +080046 * generate better code by using them directly rather than
47 * using the generic single-entry routines.
48 */
49
50struct list_head {
Wentao MA9a164002022-08-29 11:20:24 +080051 struct list_head *c_next, *c_prev;
hualing chenb31a6c62020-01-13 17:27:00 +080052};
53
54#define LIST_HEAD_INIT(name) { &(name), &(name) }
55
56#define LIST_HEAD(name) \
hualing chen002e5b92022-02-23 17:51:21 +080057 struct list_head name = LIST_HEAD_INIT(name)
hualing chenb31a6c62020-01-13 17:27:00 +080058
59static inline void INIT_LIST_HEAD(struct list_head *list)
60{
Wentao MA9a164002022-08-29 11:20:24 +080061 list->c_next = list;
62 list->c_prev = list;
hualing chenb31a6c62020-01-13 17:27:00 +080063}
64
65/*
Wentao MA9a164002022-08-29 11:20:24 +080066 * Insert a c_new entry between two known consecutive entries.
hualing chenb31a6c62020-01-13 17:27:00 +080067 *
68 * This is only for internal list manipulation where we know
Wentao MA9a164002022-08-29 11:20:24 +080069 * the c_prev/c_next entries already!
hualing chenb31a6c62020-01-13 17:27:00 +080070 */
71#ifndef CONFIG_DEBUG_LIST
Wentao MA9a164002022-08-29 11:20:24 +080072static inline void __list_add(struct list_head *c_new,
73 struct list_head *c_prev,
74 struct list_head *c_next)
hualing chenb31a6c62020-01-13 17:27:00 +080075{
Wentao MA9a164002022-08-29 11:20:24 +080076 c_next->c_prev = c_new;
77 c_new->c_next = c_next;
78 c_new->c_prev = c_prev;
79 c_prev->c_next = c_new;
hualing chenb31a6c62020-01-13 17:27:00 +080080}
81#else
Wentao MA9a164002022-08-29 11:20:24 +080082extern void __list_add(struct list_head *c_new,
hualing chen002e5b92022-02-23 17:51:21 +080083 struct list_head *prev,
84 struct list_head *next);
hualing chenb31a6c62020-01-13 17:27:00 +080085#endif
86
87/**
Wentao MA9a164002022-08-29 11:20:24 +080088 * list_add - add a c_new entry
89 * @c_new: c_new entry to be added
hualing chenb31a6c62020-01-13 17:27:00 +080090 * @head: list head to add it after
91 *
Wentao MA9a164002022-08-29 11:20:24 +080092 * Insert a c_new entry after the specified head.
hualing chenb31a6c62020-01-13 17:27:00 +080093 * This is good for implementing stacks.
94 */
Wentao MA9a164002022-08-29 11:20:24 +080095static inline void list_add(struct list_head *c_new, struct list_head *head)
hualing chenb31a6c62020-01-13 17:27:00 +080096{
wentao.maa22bc852022-10-13 12:18:06 +080097 __list_add((struct list_head*)c_new, head, head->c_next);
hualing chenb31a6c62020-01-13 17:27:00 +080098}
99
100
101/**
Wentao MA9a164002022-08-29 11:20:24 +0800102 * list_add_tail - add a c_new entry
103 * @c_new: c_new entry to be added
hualing chenb31a6c62020-01-13 17:27:00 +0800104 * @head: list head to add it before
105 *
Wentao MA9a164002022-08-29 11:20:24 +0800106 * Insert a c_new entry before the specified head.
hualing chenb31a6c62020-01-13 17:27:00 +0800107 * This is useful for implementing queues.
108 */
Wentao MA9a164002022-08-29 11:20:24 +0800109static inline void list_add_tail(struct list_head *c_new, struct list_head *head)
hualing chenb31a6c62020-01-13 17:27:00 +0800110{
Wentao MA9a164002022-08-29 11:20:24 +0800111 __list_add(c_new, head->c_prev, head);
hualing chenb31a6c62020-01-13 17:27:00 +0800112}
113
114/*
Wentao MA9a164002022-08-29 11:20:24 +0800115 * Delete a list entry by making the c_prev/c_next entries
hualing chenb31a6c62020-01-13 17:27:00 +0800116 * point to each other.
117 *
118 * This is only for internal list manipulation where we know
Wentao MA9a164002022-08-29 11:20:24 +0800119 * the c_prev/c_next entries already!
hualing chenb31a6c62020-01-13 17:27:00 +0800120 */
Wentao MA9a164002022-08-29 11:20:24 +0800121static inline void __list_del(struct list_head * c_prev, struct list_head * c_next)
hualing chenb31a6c62020-01-13 17:27:00 +0800122{
Wentao MA9a164002022-08-29 11:20:24 +0800123 c_next->c_prev = c_prev;
124 c_prev->c_next = c_next;
hualing chenb31a6c62020-01-13 17:27:00 +0800125}
126
127/**
128 * list_del - deletes entry from list.
129 * @entry: the element to delete from the list.
130 * Note: list_empty() on entry does not return true after this, the entry is
131 * in an undefined state.
132 */
133#ifndef CONFIG_DEBUG_LIST
134static inline void list_del(struct list_head *entry)
135{
Wentao MA9a164002022-08-29 11:20:24 +0800136 __list_del(entry->c_prev, entry->c_next);
137 entry->c_next = (struct list_head *)LIST_POISON1;
138 entry->c_prev = (struct list_head *)LIST_POISON2;
hualing chenb31a6c62020-01-13 17:27:00 +0800139}
140#else
141extern void list_del(struct list_head *entry);
142#endif
143
144/**
Wentao MA9a164002022-08-29 11:20:24 +0800145 * list_replace - replace old entry by c_new one
hualing chenb31a6c62020-01-13 17:27:00 +0800146 * @old : the element to be replaced
Wentao MA9a164002022-08-29 11:20:24 +0800147 * @c_new : the c_new element to insert
hualing chenb31a6c62020-01-13 17:27:00 +0800148 *
149 * If @old was empty, it will be overwritten.
150 */
151static inline void list_replace(struct list_head *old,
Wentao MA9a164002022-08-29 11:20:24 +0800152 struct list_head *c_new)
hualing chenb31a6c62020-01-13 17:27:00 +0800153{
Wentao MA9a164002022-08-29 11:20:24 +0800154 c_new->c_next = old->c_next;
155 c_new->c_next->c_prev = c_new;
156 c_new->c_prev = old->c_prev;
157 c_new->c_prev->c_next = c_new;
hualing chenb31a6c62020-01-13 17:27:00 +0800158}
159
160static inline void list_replace_init(struct list_head *old,
Wentao MA9a164002022-08-29 11:20:24 +0800161 struct list_head *c_new)
hualing chenb31a6c62020-01-13 17:27:00 +0800162{
Wentao MA9a164002022-08-29 11:20:24 +0800163 list_replace(old, c_new);
hualing chen002e5b92022-02-23 17:51:21 +0800164 INIT_LIST_HEAD(old);
hualing chenb31a6c62020-01-13 17:27:00 +0800165}
166
167/**
168 * list_del_init - deletes entry from list and reinitialize it.
169 * @entry: the element to delete from the list.
170 */
171static inline void list_del_init(struct list_head *entry)
172{
Wentao MA9a164002022-08-29 11:20:24 +0800173 __list_del(entry->c_prev, entry->c_next);
hualing chen002e5b92022-02-23 17:51:21 +0800174 INIT_LIST_HEAD(entry);
hualing chenb31a6c62020-01-13 17:27:00 +0800175}
176
177/**
178 * list_move - delete from one list and add as another's head
179 * @list: the entry to move
180 * @head: the head that will precede our entry
181 */
182static inline void list_move(struct list_head *list, struct list_head *head)
183{
Wentao MA9a164002022-08-29 11:20:24 +0800184 __list_del(list->c_prev, list->c_next);
hualing chen002e5b92022-02-23 17:51:21 +0800185 list_add(list, head);
hualing chenb31a6c62020-01-13 17:27:00 +0800186}
187
188/**
189 * list_move_tail - delete from one list and add as another's tail
190 * @list: the entry to move
191 * @head: the head that will follow our entry
192 */
193static inline void list_move_tail(struct list_head *list,
hualing chen002e5b92022-02-23 17:51:21 +0800194 struct list_head *head)
hualing chenb31a6c62020-01-13 17:27:00 +0800195{
Wentao MA9a164002022-08-29 11:20:24 +0800196 __list_del(list->c_prev, list->c_next);
hualing chen002e5b92022-02-23 17:51:21 +0800197 list_add_tail(list, head);
hualing chenb31a6c62020-01-13 17:27:00 +0800198}
199
200/**
201 * list_is_last - tests whether @list is the last entry in list @head
202 * @list: the entry to test
203 * @head: the head of the list
204 */
205static inline int list_is_last(const struct list_head *list,
hualing chen002e5b92022-02-23 17:51:21 +0800206 const struct list_head *head)
hualing chenb31a6c62020-01-13 17:27:00 +0800207{
Wentao MA9a164002022-08-29 11:20:24 +0800208 return list->c_next == head;
hualing chenb31a6c62020-01-13 17:27:00 +0800209}
210
211/**
212 * list_empty - tests whether a list is empty
213 * @head: the list to test.
214 */
215static inline int list_empty(const struct list_head *head)
216{
Wentao MA9a164002022-08-29 11:20:24 +0800217 return head->c_next == head;
hualing chenb31a6c62020-01-13 17:27:00 +0800218}
219
220/**
221 * list_empty_careful - tests whether a list is empty and not being modified
222 * @head: the list to test
223 *
224 * Description:
225 * tests whether a list is empty _and_ checks that no other CPU might be
Wentao MA9a164002022-08-29 11:20:24 +0800226 * in the process of modifying either member (c_next or c_prev)
hualing chenb31a6c62020-01-13 17:27:00 +0800227 *
228 * NOTE: using list_empty_careful() without synchronization
229 * can only be safe if the only activity that can happen
230 * to the list entry is list_del_init(). Eg. it cannot be used
231 * if another CPU could re-list_add() it.
232 */
233static inline int list_empty_careful(const struct list_head *head)
234{
Wentao MA9a164002022-08-29 11:20:24 +0800235 struct list_head *c_next = head->c_next;
236 return (c_next == head) && (c_next == head->c_prev);
hualing chenb31a6c62020-01-13 17:27:00 +0800237}
238
239/**
240 * list_rotate_left - rotate the list to the left
241 * @head: the head of the list
242 */
243static inline void list_rotate_left(struct list_head *head)
244{
hualing chen002e5b92022-02-23 17:51:21 +0800245 struct list_head *first;
hualing chenb31a6c62020-01-13 17:27:00 +0800246
hualing chen002e5b92022-02-23 17:51:21 +0800247 if (!list_empty(head)) {
Wentao MA9a164002022-08-29 11:20:24 +0800248 first = head->c_next;
hualing chen002e5b92022-02-23 17:51:21 +0800249 list_move_tail(first, head);
250 }
hualing chenb31a6c62020-01-13 17:27:00 +0800251}
252
253/**
254 * list_is_singular - tests whether a list has just one entry.
255 * @head: the list to test.
256 */
257static inline int list_is_singular(const struct list_head *head)
258{
Wentao MA9a164002022-08-29 11:20:24 +0800259 return !list_empty(head) && (head->c_next == head->c_prev);
hualing chenb31a6c62020-01-13 17:27:00 +0800260}
261
262static inline void __list_cut_position(struct list_head *list,
hualing chen002e5b92022-02-23 17:51:21 +0800263 struct list_head *head, struct list_head *entry)
hualing chenb31a6c62020-01-13 17:27:00 +0800264{
Wentao MA9a164002022-08-29 11:20:24 +0800265 struct list_head *c_new_first = entry->c_next;
266 list->c_next = head->c_next;
267 list->c_next->c_prev = list;
268 list->c_prev = entry;
269 entry->c_next = list;
270 head->c_next = c_new_first;
271 c_new_first->c_prev = head;
hualing chenb31a6c62020-01-13 17:27:00 +0800272}
273
274/**
275 * list_cut_position - cut a list into two
Wentao MA9a164002022-08-29 11:20:24 +0800276 * @list: a c_new list to add all removed entries
hualing chenb31a6c62020-01-13 17:27:00 +0800277 * @head: a list with entries
278 * @entry: an entry within head, could be the head itself
hualing chen002e5b92022-02-23 17:51:21 +0800279 * and if so we won't cut the list
hualing chenb31a6c62020-01-13 17:27:00 +0800280 *
281 * This helper moves the initial part of @head, up to and
282 * including @entry, from @head to @list. You should
283 * pass on @entry an element you know is on @head. @list
284 * should be an empty list or a list you do not care about
285 * losing its data.
286 *
287 */
288static inline void list_cut_position(struct list_head *list,
hualing chen002e5b92022-02-23 17:51:21 +0800289 struct list_head *head, struct list_head *entry)
hualing chenb31a6c62020-01-13 17:27:00 +0800290{
hualing chen002e5b92022-02-23 17:51:21 +0800291 if (list_empty(head))
292 return;
293 if (list_is_singular(head) &&
Wentao MA9a164002022-08-29 11:20:24 +0800294 (head->c_next != entry && head != entry))
hualing chen002e5b92022-02-23 17:51:21 +0800295 return;
296 if (entry == head)
297 INIT_LIST_HEAD(list);
298 else
299 __list_cut_position(list, head, entry);
hualing chenb31a6c62020-01-13 17:27:00 +0800300}
301
302static inline void __list_splice(const struct list_head *list,
Wentao MA9a164002022-08-29 11:20:24 +0800303 struct list_head *c_prev,
304 struct list_head *c_next)
hualing chenb31a6c62020-01-13 17:27:00 +0800305{
Wentao MA9a164002022-08-29 11:20:24 +0800306 struct list_head *first = list->c_next;
307 struct list_head *last = list->c_prev;
hualing chenb31a6c62020-01-13 17:27:00 +0800308
Wentao MA9a164002022-08-29 11:20:24 +0800309 first->c_prev = c_prev;
310 c_prev->c_next = first;
hualing chenb31a6c62020-01-13 17:27:00 +0800311
Wentao MA9a164002022-08-29 11:20:24 +0800312 last->c_next = c_next;
313 c_next->c_prev = last;
hualing chenb31a6c62020-01-13 17:27:00 +0800314}
315
316/**
317 * list_splice - join two lists, this is designed for stacks
Wentao MA9a164002022-08-29 11:20:24 +0800318 * @list: the c_new list to add.
hualing chenb31a6c62020-01-13 17:27:00 +0800319 * @head: the place to add it in the first list.
320 */
321static inline void list_splice(const struct list_head *list,
hualing chen002e5b92022-02-23 17:51:21 +0800322 struct list_head *head)
hualing chenb31a6c62020-01-13 17:27:00 +0800323{
hualing chen002e5b92022-02-23 17:51:21 +0800324 if (!list_empty(list))
Wentao MA9a164002022-08-29 11:20:24 +0800325 __list_splice(list, head, head->c_next);
hualing chenb31a6c62020-01-13 17:27:00 +0800326}
327
328/**
329 * list_splice_tail - join two lists, each list being a queue
Wentao MA9a164002022-08-29 11:20:24 +0800330 * @list: the c_new list to add.
hualing chenb31a6c62020-01-13 17:27:00 +0800331 * @head: the place to add it in the first list.
332 */
333static inline void list_splice_tail(struct list_head *list,
hualing chen002e5b92022-02-23 17:51:21 +0800334 struct list_head *head)
hualing chenb31a6c62020-01-13 17:27:00 +0800335{
hualing chen002e5b92022-02-23 17:51:21 +0800336 if (!list_empty(list))
Wentao MA9a164002022-08-29 11:20:24 +0800337 __list_splice(list, head->c_prev, head);
hualing chenb31a6c62020-01-13 17:27:00 +0800338}
339
340/**
341 * list_splice_init - join two lists and reinitialise the emptied list.
Wentao MA9a164002022-08-29 11:20:24 +0800342 * @list: the c_new list to add.
hualing chenb31a6c62020-01-13 17:27:00 +0800343 * @head: the place to add it in the first list.
344 *
345 * The list at @list is reinitialised
346 */
347static inline void list_splice_init(struct list_head *list,
hualing chen002e5b92022-02-23 17:51:21 +0800348 struct list_head *head)
hualing chenb31a6c62020-01-13 17:27:00 +0800349{
hualing chen002e5b92022-02-23 17:51:21 +0800350 if (!list_empty(list)) {
Wentao MA9a164002022-08-29 11:20:24 +0800351 __list_splice(list, head, head->c_next);
hualing chen002e5b92022-02-23 17:51:21 +0800352 INIT_LIST_HEAD(list);
353 }
hualing chenb31a6c62020-01-13 17:27:00 +0800354}
355
356/**
357 * list_splice_tail_init - join two lists and reinitialise the emptied list
Wentao MA9a164002022-08-29 11:20:24 +0800358 * @list: the c_new list to add.
hualing chenb31a6c62020-01-13 17:27:00 +0800359 * @head: the place to add it in the first list.
360 *
361 * Each of the lists is a queue.
362 * The list at @list is reinitialised
363 */
364static inline void list_splice_tail_init(struct list_head *list,
hualing chen002e5b92022-02-23 17:51:21 +0800365 struct list_head *head)
hualing chenb31a6c62020-01-13 17:27:00 +0800366{
hualing chen002e5b92022-02-23 17:51:21 +0800367 if (!list_empty(list)) {
Wentao MA9a164002022-08-29 11:20:24 +0800368 __list_splice(list, head->c_prev, head);
hualing chen002e5b92022-02-23 17:51:21 +0800369 INIT_LIST_HEAD(list);
370 }
hualing chenb31a6c62020-01-13 17:27:00 +0800371}
372
373/**
374 * list_entry - get the struct for this entry
hualing chen002e5b92022-02-23 17:51:21 +0800375 * @ptr: the &struct list_head pointer.
376 * @type: the type of the struct this is embedded in.
377 * @member: the name of the list_struct within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800378 */
379#define list_entry(ptr, type, member) \
hualing chen002e5b92022-02-23 17:51:21 +0800380 container_of(ptr, type, member)
hualing chenb31a6c62020-01-13 17:27:00 +0800381
382/**
383 * list_first_entry - get the first element from a list
hualing chen002e5b92022-02-23 17:51:21 +0800384 * @ptr: the list head to take the element from.
385 * @type: the type of the struct this is embedded in.
386 * @member: the name of the list_struct within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800387 *
388 * Note, that list is expected to be not empty.
389 */
390#define list_first_entry(ptr, type, member) \
Wentao MA9a164002022-08-29 11:20:24 +0800391 list_entry((ptr)->c_next, type, member)
hualing chenb31a6c62020-01-13 17:27:00 +0800392
393/**
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800394 * list_last_entry - get the last element from a list
hualing chen002e5b92022-02-23 17:51:21 +0800395 * @ptr: the list head to take the element from.
396 * @type: the type of the struct this is embedded in.
397 * @member: the name of the list_head within the struct.
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800398 *
399 * Note, that list is expected to be not empty.
400 */
401#define list_last_entry(ptr, type, member) \
Wentao MA9a164002022-08-29 11:20:24 +0800402 list_entry((ptr)->c_prev, type, member)
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800403
404/**
Wentao MA9a164002022-08-29 11:20:24 +0800405 * list_next_entry - get the c_next element in list
hualing chen002e5b92022-02-23 17:51:21 +0800406 * @pos: the type * to cursor
407 * @member: the name of the list_head within the struct.
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800408 */
409#define list_next_entry(pos, member) \
Wentao MA9a164002022-08-29 11:20:24 +0800410 list_entry((pos)->member.c_next, typeof(*(pos)), member)
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800411
412/**
Wentao MA9a164002022-08-29 11:20:24 +0800413 * list_prev_entry - get the c_prev element in list
hualing chen002e5b92022-02-23 17:51:21 +0800414 * @pos: the type * to cursor
415 * @member: the name of the list_head within the struct.
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800416 */
417#define list_prev_entry(pos, member) \
Wentao MA9a164002022-08-29 11:20:24 +0800418 list_entry((pos)->member.c_prev, typeof(*(pos)), member)
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800419
420/**
hualing chen002e5b92022-02-23 17:51:21 +0800421 * list_for_each - iterate over a list
422 * @pos: the &struct list_head to use as a loop cursor.
423 * @head: the head for your list.
hualing chenb31a6c62020-01-13 17:27:00 +0800424 */
425#define list_for_each(pos, head) \
Wentao MA9a164002022-08-29 11:20:24 +0800426 for (pos = (head)->c_next; prefetch(pos->c_next), pos != (head); \
427 pos = pos->c_next)
hualing chenb31a6c62020-01-13 17:27:00 +0800428
429/**
hualing chen002e5b92022-02-23 17:51:21 +0800430 * __list_for_each - iterate over a list
431 * @pos: the &struct list_head to use as a loop cursor.
432 * @head: the head for your list.
hualing chenb31a6c62020-01-13 17:27:00 +0800433 *
434 * This variant differs from list_for_each() in that it's the
435 * simplest possible list iteration code, no prefetching is done.
436 * Use this for code that knows the list to be very short (empty
437 * or 1 entry) most of the time.
438 */
439#define __list_for_each(pos, head) \
Wentao MA9a164002022-08-29 11:20:24 +0800440 for (pos = (head)->c_next; pos != (head); pos = pos->c_next)
hualing chenb31a6c62020-01-13 17:27:00 +0800441
442/**
hualing chen002e5b92022-02-23 17:51:21 +0800443 * list_for_each_prev - iterate over a list backwards
444 * @pos: the &struct list_head to use as a loop cursor.
445 * @head: the head for your list.
hualing chenb31a6c62020-01-13 17:27:00 +0800446 */
447#define list_for_each_prev(pos, head) \
Wentao MA9a164002022-08-29 11:20:24 +0800448 for (pos = (head)->c_prev; prefetch(pos->c_prev), pos != (head); \
449 pos = pos->c_prev)
hualing chenb31a6c62020-01-13 17:27:00 +0800450
451/**
452 * list_for_each_safe - iterate over a list safe against removal of list entry
hualing chen002e5b92022-02-23 17:51:21 +0800453 * @pos: the &struct list_head to use as a loop cursor.
454 * @n: another &struct list_head to use as temporary storage
455 * @head: the head for your list.
hualing chenb31a6c62020-01-13 17:27:00 +0800456 */
457#define list_for_each_safe(pos, n, head) \
Wentao MA9a164002022-08-29 11:20:24 +0800458 for (pos = (head)->c_next, n = pos->c_next; pos != (head); \
459 pos = n, n = pos->c_next)
hualing chenb31a6c62020-01-13 17:27:00 +0800460
461/**
462 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
hualing chen002e5b92022-02-23 17:51:21 +0800463 * @pos: the &struct list_head to use as a loop cursor.
464 * @n: another &struct list_head to use as temporary storage
465 * @head: the head for your list.
hualing chenb31a6c62020-01-13 17:27:00 +0800466 */
467#define list_for_each_prev_safe(pos, n, head) \
Wentao MA9a164002022-08-29 11:20:24 +0800468 for (pos = (head)->c_prev, n = pos->c_prev; \
469 prefetch(pos->c_prev), pos != (head); \
470 pos = n, n = pos->c_prev)
hualing chenb31a6c62020-01-13 17:27:00 +0800471
472/**
hualing chen002e5b92022-02-23 17:51:21 +0800473 * list_for_each_entry - iterate over list of given type
474 * @pos: the type * to use as a loop cursor.
475 * @head: the head for your list.
476 * @member: the name of the list_struct within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800477 */
hualing chen002e5b92022-02-23 17:51:21 +0800478#define list_for_each_entry(pos, head, member) \
Wentao MA9a164002022-08-29 11:20:24 +0800479 for (pos = list_entry((head)->c_next, typeof(*pos), member); \
480 prefetch(pos->member.c_next), &pos->member != (head); \
481 pos = list_entry(pos->member.c_next, typeof(*pos), member))
hualing chenb31a6c62020-01-13 17:27:00 +0800482
483/**
484 * list_for_each_entry_reverse - iterate backwards over list of given type.
hualing chen002e5b92022-02-23 17:51:21 +0800485 * @pos: the type * to use as a loop cursor.
486 * @head: the head for your list.
487 * @member: the name of the list_struct within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800488 */
hualing chen002e5b92022-02-23 17:51:21 +0800489#define list_for_each_entry_reverse(pos, head, member) \
Wentao MA9a164002022-08-29 11:20:24 +0800490 for (pos = list_entry((head)->c_prev, typeof(*pos), member); \
491 prefetch(pos->member.c_prev), &pos->member != (head); \
492 pos = list_entry(pos->member.c_prev, typeof(*pos), member))
hualing chenb31a6c62020-01-13 17:27:00 +0800493
494/**
495 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
hualing chen002e5b92022-02-23 17:51:21 +0800496 * @pos: the type * to use as a start point
497 * @head: the head of the list
498 * @member: the name of the list_struct within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800499 *
500 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
501 */
502#define list_prepare_entry(pos, head, member) \
hualing chen002e5b92022-02-23 17:51:21 +0800503 ((pos) ? : list_entry(head, typeof(*pos), member))
hualing chenb31a6c62020-01-13 17:27:00 +0800504
505/**
506 * list_for_each_entry_continue - continue iteration over list of given type
hualing chen002e5b92022-02-23 17:51:21 +0800507 * @pos: the type * to use as a loop cursor.
508 * @head: the head for your list.
509 * @member: the name of the list_struct within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800510 *
511 * Continue to iterate over list of given type, continuing after
512 * the current position.
513 */
hualing chen002e5b92022-02-23 17:51:21 +0800514#define list_for_each_entry_continue(pos, head, member) \
Wentao MA9a164002022-08-29 11:20:24 +0800515 for (pos = list_entry(pos->member.c_next, typeof(*pos), member); \
516 prefetch(pos->member.c_next), &pos->member != (head); \
517 pos = list_entry(pos->member.c_next, typeof(*pos), member))
hualing chenb31a6c62020-01-13 17:27:00 +0800518
519/**
520 * list_for_each_entry_continue_reverse - iterate backwards from the given point
hualing chen002e5b92022-02-23 17:51:21 +0800521 * @pos: the type * to use as a loop cursor.
522 * @head: the head for your list.
523 * @member: the name of the list_struct within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800524 *
525 * Start to iterate over list of given type backwards, continuing after
526 * the current position.
527 */
hualing chen002e5b92022-02-23 17:51:21 +0800528#define list_for_each_entry_continue_reverse(pos, head, member) \
Wentao MA9a164002022-08-29 11:20:24 +0800529 for (pos = list_entry(pos->member.c_prev, typeof(*pos), member); \
530 prefetch(pos->member.c_prev), &pos->member != (head); \
531 pos = list_entry(pos->member.c_prev, typeof(*pos), member))
hualing chenb31a6c62020-01-13 17:27:00 +0800532
533/**
534 * list_for_each_entry_from - iterate over list of given type from the current point
hualing chen002e5b92022-02-23 17:51:21 +0800535 * @pos: the type * to use as a loop cursor.
536 * @head: the head for your list.
537 * @member: the name of the list_struct within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800538 *
539 * Iterate over list of given type, continuing from current position.
540 */
hualing chen002e5b92022-02-23 17:51:21 +0800541#define list_for_each_entry_from(pos, head, member) \
Wentao MA9a164002022-08-29 11:20:24 +0800542 for (; prefetch(pos->member.c_next), &pos->member != (head); \
543 pos = list_entry(pos->member.c_next, typeof(*pos), member))
hualing chenb31a6c62020-01-13 17:27:00 +0800544
545/**
546 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
hualing chen002e5b92022-02-23 17:51:21 +0800547 * @pos: the type * to use as a loop cursor.
548 * @n: another type * to use as temporary storage
549 * @head: the head for your list.
550 * @member: the name of the list_struct within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800551 */
hualing chen002e5b92022-02-23 17:51:21 +0800552#define list_for_each_entry_safe(pos, n, head, member) \
Wentao MA9a164002022-08-29 11:20:24 +0800553 for (pos = list_entry((head)->c_next, typeof(*pos), member), \
554 n = list_entry(pos->member.c_next, typeof(*pos), member); \
Wentao MAe2a2db92024-08-26 16:21:41 +0800555 (pos > 0) && &pos->member != (head); \
Wentao MA9a164002022-08-29 11:20:24 +0800556 pos = n, n = list_entry(n->member.c_next, typeof(*n), member))
hualing chenb31a6c62020-01-13 17:27:00 +0800557
558/**
559 * list_for_each_entry_safe_continue - continue list iteration safe against removal
hualing chen002e5b92022-02-23 17:51:21 +0800560 * @pos: the type * to use as a loop cursor.
561 * @n: another type * to use as temporary storage
562 * @head: the head for your list.
563 * @member: the name of the list_struct within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800564 *
565 * Iterate over list of given type, continuing after current point,
566 * safe against removal of list entry.
567 */
hualing chen002e5b92022-02-23 17:51:21 +0800568#define list_for_each_entry_safe_continue(pos, n, head, member) \
Wentao MA9a164002022-08-29 11:20:24 +0800569 for (pos = list_entry(pos->member.c_next, typeof(*pos), member), \
570 n = list_entry(pos->member.c_next, typeof(*pos), member); \
hualing chen002e5b92022-02-23 17:51:21 +0800571 &pos->member != (head); \
Wentao MA9a164002022-08-29 11:20:24 +0800572 pos = n, n = list_entry(n->member.c_next, typeof(*n), member))
hualing chenb31a6c62020-01-13 17:27:00 +0800573
574/**
575 * list_for_each_entry_safe_from - iterate over list from current point safe against removal
hualing chen002e5b92022-02-23 17:51:21 +0800576 * @pos: the type * to use as a loop cursor.
577 * @n: another type * to use as temporary storage
578 * @head: the head for your list.
579 * @member: the name of the list_struct within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800580 *
581 * Iterate over list of given type from current point, safe against
582 * removal of list entry.
583 */
hualing chen002e5b92022-02-23 17:51:21 +0800584#define list_for_each_entry_safe_from(pos, n, head, member) \
Wentao MA9a164002022-08-29 11:20:24 +0800585 for (n = list_entry(pos->member.c_next, typeof(*pos), member); \
hualing chen002e5b92022-02-23 17:51:21 +0800586 &pos->member != (head); \
Wentao MA9a164002022-08-29 11:20:24 +0800587 pos = n, n = list_entry(n->member.c_next, typeof(*n), member))
hualing chenb31a6c62020-01-13 17:27:00 +0800588
589/**
590 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
hualing chen002e5b92022-02-23 17:51:21 +0800591 * @pos: the type * to use as a loop cursor.
592 * @n: another type * to use as temporary storage
593 * @head: the head for your list.
594 * @member: the name of the list_struct within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800595 *
596 * Iterate backwards over list of given type, safe against removal
597 * of list entry.
598 */
hualing chen002e5b92022-02-23 17:51:21 +0800599#define list_for_each_entry_safe_reverse(pos, n, head, member) \
Wentao MA9a164002022-08-29 11:20:24 +0800600 for (pos = list_entry((head)->c_prev, typeof(*pos), member), \
601 n = list_entry(pos->member.c_prev, typeof(*pos), member); \
hualing chen002e5b92022-02-23 17:51:21 +0800602 &pos->member != (head); \
Wentao MA9a164002022-08-29 11:20:24 +0800603 pos = n, n = list_entry(n->member.c_prev, typeof(*n), member))
hualing chenb31a6c62020-01-13 17:27:00 +0800604
605/*
606 * Double linked lists with a single pointer list head.
607 * Mostly useful for hash tables where the two pointer list head is
608 * too wasteful.
609 * You lose the ability to access the tail in O(1).
610 */
611
612struct hlist_head {
hualing chen002e5b92022-02-23 17:51:21 +0800613 struct hlist_node *first;
hualing chenb31a6c62020-01-13 17:27:00 +0800614};
615
616struct hlist_node {
Wentao MA9a164002022-08-29 11:20:24 +0800617 struct hlist_node *c_next, **pc_prev;
hualing chenb31a6c62020-01-13 17:27:00 +0800618};
619
620#define HLIST_HEAD_INIT { .first = NULL }
621#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
622#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
623static inline void INIT_HLIST_NODE(struct hlist_node *h)
624{
Wentao MA9a164002022-08-29 11:20:24 +0800625 h->c_next = NULL;
626 h->pc_prev = NULL;
hualing chenb31a6c62020-01-13 17:27:00 +0800627}
628
629static inline int hlist_unhashed(const struct hlist_node *h)
630{
Wentao MA9a164002022-08-29 11:20:24 +0800631 return !h->pc_prev;
hualing chenb31a6c62020-01-13 17:27:00 +0800632}
633
634static inline int hlist_empty(const struct hlist_head *h)
635{
hualing chen002e5b92022-02-23 17:51:21 +0800636 return !h->first;
hualing chenb31a6c62020-01-13 17:27:00 +0800637}
638
639static inline void __hlist_del(struct hlist_node *n)
640{
Wentao MA9a164002022-08-29 11:20:24 +0800641 struct hlist_node *c_next = n->c_next;
642 struct hlist_node **pc_prev = n->pc_prev;
643 *pc_prev = c_next;
644 if (c_next)
645 c_next->pc_prev = pc_prev;
hualing chenb31a6c62020-01-13 17:27:00 +0800646}
647
648static inline void hlist_del(struct hlist_node *n)
649{
hualing chen002e5b92022-02-23 17:51:21 +0800650 __hlist_del(n);
Wentao MA9a164002022-08-29 11:20:24 +0800651 n->c_next = (struct hlist_node *)LIST_POISON1;
652 n->pc_prev = (struct hlist_node **)LIST_POISON2;
hualing chenb31a6c62020-01-13 17:27:00 +0800653}
654
655static inline void hlist_del_init(struct hlist_node *n)
656{
hualing chen002e5b92022-02-23 17:51:21 +0800657 if (!hlist_unhashed(n)) {
658 __hlist_del(n);
659 INIT_HLIST_NODE(n);
660 }
hualing chenb31a6c62020-01-13 17:27:00 +0800661}
662
663static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
664{
hualing chen002e5b92022-02-23 17:51:21 +0800665 struct hlist_node *first = h->first;
Wentao MA9a164002022-08-29 11:20:24 +0800666 n->c_next = first;
hualing chen002e5b92022-02-23 17:51:21 +0800667 if (first)
Wentao MA9a164002022-08-29 11:20:24 +0800668 first->pc_prev = &n->c_next;
hualing chen002e5b92022-02-23 17:51:21 +0800669 h->first = n;
Wentao MA9a164002022-08-29 11:20:24 +0800670 n->pc_prev = &h->first;
hualing chenb31a6c62020-01-13 17:27:00 +0800671}
672
Wentao MA9a164002022-08-29 11:20:24 +0800673/* c_next must be != NULL */
hualing chenb31a6c62020-01-13 17:27:00 +0800674static inline void hlist_add_before(struct hlist_node *n,
Wentao MA9a164002022-08-29 11:20:24 +0800675 struct hlist_node *c_next)
hualing chenb31a6c62020-01-13 17:27:00 +0800676{
Wentao MA9a164002022-08-29 11:20:24 +0800677 n->pc_prev = c_next->pc_prev;
678 n->c_next = c_next;
679 c_next->pc_prev = &n->c_next;
680 *(n->pc_prev) = n;
hualing chenb31a6c62020-01-13 17:27:00 +0800681}
682
683static inline void hlist_add_after(struct hlist_node *n,
Wentao MA9a164002022-08-29 11:20:24 +0800684 struct hlist_node *c_next)
hualing chenb31a6c62020-01-13 17:27:00 +0800685{
Wentao MA9a164002022-08-29 11:20:24 +0800686 c_next->c_next = n->c_next;
687 n->c_next = c_next;
688 c_next->pc_prev = &n->c_next;
hualing chenb31a6c62020-01-13 17:27:00 +0800689
Wentao MA9a164002022-08-29 11:20:24 +0800690 if(c_next->c_next)
691 c_next->c_next->pc_prev = &c_next->c_next;
hualing chenb31a6c62020-01-13 17:27:00 +0800692}
693
694/*
Wentao MA9a164002022-08-29 11:20:24 +0800695 * Move a list from one list head to another. Fixup the pc_prev
hualing chenb31a6c62020-01-13 17:27:00 +0800696 * reference of the first entry if it exists.
697 */
698static inline void hlist_move_list(struct hlist_head *old,
Wentao MA9a164002022-08-29 11:20:24 +0800699 struct hlist_head *c_new)
hualing chenb31a6c62020-01-13 17:27:00 +0800700{
Wentao MA9a164002022-08-29 11:20:24 +0800701 c_new->first = old->first;
702 if (c_new->first)
703 c_new->first->pc_prev = &c_new->first;
hualing chen002e5b92022-02-23 17:51:21 +0800704 old->first = NULL;
hualing chenb31a6c62020-01-13 17:27:00 +0800705}
706
707#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
708
709#define hlist_for_each(pos, head) \
Wentao MA9a164002022-08-29 11:20:24 +0800710 for (pos = (head)->first; pos && ({ prefetch(pos->c_next); 1; }); \
711 pos = pos->c_next)
hualing chenb31a6c62020-01-13 17:27:00 +0800712
713#define hlist_for_each_safe(pos, n, head) \
Wentao MA9a164002022-08-29 11:20:24 +0800714 for (pos = (head)->first; pos && ({ n = pos->c_next; 1; }); \
hualing chen002e5b92022-02-23 17:51:21 +0800715 pos = n)
hualing chenb31a6c62020-01-13 17:27:00 +0800716
717/**
hualing chen002e5b92022-02-23 17:51:21 +0800718 * hlist_for_each_entry - iterate over list of given type
719 * @tpos: the type * to use as a loop cursor.
720 * @pos: the &struct hlist_node to use as a loop cursor.
721 * @head: the head for your list.
722 * @member: the name of the hlist_node within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800723 */
hualing chen002e5b92022-02-23 17:51:21 +0800724#define hlist_for_each_entry(tpos, pos, head, member) \
725 for (pos = (head)->first; \
Wentao MA9a164002022-08-29 11:20:24 +0800726 pos && ({ prefetch(pos->c_next); 1;}) && \
hualing chen002e5b92022-02-23 17:51:21 +0800727 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
Wentao MA9a164002022-08-29 11:20:24 +0800728 pos = pos->c_next)
hualing chenb31a6c62020-01-13 17:27:00 +0800729
730/**
731 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
hualing chen002e5b92022-02-23 17:51:21 +0800732 * @tpos: the type * to use as a loop cursor.
733 * @pos: the &struct hlist_node to use as a loop cursor.
734 * @member: the name of the hlist_node within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800735 */
hualing chen002e5b92022-02-23 17:51:21 +0800736#define hlist_for_each_entry_continue(tpos, pos, member) \
Wentao MA9a164002022-08-29 11:20:24 +0800737 for (pos = (pos)->c_next; \
738 pos && ({ prefetch(pos->c_next); 1;}) && \
hualing chen002e5b92022-02-23 17:51:21 +0800739 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
Wentao MA9a164002022-08-29 11:20:24 +0800740 pos = pos->c_next)
hualing chenb31a6c62020-01-13 17:27:00 +0800741
742/**
743 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
hualing chen002e5b92022-02-23 17:51:21 +0800744 * @tpos: the type * to use as a loop cursor.
745 * @pos: the &struct hlist_node to use as a loop cursor.
746 * @member: the name of the hlist_node within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800747 */
hualing chen002e5b92022-02-23 17:51:21 +0800748#define hlist_for_each_entry_from(tpos, pos, member) \
Wentao MA9a164002022-08-29 11:20:24 +0800749 for (; pos && ({ prefetch(pos->c_next); 1;}) && \
hualing chen002e5b92022-02-23 17:51:21 +0800750 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
Wentao MA9a164002022-08-29 11:20:24 +0800751 pos = pos->c_next)
hualing chenb31a6c62020-01-13 17:27:00 +0800752
753/**
754 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
hualing chen002e5b92022-02-23 17:51:21 +0800755 * @tpos: the type * to use as a loop cursor.
756 * @pos: the &struct hlist_node to use as a loop cursor.
757 * @n: another &struct hlist_node to use as temporary storage
758 * @head: the head for your list.
759 * @member: the name of the hlist_node within the struct.
hualing chenb31a6c62020-01-13 17:27:00 +0800760 */
hualing chen002e5b92022-02-23 17:51:21 +0800761#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
762 for (pos = (head)->first; \
Wentao MA9a164002022-08-29 11:20:24 +0800763 pos && ({ n = pos->c_next; 1; }) && \
hualing chen002e5b92022-02-23 17:51:21 +0800764 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
765 pos = n)
hualing chenb31a6c62020-01-13 17:27:00 +0800766
hualing chen0888c032020-12-18 17:54:57 +0800767#ifdef __cplusplus
768}
hualing chenb31a6c62020-01-13 17:27:00 +0800769#endif
hualing chen0888c032020-12-18 17:54:57 +0800770
771
772#endif
773