hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 1 | /* |
| 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 chen | 0888c03 | 2020-12-18 17:54:57 +0800 | [diff] [blame] | 8 | |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 9 | #ifndef _LINUX_LIST_H |
| 10 | #define _LINUX_LIST_H |
| 11 | |
hualing chen | 0888c03 | 2020-12-18 17:54:57 +0800 | [diff] [blame] | 12 | #ifdef __cplusplus |
| 13 | extern "C" |
| 14 | { |
| 15 | #endif |
| 16 | |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 17 | #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 chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 24 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 27 | * |
| 28 | */ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 29 | #define container_of(ptr, type, member) ({ \ |
| 30 | const typeof(((type *)0)->member) * __mptr = (ptr); \ |
| 31 | (type *)((char *)__mptr - offsetof(type, member)); }) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 32 | #endif |
| 33 | |
| 34 | #define LIST_POISON1 ((void *) 0x00100100) |
| 35 | #define LIST_POISON2 ((void *) 0x00200200) |
| 36 | |
hualing chen | a540a7e | 2020-03-27 16:44:05 +0800 | [diff] [blame] | 37 | #define prefetch(x) ((x) = (x)) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 38 | |
| 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 MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 45 | * sometimes we already know the c_next/c_prev entries and we can |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 46 | * generate better code by using them directly rather than |
| 47 | * using the generic single-entry routines. |
| 48 | */ |
| 49 | |
| 50 | struct list_head { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 51 | struct list_head *c_next, *c_prev; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | #define LIST_HEAD_INIT(name) { &(name), &(name) } |
| 55 | |
| 56 | #define LIST_HEAD(name) \ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 57 | struct list_head name = LIST_HEAD_INIT(name) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 58 | |
| 59 | static inline void INIT_LIST_HEAD(struct list_head *list) |
| 60 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 61 | list->c_next = list; |
| 62 | list->c_prev = list; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /* |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 66 | * Insert a c_new entry between two known consecutive entries. |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 67 | * |
| 68 | * This is only for internal list manipulation where we know |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 69 | * the c_prev/c_next entries already! |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 70 | */ |
| 71 | #ifndef CONFIG_DEBUG_LIST |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 72 | static inline void __list_add(struct list_head *c_new, |
| 73 | struct list_head *c_prev, |
| 74 | struct list_head *c_next) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 75 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 76 | 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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 80 | } |
| 81 | #else |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 82 | extern void __list_add(struct list_head *c_new, |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 83 | struct list_head *prev, |
| 84 | struct list_head *next); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 85 | #endif |
| 86 | |
| 87 | /** |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 88 | * list_add - add a c_new entry |
| 89 | * @c_new: c_new entry to be added |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 90 | * @head: list head to add it after |
| 91 | * |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 92 | * Insert a c_new entry after the specified head. |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 93 | * This is good for implementing stacks. |
| 94 | */ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 95 | static inline void list_add(struct list_head *c_new, struct list_head *head) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 96 | { |
wentao.ma | a22bc85 | 2022-10-13 12:18:06 +0800 | [diff] [blame] | 97 | __list_add((struct list_head*)c_new, head, head->c_next); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | |
| 101 | /** |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 102 | * list_add_tail - add a c_new entry |
| 103 | * @c_new: c_new entry to be added |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 104 | * @head: list head to add it before |
| 105 | * |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 106 | * Insert a c_new entry before the specified head. |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 107 | * This is useful for implementing queues. |
| 108 | */ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 109 | static inline void list_add_tail(struct list_head *c_new, struct list_head *head) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 110 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 111 | __list_add(c_new, head->c_prev, head); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /* |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 115 | * Delete a list entry by making the c_prev/c_next entries |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 116 | * point to each other. |
| 117 | * |
| 118 | * This is only for internal list manipulation where we know |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 119 | * the c_prev/c_next entries already! |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 120 | */ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 121 | static inline void __list_del(struct list_head * c_prev, struct list_head * c_next) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 122 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 123 | c_next->c_prev = c_prev; |
| 124 | c_prev->c_next = c_next; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 125 | } |
| 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 |
| 134 | static inline void list_del(struct list_head *entry) |
| 135 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 136 | __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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 139 | } |
| 140 | #else |
| 141 | extern void list_del(struct list_head *entry); |
| 142 | #endif |
| 143 | |
| 144 | /** |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 145 | * list_replace - replace old entry by c_new one |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 146 | * @old : the element to be replaced |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 147 | * @c_new : the c_new element to insert |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 148 | * |
| 149 | * If @old was empty, it will be overwritten. |
| 150 | */ |
| 151 | static inline void list_replace(struct list_head *old, |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 152 | struct list_head *c_new) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 153 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 154 | 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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static inline void list_replace_init(struct list_head *old, |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 161 | struct list_head *c_new) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 162 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 163 | list_replace(old, c_new); |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 164 | INIT_LIST_HEAD(old); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * list_del_init - deletes entry from list and reinitialize it. |
| 169 | * @entry: the element to delete from the list. |
| 170 | */ |
| 171 | static inline void list_del_init(struct list_head *entry) |
| 172 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 173 | __list_del(entry->c_prev, entry->c_next); |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 174 | INIT_LIST_HEAD(entry); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 175 | } |
| 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 | */ |
| 182 | static inline void list_move(struct list_head *list, struct list_head *head) |
| 183 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 184 | __list_del(list->c_prev, list->c_next); |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 185 | list_add(list, head); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 186 | } |
| 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 | */ |
| 193 | static inline void list_move_tail(struct list_head *list, |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 194 | struct list_head *head) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 195 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 196 | __list_del(list->c_prev, list->c_next); |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 197 | list_add_tail(list, head); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 198 | } |
| 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 | */ |
| 205 | static inline int list_is_last(const struct list_head *list, |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 206 | const struct list_head *head) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 207 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 208 | return list->c_next == head; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /** |
| 212 | * list_empty - tests whether a list is empty |
| 213 | * @head: the list to test. |
| 214 | */ |
| 215 | static inline int list_empty(const struct list_head *head) |
| 216 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 217 | return head->c_next == head; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 218 | } |
| 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 MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 226 | * in the process of modifying either member (c_next or c_prev) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 227 | * |
| 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 | */ |
| 233 | static inline int list_empty_careful(const struct list_head *head) |
| 234 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 235 | struct list_head *c_next = head->c_next; |
| 236 | return (c_next == head) && (c_next == head->c_prev); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | /** |
| 240 | * list_rotate_left - rotate the list to the left |
| 241 | * @head: the head of the list |
| 242 | */ |
| 243 | static inline void list_rotate_left(struct list_head *head) |
| 244 | { |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 245 | struct list_head *first; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 246 | |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 247 | if (!list_empty(head)) { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 248 | first = head->c_next; |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 249 | list_move_tail(first, head); |
| 250 | } |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | /** |
| 254 | * list_is_singular - tests whether a list has just one entry. |
| 255 | * @head: the list to test. |
| 256 | */ |
| 257 | static inline int list_is_singular(const struct list_head *head) |
| 258 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 259 | return !list_empty(head) && (head->c_next == head->c_prev); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | static inline void __list_cut_position(struct list_head *list, |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 263 | struct list_head *head, struct list_head *entry) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 264 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 265 | 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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
| 275 | * list_cut_position - cut a list into two |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 276 | * @list: a c_new list to add all removed entries |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 277 | * @head: a list with entries |
| 278 | * @entry: an entry within head, could be the head itself |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 279 | * and if so we won't cut the list |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 280 | * |
| 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 | */ |
| 288 | static inline void list_cut_position(struct list_head *list, |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 289 | struct list_head *head, struct list_head *entry) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 290 | { |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 291 | if (list_empty(head)) |
| 292 | return; |
| 293 | if (list_is_singular(head) && |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 294 | (head->c_next != entry && head != entry)) |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 295 | return; |
| 296 | if (entry == head) |
| 297 | INIT_LIST_HEAD(list); |
| 298 | else |
| 299 | __list_cut_position(list, head, entry); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static inline void __list_splice(const struct list_head *list, |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 303 | struct list_head *c_prev, |
| 304 | struct list_head *c_next) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 305 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 306 | struct list_head *first = list->c_next; |
| 307 | struct list_head *last = list->c_prev; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 308 | |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 309 | first->c_prev = c_prev; |
| 310 | c_prev->c_next = first; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 311 | |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 312 | last->c_next = c_next; |
| 313 | c_next->c_prev = last; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
| 317 | * list_splice - join two lists, this is designed for stacks |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 318 | * @list: the c_new list to add. |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 319 | * @head: the place to add it in the first list. |
| 320 | */ |
| 321 | static inline void list_splice(const struct list_head *list, |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 322 | struct list_head *head) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 323 | { |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 324 | if (!list_empty(list)) |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 325 | __list_splice(list, head, head->c_next); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | /** |
| 329 | * list_splice_tail - join two lists, each list being a queue |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 330 | * @list: the c_new list to add. |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 331 | * @head: the place to add it in the first list. |
| 332 | */ |
| 333 | static inline void list_splice_tail(struct list_head *list, |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 334 | struct list_head *head) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 335 | { |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 336 | if (!list_empty(list)) |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 337 | __list_splice(list, head->c_prev, head); |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /** |
| 341 | * list_splice_init - join two lists and reinitialise the emptied list. |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 342 | * @list: the c_new list to add. |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 343 | * @head: the place to add it in the first list. |
| 344 | * |
| 345 | * The list at @list is reinitialised |
| 346 | */ |
| 347 | static inline void list_splice_init(struct list_head *list, |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 348 | struct list_head *head) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 349 | { |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 350 | if (!list_empty(list)) { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 351 | __list_splice(list, head, head->c_next); |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 352 | INIT_LIST_HEAD(list); |
| 353 | } |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
| 357 | * list_splice_tail_init - join two lists and reinitialise the emptied list |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 358 | * @list: the c_new list to add. |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 359 | * @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 | */ |
| 364 | static inline void list_splice_tail_init(struct list_head *list, |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 365 | struct list_head *head) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 366 | { |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 367 | if (!list_empty(list)) { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 368 | __list_splice(list, head->c_prev, head); |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 369 | INIT_LIST_HEAD(list); |
| 370 | } |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | /** |
| 374 | * list_entry - get the struct for this entry |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 375 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 378 | */ |
| 379 | #define list_entry(ptr, type, member) \ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 380 | container_of(ptr, type, member) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 381 | |
| 382 | /** |
| 383 | * list_first_entry - get the first element from a list |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 384 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 387 | * |
| 388 | * Note, that list is expected to be not empty. |
| 389 | */ |
| 390 | #define list_first_entry(ptr, type, member) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 391 | list_entry((ptr)->c_next, type, member) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 392 | |
| 393 | /** |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 394 | * list_last_entry - get the last element from a list |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 395 | * @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 Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 398 | * |
| 399 | * Note, that list is expected to be not empty. |
| 400 | */ |
| 401 | #define list_last_entry(ptr, type, member) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 402 | list_entry((ptr)->c_prev, type, member) |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 403 | |
| 404 | /** |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 405 | * list_next_entry - get the c_next element in list |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 406 | * @pos: the type * to cursor |
| 407 | * @member: the name of the list_head within the struct. |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 408 | */ |
| 409 | #define list_next_entry(pos, member) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 410 | list_entry((pos)->member.c_next, typeof(*(pos)), member) |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 411 | |
| 412 | /** |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 413 | * list_prev_entry - get the c_prev element in list |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 414 | * @pos: the type * to cursor |
| 415 | * @member: the name of the list_head within the struct. |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 416 | */ |
| 417 | #define list_prev_entry(pos, member) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 418 | list_entry((pos)->member.c_prev, typeof(*(pos)), member) |
Zhiqiang Han | aeb0c71 | 2020-04-30 15:17:26 +0800 | [diff] [blame] | 419 | |
| 420 | /** |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 421 | * 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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 424 | */ |
| 425 | #define list_for_each(pos, head) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 426 | for (pos = (head)->c_next; prefetch(pos->c_next), pos != (head); \ |
| 427 | pos = pos->c_next) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 428 | |
| 429 | /** |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 430 | * __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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 433 | * |
| 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 MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 440 | for (pos = (head)->c_next; pos != (head); pos = pos->c_next) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 441 | |
| 442 | /** |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 443 | * 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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 446 | */ |
| 447 | #define list_for_each_prev(pos, head) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 448 | for (pos = (head)->c_prev; prefetch(pos->c_prev), pos != (head); \ |
| 449 | pos = pos->c_prev) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 450 | |
| 451 | /** |
| 452 | * list_for_each_safe - iterate over a list safe against removal of list entry |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 453 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 456 | */ |
| 457 | #define list_for_each_safe(pos, n, head) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 458 | for (pos = (head)->c_next, n = pos->c_next; pos != (head); \ |
| 459 | pos = n, n = pos->c_next) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 460 | |
| 461 | /** |
| 462 | * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 463 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 466 | */ |
| 467 | #define list_for_each_prev_safe(pos, n, head) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 468 | for (pos = (head)->c_prev, n = pos->c_prev; \ |
| 469 | prefetch(pos->c_prev), pos != (head); \ |
| 470 | pos = n, n = pos->c_prev) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 471 | |
| 472 | /** |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 473 | * 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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 477 | */ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 478 | #define list_for_each_entry(pos, head, member) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 479 | 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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 482 | |
| 483 | /** |
| 484 | * list_for_each_entry_reverse - iterate backwards over list of given type. |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 485 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 488 | */ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 489 | #define list_for_each_entry_reverse(pos, head, member) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 490 | 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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 493 | |
| 494 | /** |
| 495 | * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue() |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 496 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 499 | * |
| 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 chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 503 | ((pos) ? : list_entry(head, typeof(*pos), member)) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 504 | |
| 505 | /** |
| 506 | * list_for_each_entry_continue - continue iteration over list of given type |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 507 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 510 | * |
| 511 | * Continue to iterate over list of given type, continuing after |
| 512 | * the current position. |
| 513 | */ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 514 | #define list_for_each_entry_continue(pos, head, member) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 515 | 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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 518 | |
| 519 | /** |
| 520 | * list_for_each_entry_continue_reverse - iterate backwards from the given point |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 521 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 524 | * |
| 525 | * Start to iterate over list of given type backwards, continuing after |
| 526 | * the current position. |
| 527 | */ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 528 | #define list_for_each_entry_continue_reverse(pos, head, member) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 529 | 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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 532 | |
| 533 | /** |
| 534 | * list_for_each_entry_from - iterate over list of given type from the current point |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 535 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 538 | * |
| 539 | * Iterate over list of given type, continuing from current position. |
| 540 | */ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 541 | #define list_for_each_entry_from(pos, head, member) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 542 | for (; prefetch(pos->member.c_next), &pos->member != (head); \ |
| 543 | pos = list_entry(pos->member.c_next, typeof(*pos), member)) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 544 | |
| 545 | /** |
| 546 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 547 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 551 | */ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 552 | #define list_for_each_entry_safe(pos, n, head, member) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 553 | for (pos = list_entry((head)->c_next, typeof(*pos), member), \ |
| 554 | n = list_entry(pos->member.c_next, typeof(*pos), member); \ |
Wentao MA | e2a2db9 | 2024-08-26 16:21:41 +0800 | [diff] [blame] | 555 | (pos > 0) && &pos->member != (head); \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 556 | pos = n, n = list_entry(n->member.c_next, typeof(*n), member)) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 557 | |
| 558 | /** |
| 559 | * list_for_each_entry_safe_continue - continue list iteration safe against removal |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 560 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 564 | * |
| 565 | * Iterate over list of given type, continuing after current point, |
| 566 | * safe against removal of list entry. |
| 567 | */ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 568 | #define list_for_each_entry_safe_continue(pos, n, head, member) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 569 | for (pos = list_entry(pos->member.c_next, typeof(*pos), member), \ |
| 570 | n = list_entry(pos->member.c_next, typeof(*pos), member); \ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 571 | &pos->member != (head); \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 572 | pos = n, n = list_entry(n->member.c_next, typeof(*n), member)) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 573 | |
| 574 | /** |
| 575 | * list_for_each_entry_safe_from - iterate over list from current point safe against removal |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 576 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 580 | * |
| 581 | * Iterate over list of given type from current point, safe against |
| 582 | * removal of list entry. |
| 583 | */ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 584 | #define list_for_each_entry_safe_from(pos, n, head, member) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 585 | for (n = list_entry(pos->member.c_next, typeof(*pos), member); \ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 586 | &pos->member != (head); \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 587 | pos = n, n = list_entry(n->member.c_next, typeof(*n), member)) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 588 | |
| 589 | /** |
| 590 | * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 591 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 595 | * |
| 596 | * Iterate backwards over list of given type, safe against removal |
| 597 | * of list entry. |
| 598 | */ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 599 | #define list_for_each_entry_safe_reverse(pos, n, head, member) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 600 | for (pos = list_entry((head)->c_prev, typeof(*pos), member), \ |
| 601 | n = list_entry(pos->member.c_prev, typeof(*pos), member); \ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 602 | &pos->member != (head); \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 603 | pos = n, n = list_entry(n->member.c_prev, typeof(*n), member)) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 604 | |
| 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 | |
| 612 | struct hlist_head { |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 613 | struct hlist_node *first; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 614 | }; |
| 615 | |
| 616 | struct hlist_node { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 617 | struct hlist_node *c_next, **pc_prev; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 618 | }; |
| 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) |
| 623 | static inline void INIT_HLIST_NODE(struct hlist_node *h) |
| 624 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 625 | h->c_next = NULL; |
| 626 | h->pc_prev = NULL; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | static inline int hlist_unhashed(const struct hlist_node *h) |
| 630 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 631 | return !h->pc_prev; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | static inline int hlist_empty(const struct hlist_head *h) |
| 635 | { |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 636 | return !h->first; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | static inline void __hlist_del(struct hlist_node *n) |
| 640 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 641 | 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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | static inline void hlist_del(struct hlist_node *n) |
| 649 | { |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 650 | __hlist_del(n); |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 651 | n->c_next = (struct hlist_node *)LIST_POISON1; |
| 652 | n->pc_prev = (struct hlist_node **)LIST_POISON2; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | static inline void hlist_del_init(struct hlist_node *n) |
| 656 | { |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 657 | if (!hlist_unhashed(n)) { |
| 658 | __hlist_del(n); |
| 659 | INIT_HLIST_NODE(n); |
| 660 | } |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) |
| 664 | { |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 665 | struct hlist_node *first = h->first; |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 666 | n->c_next = first; |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 667 | if (first) |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 668 | first->pc_prev = &n->c_next; |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 669 | h->first = n; |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 670 | n->pc_prev = &h->first; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 671 | } |
| 672 | |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 673 | /* c_next must be != NULL */ |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 674 | static inline void hlist_add_before(struct hlist_node *n, |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 675 | struct hlist_node *c_next) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 676 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 677 | 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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | static inline void hlist_add_after(struct hlist_node *n, |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 684 | struct hlist_node *c_next) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 685 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 686 | c_next->c_next = n->c_next; |
| 687 | n->c_next = c_next; |
| 688 | c_next->pc_prev = &n->c_next; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 689 | |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 690 | if(c_next->c_next) |
| 691 | c_next->c_next->pc_prev = &c_next->c_next; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 692 | } |
| 693 | |
| 694 | /* |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 695 | * Move a list from one list head to another. Fixup the pc_prev |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 696 | * reference of the first entry if it exists. |
| 697 | */ |
| 698 | static inline void hlist_move_list(struct hlist_head *old, |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 699 | struct hlist_head *c_new) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 700 | { |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 701 | c_new->first = old->first; |
| 702 | if (c_new->first) |
| 703 | c_new->first->pc_prev = &c_new->first; |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 704 | old->first = NULL; |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | #define hlist_entry(ptr, type, member) container_of(ptr,type,member) |
| 708 | |
| 709 | #define hlist_for_each(pos, head) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 710 | for (pos = (head)->first; pos && ({ prefetch(pos->c_next); 1; }); \ |
| 711 | pos = pos->c_next) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 712 | |
| 713 | #define hlist_for_each_safe(pos, n, head) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 714 | for (pos = (head)->first; pos && ({ n = pos->c_next; 1; }); \ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 715 | pos = n) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 716 | |
| 717 | /** |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 718 | * 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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 723 | */ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 724 | #define hlist_for_each_entry(tpos, pos, head, member) \ |
| 725 | for (pos = (head)->first; \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 726 | pos && ({ prefetch(pos->c_next); 1;}) && \ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 727 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 728 | pos = pos->c_next) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 729 | |
| 730 | /** |
| 731 | * hlist_for_each_entry_continue - iterate over a hlist continuing after current point |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 732 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 735 | */ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 736 | #define hlist_for_each_entry_continue(tpos, pos, member) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 737 | for (pos = (pos)->c_next; \ |
| 738 | pos && ({ prefetch(pos->c_next); 1;}) && \ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 739 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 740 | pos = pos->c_next) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 741 | |
| 742 | /** |
| 743 | * hlist_for_each_entry_from - iterate over a hlist continuing from current point |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 744 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 747 | */ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 748 | #define hlist_for_each_entry_from(tpos, pos, member) \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 749 | for (; pos && ({ prefetch(pos->c_next); 1;}) && \ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 750 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 751 | pos = pos->c_next) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 752 | |
| 753 | /** |
| 754 | * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 755 | * @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 chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 760 | */ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 761 | #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ |
| 762 | for (pos = (head)->first; \ |
Wentao MA | 9a16400 | 2022-08-29 11:20:24 +0800 | [diff] [blame] | 763 | pos && ({ n = pos->c_next; 1; }) && \ |
hualing chen | 002e5b9 | 2022-02-23 17:51:21 +0800 | [diff] [blame] | 764 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ |
| 765 | pos = n) |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 766 | |
hualing chen | 0888c03 | 2020-12-18 17:54:57 +0800 | [diff] [blame] | 767 | #ifdef __cplusplus |
| 768 | } |
hualing chen | b31a6c6 | 2020-01-13 17:27:00 +0800 | [diff] [blame] | 769 | #endif |
hualing chen | 0888c03 | 2020-12-18 17:54:57 +0800 | [diff] [blame] | 770 | |
| 771 | |
| 772 | #endif |
| 773 | |