Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file linuxlist.h |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 2 | * |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 3 | * Simple doubly linked list implementation. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 4 | * |
| 5 | * Some of the internal functions ("__xxx") are useful when |
| 6 | * manipulating whole llists rather than single entries, as |
| 7 | * sometimes we already know the next/prev entries and we can |
| 8 | * generate better code by using them directly rather than |
| 9 | * using the generic single-entry routines. |
| 10 | */ |
| 11 | |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 12 | #pragma once |
| 13 | |
| 14 | /*! \defgroup linuxlist Simple doubly linked list implementation |
| 15 | * @{ |
| 16 | * \file linuxlist.h */ |
| 17 | |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 18 | #include <stddef.h> |
| 19 | |
| 20 | #ifndef inline |
| 21 | #define inline __inline__ |
| 22 | #endif |
| 23 | |
| 24 | static inline void prefetch(const void *x) {;} |
| 25 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 26 | /*! Cast a member of a structure out to the containing structure. |
| 27 | * \param[in] ptr the pointer to the member. |
| 28 | * \param[in] type the type of the container struct this is embedded in. |
| 29 | * \param[in] member the name of the member within the struct. |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 30 | */ |
| 31 | #define container_of(ptr, type, member) ({ \ |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 32 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 33 | (type *)( (char *)__mptr - offsetof(type, member) );}) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 34 | |
| 35 | |
| 36 | /*! |
| 37 | * These are non-NULL pointers that will result in page faults |
| 38 | * under normal circumstances, used to verify that nobody uses |
| 39 | * non-initialized llist entries. |
| 40 | */ |
| 41 | #define LLIST_POISON1 ((void *) 0x00100100) |
| 42 | #define LLIST_POISON2 ((void *) 0x00200200) |
| 43 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 44 | /*! (double) linked list header structure */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 45 | struct llist_head { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 46 | /*! Pointer to next and previous item */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 47 | struct llist_head *next, *prev; |
| 48 | }; |
| 49 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 50 | /*! Define a new llist_head pointing to a given llist_head. |
| 51 | * \param[in] name another llist_head to be pointed. |
| 52 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 53 | #define LLIST_HEAD_INIT(name) { &(name), &(name) } |
| 54 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 55 | /*! Define a statically-initialized variable of type llist_head. |
| 56 | * \param[in] name variable (symbol) name. |
| 57 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 58 | #define LLIST_HEAD(name) \ |
| 59 | struct llist_head name = LLIST_HEAD_INIT(name) |
| 60 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 61 | /*! Initialize a llist_head to point back to itself. |
| 62 | * \param[in] ptr llist_head to be initialized. |
| 63 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 64 | #define INIT_LLIST_HEAD(ptr) do { \ |
| 65 | (ptr)->next = (ptr); (ptr)->prev = (ptr); \ |
| 66 | } while (0) |
| 67 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 68 | /* |
| 69 | * Insert a new entry between two known consecutive entries. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 70 | * |
| 71 | * This is only for internal llist manipulation where we know |
| 72 | * the prev/next entries already! |
| 73 | */ |
| 74 | static inline void __llist_add(struct llist_head *_new, |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 75 | struct llist_head *prev, |
| 76 | struct llist_head *next) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 77 | { |
| 78 | next->prev = _new; |
| 79 | _new->next = next; |
| 80 | _new->prev = prev; |
| 81 | prev->next = _new; |
| 82 | } |
| 83 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 84 | /*! Add a new entry into a linked list (at head). |
| 85 | * \param _new the entry to be added. |
| 86 | * \param head llist_head to prepend the element to. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 87 | * |
| 88 | * Insert a new entry after the specified head. |
| 89 | * This is good for implementing stacks. |
| 90 | */ |
| 91 | static inline void llist_add(struct llist_head *_new, struct llist_head *head) |
| 92 | { |
| 93 | __llist_add(_new, head, head->next); |
| 94 | } |
| 95 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 96 | /*! Add a new entry into a linked list (at tail). |
| 97 | * \param _new the entry to be added. |
| 98 | * \param head llist_head to append the element to. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 99 | * |
| 100 | * Insert a new entry before the specified head. |
| 101 | * This is useful for implementing queues. |
| 102 | */ |
| 103 | static inline void llist_add_tail(struct llist_head *_new, struct llist_head *head) |
| 104 | { |
| 105 | __llist_add(_new, head->prev, head); |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Delete a llist entry by making the prev/next entries |
| 110 | * point to each other. |
| 111 | * |
| 112 | * This is only for internal llist manipulation where we know |
| 113 | * the prev/next entries already! |
| 114 | */ |
| 115 | static inline void __llist_del(struct llist_head * prev, struct llist_head * next) |
| 116 | { |
| 117 | next->prev = prev; |
| 118 | prev->next = next; |
| 119 | } |
| 120 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 121 | /*! Delete a single entry from a linked list. |
| 122 | * \param entry the element to delete. |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 123 | * |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 124 | * Note: llist_empty on entry does not return true after this, the entry is |
| 125 | * in an undefined state. |
| 126 | */ |
| 127 | static inline void llist_del(struct llist_head *entry) |
| 128 | { |
| 129 | __llist_del(entry->prev, entry->next); |
| 130 | entry->next = (struct llist_head *)LLIST_POISON1; |
| 131 | entry->prev = (struct llist_head *)LLIST_POISON2; |
| 132 | } |
| 133 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 134 | /*! Delete a single entry from a linked list and reinitialize it. |
| 135 | * \param entry the element to delete and reinitialize. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 136 | */ |
| 137 | static inline void llist_del_init(struct llist_head *entry) |
| 138 | { |
| 139 | __llist_del(entry->prev, entry->next); |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 140 | INIT_LLIST_HEAD(entry); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 143 | /*! Delete from one llist and add as another's head. |
| 144 | * \param llist the entry to move. |
| 145 | * \param head the head that will precede our entry. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 146 | */ |
| 147 | static inline void llist_move(struct llist_head *llist, struct llist_head *head) |
| 148 | { |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 149 | __llist_del(llist->prev, llist->next); |
| 150 | llist_add(llist, head); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 153 | /*! Delete from one llist and add as another's tail. |
| 154 | * \param llist the entry to move. |
| 155 | * \param head the head that will follow our entry. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 156 | */ |
| 157 | static inline void llist_move_tail(struct llist_head *llist, |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 158 | struct llist_head *head) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 159 | { |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 160 | __llist_del(llist->prev, llist->next); |
| 161 | llist_add_tail(llist, head); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 164 | /*! Test whether a linked list is empty. |
| 165 | * \param[in] head the llist to test. |
| 166 | * \returns 1 if the list is empty, 0 otherwise. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 167 | */ |
| 168 | static inline int llist_empty(const struct llist_head *head) |
| 169 | { |
| 170 | return head->next == head; |
| 171 | } |
| 172 | |
| 173 | static inline void __llist_splice(struct llist_head *llist, |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 174 | struct llist_head *head) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 175 | { |
| 176 | struct llist_head *first = llist->next; |
| 177 | struct llist_head *last = llist->prev; |
| 178 | struct llist_head *at = head->next; |
| 179 | |
| 180 | first->prev = head; |
| 181 | head->next = first; |
| 182 | |
| 183 | last->next = at; |
| 184 | at->prev = last; |
| 185 | } |
| 186 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 187 | /*! Join two linked lists. |
| 188 | * \param llist the new linked list to add. |
| 189 | * \param head the place to add llist within the other list. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 190 | */ |
| 191 | static inline void llist_splice(struct llist_head *llist, struct llist_head *head) |
| 192 | { |
| 193 | if (!llist_empty(llist)) |
| 194 | __llist_splice(llist, head); |
| 195 | } |
| 196 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 197 | /*! Join two llists and reinitialise the emptied llist. |
| 198 | * \param llist the new linked list to add. |
| 199 | * \param head the place to add it within the first llist. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 200 | * |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 201 | * The llist is reinitialised. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 202 | */ |
| 203 | static inline void llist_splice_init(struct llist_head *llist, |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 204 | struct llist_head *head) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 205 | { |
| 206 | if (!llist_empty(llist)) { |
| 207 | __llist_splice(llist, head); |
| 208 | INIT_LLIST_HEAD(llist); |
| 209 | } |
| 210 | } |
| 211 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 212 | /*! Get the struct containing this list entry. |
| 213 | * \param ptr the llist_head pointer. |
| 214 | * \param type the type of the struct this is embedded in. |
| 215 | * \param member the name of the llist_head within the struct. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 216 | */ |
| 217 | #define llist_entry(ptr, type, member) \ |
| 218 | container_of(ptr, type, member) |
| 219 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 220 | /*! Get the first element from a linked list. |
Neels Hofmeyr | 4cb0c8b | 2017-03-14 22:48:02 +0100 | [diff] [blame] | 221 | * \param ptr the list head to take the element from. |
| 222 | * \param type the type of the struct this is embedded in. |
| 223 | * \param member the name of the list_head within the struct. |
| 224 | * |
| 225 | * Note, that list is expected to be not empty. |
| 226 | */ |
| 227 | #define llist_first_entry(ptr, type, member) \ |
| 228 | llist_entry((ptr)->next, type, member) |
| 229 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 230 | /*! Get the last element from a list. |
Neels Hofmeyr | 4cb0c8b | 2017-03-14 22:48:02 +0100 | [diff] [blame] | 231 | * \param ptr the list head to take the element from. |
| 232 | * \param type the type of the struct this is embedded in. |
| 233 | * \param member the name of the llist_head within the struct. |
| 234 | * |
| 235 | * Note, that list is expected to be not empty. |
| 236 | */ |
| 237 | #define llist_last_entry(ptr, type, member) \ |
| 238 | llist_entry((ptr)->prev, type, member) |
| 239 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 240 | /*! Get the first element from a list, or NULL. |
Neels Hofmeyr | 4cb0c8b | 2017-03-14 22:48:02 +0100 | [diff] [blame] | 241 | * \param ptr the list head to take the element from. |
| 242 | * \param type the type of the struct this is embedded in. |
| 243 | * \param member the name of the list_head within the struct. |
| 244 | * |
| 245 | * Note that if the list is empty, it returns NULL. |
| 246 | */ |
| 247 | #define llist_first_entry_or_null(ptr, type, member) \ |
| 248 | (!llist_empty(ptr) ? llist_first_entry(ptr, type, member) : NULL) |
| 249 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 250 | /*! Iterate over a linked list. |
| 251 | * \param pos the llist_head to use as a loop counter. |
| 252 | * \param head the head of the list over which to iterate. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 253 | */ |
| 254 | #define llist_for_each(pos, head) \ |
| 255 | for (pos = (head)->next, prefetch(pos->next); pos != (head); \ |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 256 | pos = pos->next, prefetch(pos->next)) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 257 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 258 | /*! Iterate over a linked list (no prefetch). |
| 259 | * \param pos the llist_head to use as a loop counter. |
| 260 | * \param head the head of the list over which to iterate. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 261 | * |
| 262 | * This variant differs from llist_for_each() in that it's the |
| 263 | * simplest possible llist iteration code, no prefetching is done. |
| 264 | * Use this for code that knows the llist to be very short (empty |
| 265 | * or 1 entry) most of the time. |
| 266 | */ |
| 267 | #define __llist_for_each(pos, head) \ |
| 268 | for (pos = (head)->next; pos != (head); pos = pos->next) |
| 269 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 270 | /*! Iterate over a linked list backwards. |
| 271 | * \param pos the llist_head to use as a loop counter. |
| 272 | * \param head the head of the list over which to iterate. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 273 | */ |
| 274 | #define llist_for_each_prev(pos, head) \ |
| 275 | for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \ |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 276 | pos = pos->prev, prefetch(pos->prev)) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 277 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 278 | /*! Iterate over a linked list, safe against removal of llist entry. |
| 279 | * \param pos the llist_head to use as a loop counter. |
| 280 | * \param n another llist_head to use as temporary storage. |
| 281 | * \param head the head of the list over which to iterate. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 282 | */ |
| 283 | #define llist_for_each_safe(pos, n, head) \ |
| 284 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
| 285 | pos = n, n = pos->next) |
| 286 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 287 | /*! Iterate over a linked list of a given type. |
| 288 | * \param pos the 'type *' to use as a loop counter. |
| 289 | * \param head the head of the list over which to iterate. |
| 290 | * \param member the name of the llist_head within the struct pos. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 291 | */ |
| 292 | #define llist_for_each_entry(pos, head, member) \ |
| 293 | for (pos = llist_entry((head)->next, typeof(*pos), member), \ |
| 294 | prefetch(pos->member.next); \ |
| 295 | &pos->member != (head); \ |
| 296 | pos = llist_entry(pos->member.next, typeof(*pos), member), \ |
| 297 | prefetch(pos->member.next)) |
| 298 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 299 | /*! Iterate backwards over a linked list of a given type. |
| 300 | * \param pos the 'type *' to use as a loop counter. |
| 301 | * \param head the head of the list over which to iterate. |
| 302 | * \param member the name of the llist_head within the struct pos. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 303 | */ |
| 304 | #define llist_for_each_entry_reverse(pos, head, member) \ |
| 305 | for (pos = llist_entry((head)->prev, typeof(*pos), member), \ |
| 306 | prefetch(pos->member.prev); \ |
| 307 | &pos->member != (head); \ |
| 308 | pos = llist_entry(pos->member.prev, typeof(*pos), member), \ |
| 309 | prefetch(pos->member.prev)) |
| 310 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 311 | /*! Iterate over a linked list of a given type, |
| 312 | * continuing after an existing point. |
| 313 | * \param pos the 'type *' to use as a loop counter. |
| 314 | * \param head the head of the list over which to iterate. |
| 315 | * \param member the name of the llist_head within the struct pos. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 316 | */ |
| 317 | #define llist_for_each_entry_continue(pos, head, member) \ |
| 318 | for (pos = llist_entry(pos->member.next, typeof(*pos), member), \ |
| 319 | prefetch(pos->member.next); \ |
| 320 | &pos->member != (head); \ |
| 321 | pos = llist_entry(pos->member.next, typeof(*pos), member), \ |
| 322 | prefetch(pos->member.next)) |
| 323 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 324 | /*! Iterate over llist of given type, safe against removal of |
| 325 | * non-consecutive(!) llist entries. |
| 326 | * \param pos the 'type *' to use as a loop counter. |
| 327 | * \param n another 'type *' to use as temporary storage. |
| 328 | * \param head the head of the list over which to iterate. |
| 329 | * \param member the name of the llist_head within the struct pos. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 330 | */ |
| 331 | #define llist_for_each_entry_safe(pos, n, head, member) \ |
| 332 | for (pos = llist_entry((head)->next, typeof(*pos), member), \ |
| 333 | n = llist_entry(pos->member.next, typeof(*pos), member); \ |
| 334 | &pos->member != (head); \ |
| 335 | pos = n, n = llist_entry(n->member.next, typeof(*n), member)) |
| 336 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 337 | /*! Iterate over an rcu-protected llist. |
| 338 | * \param pos the llist_head to use as a loop counter. |
| 339 | * \param head the head of the list over which to iterate. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 340 | */ |
| 341 | #define llist_for_each_rcu(pos, head) \ |
| 342 | for (pos = (head)->next, prefetch(pos->next); pos != (head); \ |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 343 | pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next)) |
| 344 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 345 | #define __llist_for_each_rcu(pos, head) \ |
| 346 | for (pos = (head)->next; pos != (head); \ |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 347 | pos = pos->next, ({ smp_read_barrier_depends(); 0;})) |
| 348 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 349 | /*! Iterate over an rcu-protected llist, safe against removal of llist entry. |
| 350 | * \param pos the llist_head to use as a loop counter. |
| 351 | * \param n another llist_head to use as temporary storage. |
| 352 | * \param head the head of the list over which to iterate. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 353 | */ |
| 354 | #define llist_for_each_safe_rcu(pos, n, head) \ |
| 355 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
| 356 | pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next) |
| 357 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 358 | /*! Iterate over an rcu-protected llist of a given type. |
| 359 | * \param pos the 'type *' to use as a loop counter. |
| 360 | * \param head the head of the list over which to iterate. |
| 361 | * \param member the name of the llist_struct within the struct. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 362 | */ |
| 363 | #define llist_for_each_entry_rcu(pos, head, member) \ |
| 364 | for (pos = llist_entry((head)->next, typeof(*pos), member), \ |
| 365 | prefetch(pos->member.next); \ |
| 366 | &pos->member != (head); \ |
| 367 | pos = llist_entry(pos->member.next, typeof(*pos), member), \ |
| 368 | ({ smp_read_barrier_depends(); 0;}), \ |
| 369 | prefetch(pos->member.next)) |
| 370 | |
| 371 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 372 | /*! Iterate over an rcu-protected llist, continuing after existing point. |
| 373 | * \param pos the llist_head to use as a loop counter. |
| 374 | * \param head the head of the list over which to iterate. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 375 | */ |
| 376 | #define llist_for_each_continue_rcu(pos, head) \ |
| 377 | for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \ |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 378 | (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next)) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 379 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 380 | /*! Count number of llist items by iterating. |
| 381 | * \param head the llist head to count items of. |
Neels Hofmeyr | 505a22f | 2017-01-11 00:33:10 +0100 | [diff] [blame] | 382 | * \returns Number of items. |
| 383 | * |
| 384 | * This function is not efficient, mostly useful for small lists and non time |
| 385 | * critical cases like unit tests. |
| 386 | */ |
Max | c01cff1 | 2018-12-10 12:46:03 +0100 | [diff] [blame] | 387 | static inline unsigned int llist_count(const struct llist_head *head) |
Neels Hofmeyr | 505a22f | 2017-01-11 00:33:10 +0100 | [diff] [blame] | 388 | { |
| 389 | struct llist_head *entry; |
| 390 | unsigned int i = 0; |
| 391 | llist_for_each(entry, head) |
| 392 | i++; |
| 393 | return i; |
| 394 | } |
| 395 | |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 396 | /*! |
Neels Hofmeyr | b525b9e | 2017-10-16 16:46:43 +0200 | [diff] [blame] | 397 | * @} |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 398 | */ |