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