blob: 89230e77f5b2a92337f595b26734ff5a4ff1d06a [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
hualing chen0888c032020-12-18 17:54:57 +080045 * sometimes we already know the cnext/cprev 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 {
hualing chen002e5b92022-02-23 17:51:21 +080051 struct list_head *cnext, *cprev;
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{
hualing chen002e5b92022-02-23 17:51:21 +080061 list->cnext = list;
62 list->cprev = list;
hualing chenb31a6c62020-01-13 17:27:00 +080063}
64
65/*
hualing chen0888c032020-12-18 17:54:57 +080066 * Insert a cnew 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
hualing chen0888c032020-12-18 17:54:57 +080069 * the cprev/cnext entries already!
hualing chenb31a6c62020-01-13 17:27:00 +080070 */
71#ifndef CONFIG_DEBUG_LIST
hualing chen0888c032020-12-18 17:54:57 +080072static inline void __list_add(struct list_head *cnew,
hualing chen002e5b92022-02-23 17:51:21 +080073 struct list_head *cprev,
74 struct list_head *cnext)
hualing chenb31a6c62020-01-13 17:27:00 +080075{
hualing chen002e5b92022-02-23 17:51:21 +080076 cnext->cprev = cnew;
77 cnew->cnext = cnext;
78 cnew->cprev = cprev;
79 cprev->cnext = cnew;
hualing chenb31a6c62020-01-13 17:27:00 +080080}
81#else
hualing chen0888c032020-12-18 17:54:57 +080082extern void __list_add(struct list_head *cnew,
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/**
hualing chen0888c032020-12-18 17:54:57 +080088 * list_add - add a cnew entry
89 * @cnew: cnew entry to be added
hualing chenb31a6c62020-01-13 17:27:00 +080090 * @head: list head to add it after
91 *
hualing chen0888c032020-12-18 17:54:57 +080092 * Insert a cnew entry after the specified head.
hualing chenb31a6c62020-01-13 17:27:00 +080093 * This is good for implementing stacks.
94 */
hualing chen0888c032020-12-18 17:54:57 +080095static inline void list_add(struct list_head *cnew, struct list_head *head)
hualing chenb31a6c62020-01-13 17:27:00 +080096{
hualing chen002e5b92022-02-23 17:51:21 +080097 __list_add(cnew, head, head->cnext);
hualing chenb31a6c62020-01-13 17:27:00 +080098}
99
100
101/**
hualing chen0888c032020-12-18 17:54:57 +0800102 * list_add_tail - add a cnew entry
103 * @cnew: cnew entry to be added
hualing chenb31a6c62020-01-13 17:27:00 +0800104 * @head: list head to add it before
105 *
hualing chen0888c032020-12-18 17:54:57 +0800106 * Insert a cnew entry before the specified head.
hualing chenb31a6c62020-01-13 17:27:00 +0800107 * This is useful for implementing queues.
108 */
hualing chen0888c032020-12-18 17:54:57 +0800109static inline void list_add_tail(struct list_head *cnew, struct list_head *head)
hualing chenb31a6c62020-01-13 17:27:00 +0800110{
hualing chen002e5b92022-02-23 17:51:21 +0800111 __list_add(cnew, head->cprev, head);
hualing chenb31a6c62020-01-13 17:27:00 +0800112}
113
114/*
hualing chen0888c032020-12-18 17:54:57 +0800115 * Delete a list entry by making the cprev/cnext 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
hualing chen0888c032020-12-18 17:54:57 +0800119 * the cprev/cnext entries already!
hualing chenb31a6c62020-01-13 17:27:00 +0800120 */
hualing chen0888c032020-12-18 17:54:57 +0800121static inline void __list_del(struct list_head * cprev, struct list_head * cnext)
hualing chenb31a6c62020-01-13 17:27:00 +0800122{
hualing chen002e5b92022-02-23 17:51:21 +0800123 cnext->cprev = cprev;
124 cprev->cnext = cnext;
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{
hualing chen002e5b92022-02-23 17:51:21 +0800136 __list_del(entry->cprev, entry->cnext);
137 entry->cnext = (struct list_head *)LIST_POISON1;
138 entry->cprev = (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/**
hualing chen0888c032020-12-18 17:54:57 +0800145 * list_replace - replace old entry by cnew one
hualing chenb31a6c62020-01-13 17:27:00 +0800146 * @old : the element to be replaced
hualing chen0888c032020-12-18 17:54:57 +0800147 * @cnew : the cnew 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,
hualing chen002e5b92022-02-23 17:51:21 +0800152 struct list_head *cnew)
hualing chenb31a6c62020-01-13 17:27:00 +0800153{
hualing chen002e5b92022-02-23 17:51:21 +0800154 cnew->cnext = old->cnext;
155 cnew->cnext->cprev = cnew;
156 cnew->cprev = old->cprev;
157 cnew->cprev->cnext = cnew;
hualing chenb31a6c62020-01-13 17:27:00 +0800158}
159
160static inline void list_replace_init(struct list_head *old,
hualing chen002e5b92022-02-23 17:51:21 +0800161 struct list_head *cnew)
hualing chenb31a6c62020-01-13 17:27:00 +0800162{
hualing chen002e5b92022-02-23 17:51:21 +0800163 list_replace(old, cnew);
164 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{
hualing chen002e5b92022-02-23 17:51:21 +0800173 __list_del(entry->cprev, entry->cnext);
174 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{
hualing chen002e5b92022-02-23 17:51:21 +0800184 __list_del(list->cprev, list->cnext);
185 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{
hualing chen002e5b92022-02-23 17:51:21 +0800196 __list_del(list->cprev, list->cnext);
197 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{
hualing chen002e5b92022-02-23 17:51:21 +0800208 return list->cnext == 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{
hualing chen002e5b92022-02-23 17:51:21 +0800217 return head->cnext == 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
hualing chen0888c032020-12-18 17:54:57 +0800226 * in the process of modifying either member (cnext or cprev)
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{
hualing chen002e5b92022-02-23 17:51:21 +0800235 struct list_head *cnext = head->cnext;
236 return (cnext == head) && (cnext == head->cprev);
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)) {
248 first = head->cnext;
249 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{
hualing chen002e5b92022-02-23 17:51:21 +0800259 return !list_empty(head) && (head->cnext == head->cprev);
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{
hualing chen002e5b92022-02-23 17:51:21 +0800265 struct list_head *cnew_first = entry->cnext;
266 list->cnext = head->cnext;
267 list->cnext->cprev = list;
268 list->cprev = entry;
269 entry->cnext = list;
270 head->cnext = cnew_first;
271 cnew_first->cprev = head;
hualing chenb31a6c62020-01-13 17:27:00 +0800272}
273
274/**
275 * list_cut_position - cut a list into two
hualing chen0888c032020-12-18 17:54:57 +0800276 * @list: a cnew 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) &&
294 (head->cnext != entry && head != entry))
295 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,
hualing chen002e5b92022-02-23 17:51:21 +0800303 struct list_head *cprev,
304 struct list_head *cnext)
hualing chenb31a6c62020-01-13 17:27:00 +0800305{
hualing chen002e5b92022-02-23 17:51:21 +0800306 struct list_head *first = list->cnext;
307 struct list_head *last = list->cprev;
hualing chenb31a6c62020-01-13 17:27:00 +0800308
hualing chen002e5b92022-02-23 17:51:21 +0800309 first->cprev = cprev;
310 cprev->cnext = first;
hualing chenb31a6c62020-01-13 17:27:00 +0800311
hualing chen002e5b92022-02-23 17:51:21 +0800312 last->cnext = cnext;
313 cnext->cprev = last;
hualing chenb31a6c62020-01-13 17:27:00 +0800314}
315
316/**
317 * list_splice - join two lists, this is designed for stacks
hualing chen0888c032020-12-18 17:54:57 +0800318 * @list: the cnew 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))
325 __list_splice(list, head, head->cnext);
hualing chenb31a6c62020-01-13 17:27:00 +0800326}
327
328/**
329 * list_splice_tail - join two lists, each list being a queue
hualing chen0888c032020-12-18 17:54:57 +0800330 * @list: the cnew 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))
337 __list_splice(list, head->cprev, head);
hualing chenb31a6c62020-01-13 17:27:00 +0800338}
339
340/**
341 * list_splice_init - join two lists and reinitialise the emptied list.
hualing chen0888c032020-12-18 17:54:57 +0800342 * @list: the cnew 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)) {
351 __list_splice(list, head, head->cnext);
352 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
hualing chen0888c032020-12-18 17:54:57 +0800358 * @list: the cnew 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)) {
368 __list_splice(list, head->cprev, head);
369 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) \
hualing chen002e5b92022-02-23 17:51:21 +0800391 list_entry((ptr)->cnext, 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) \
hualing chen002e5b92022-02-23 17:51:21 +0800402 list_entry((ptr)->cprev, type, member)
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800403
404/**
hualing chen0888c032020-12-18 17:54:57 +0800405 * list_next_entry - get the cnext 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) \
hualing chen002e5b92022-02-23 17:51:21 +0800410 list_entry((pos)->member.cnext, typeof(*(pos)), member)
Zhiqiang Hanaeb0c712020-04-30 15:17:26 +0800411
412/**
hualing chen0888c032020-12-18 17:54:57 +0800413 * list_prev_entry - get the cprev 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) \
hualing chen002e5b92022-02-23 17:51:21 +0800418 list_entry((pos)->member.cprev, 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) \
hualing chen002e5b92022-02-23 17:51:21 +0800426 for (pos = (head)->cnext; prefetch(pos->cnext), pos != (head); \
427 pos = pos->cnext)
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) \
hualing chen002e5b92022-02-23 17:51:21 +0800440 for (pos = (head)->cnext; pos != (head); pos = pos->cnext)
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) \
hualing chen002e5b92022-02-23 17:51:21 +0800448 for (pos = (head)->cprev; prefetch(pos->cprev), pos != (head); \
449 pos = pos->cprev)
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) \
hualing chen002e5b92022-02-23 17:51:21 +0800458 for (pos = (head)->cnext, n = pos->cnext; pos != (head); \
459 pos = n, n = pos->cnext)
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) \
hualing chen002e5b92022-02-23 17:51:21 +0800468 for (pos = (head)->cprev, n = pos->cprev; \
469 prefetch(pos->cprev), pos != (head); \
470 pos = n, n = pos->cprev)
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) \
479 for (pos = list_entry((head)->cnext, typeof(*pos), member); \
480 prefetch(pos->member.cnext), &pos->member != (head); \
481 pos = list_entry(pos->member.cnext, 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) \
490 for (pos = list_entry((head)->cprev, typeof(*pos), member); \
491 prefetch(pos->member.cprev), &pos->member != (head); \
492 pos = list_entry(pos->member.cprev, 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) \
515 for (pos = list_entry(pos->member.cnext, typeof(*pos), member); \
516 prefetch(pos->member.cnext), &pos->member != (head); \
517 pos = list_entry(pos->member.cnext, 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) \
529 for (pos = list_entry(pos->member.cprev, typeof(*pos), member); \
530 prefetch(pos->member.cprev), &pos->member != (head); \
531 pos = list_entry(pos->member.cprev, 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) \
542 for (; prefetch(pos->member.cnext), &pos->member != (head); \
543 pos = list_entry(pos->member.cnext, 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) \
553 for (pos = list_entry((head)->cnext, typeof(*pos), member), \
554 n = list_entry(pos->member.cnext, typeof(*pos), member); \
555 &pos->member != (head); \
556 pos = n, n = list_entry(n->member.cnext, 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) \
569 for (pos = list_entry(pos->member.cnext, typeof(*pos), member), \
570 n = list_entry(pos->member.cnext, typeof(*pos), member); \
571 &pos->member != (head); \
572 pos = n, n = list_entry(n->member.cnext, 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) \
585 for (n = list_entry(pos->member.cnext, typeof(*pos), member); \
586 &pos->member != (head); \
587 pos = n, n = list_entry(n->member.cnext, 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) \
600 for (pos = list_entry((head)->cprev, typeof(*pos), member), \
601 n = list_entry(pos->member.cprev, typeof(*pos), member); \
602 &pos->member != (head); \
603 pos = n, n = list_entry(n->member.cprev, 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 {
hualing chen002e5b92022-02-23 17:51:21 +0800617 struct hlist_node *cnext, **pcprev;
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{
hualing chen002e5b92022-02-23 17:51:21 +0800625 h->cnext = NULL;
626 h->pcprev = NULL;
hualing chenb31a6c62020-01-13 17:27:00 +0800627}
628
629static inline int hlist_unhashed(const struct hlist_node *h)
630{
hualing chen002e5b92022-02-23 17:51:21 +0800631 return !h->pcprev;
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{
hualing chen002e5b92022-02-23 17:51:21 +0800641 struct hlist_node *cnext = n->cnext;
642 struct hlist_node **pcprev = n->pcprev;
643 *pcprev = cnext;
644 if (cnext)
645 cnext->pcprev = pcprev;
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);
651 n->cnext = (struct hlist_node *)LIST_POISON1;
652 n->pcprev = (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;
666 n->cnext = first;
667 if (first)
668 first->pcprev = &n->cnext;
669 h->first = n;
670 n->pcprev = &h->first;
hualing chenb31a6c62020-01-13 17:27:00 +0800671}
672
hualing chen0888c032020-12-18 17:54:57 +0800673/* cnext must be != NULL */
hualing chenb31a6c62020-01-13 17:27:00 +0800674static inline void hlist_add_before(struct hlist_node *n,
hualing chen002e5b92022-02-23 17:51:21 +0800675 struct hlist_node *cnext)
hualing chenb31a6c62020-01-13 17:27:00 +0800676{
hualing chen002e5b92022-02-23 17:51:21 +0800677 n->pcprev = cnext->pcprev;
678 n->cnext = cnext;
679 cnext->pcprev = &n->cnext;
680 *(n->pcprev) = n;
hualing chenb31a6c62020-01-13 17:27:00 +0800681}
682
683static inline void hlist_add_after(struct hlist_node *n,
hualing chen002e5b92022-02-23 17:51:21 +0800684 struct hlist_node *cnext)
hualing chenb31a6c62020-01-13 17:27:00 +0800685{
hualing chen002e5b92022-02-23 17:51:21 +0800686 cnext->cnext = n->cnext;
687 n->cnext = cnext;
688 cnext->pcprev = &n->cnext;
hualing chenb31a6c62020-01-13 17:27:00 +0800689
hualing chen002e5b92022-02-23 17:51:21 +0800690 if(cnext->cnext)
691 cnext->cnext->pcprev = &cnext->cnext;
hualing chenb31a6c62020-01-13 17:27:00 +0800692}
693
694/*
hualing chen0888c032020-12-18 17:54:57 +0800695 * Move a list from one list head to another. Fixup the pcprev
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,
hualing chen002e5b92022-02-23 17:51:21 +0800699 struct hlist_head *cnew)
hualing chenb31a6c62020-01-13 17:27:00 +0800700{
hualing chen002e5b92022-02-23 17:51:21 +0800701 cnew->first = old->first;
702 if (cnew->first)
703 cnew->first->pcprev = &cnew->first;
704 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) \
hualing chen002e5b92022-02-23 17:51:21 +0800710 for (pos = (head)->first; pos && ({ prefetch(pos->cnext); 1; }); \
711 pos = pos->cnext)
hualing chenb31a6c62020-01-13 17:27:00 +0800712
713#define hlist_for_each_safe(pos, n, head) \
hualing chen002e5b92022-02-23 17:51:21 +0800714 for (pos = (head)->first; pos && ({ n = pos->cnext; 1; }); \
715 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; \
726 pos && ({ prefetch(pos->cnext); 1;}) && \
727 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
728 pos = pos->cnext)
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) \
737 for (pos = (pos)->cnext; \
738 pos && ({ prefetch(pos->cnext); 1;}) && \
739 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
740 pos = pos->cnext)
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) \
749 for (; pos && ({ prefetch(pos->cnext); 1;}) && \
750 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
751 pos = pos->cnext)
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; \
763 pos && ({ n = pos->cnext; 1; }) && \
764 ({ 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