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> |
Harald Welte | 7101ca2 | 2020-12-04 21:44:44 +0100 | [diff] [blame] | 19 | #include <stdbool.h> |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 20 | |
| 21 | #ifndef inline |
| 22 | #define inline __inline__ |
| 23 | #endif |
| 24 | |
| 25 | static inline void prefetch(const void *x) {;} |
| 26 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 27 | /*! Cast a member of a structure out to the containing structure. |
| 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. |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 31 | */ |
| 32 | #define container_of(ptr, type, member) ({ \ |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 33 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ |
| 34 | (type *)( (char *)__mptr - offsetof(type, member) );}) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 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 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 51 | /*! Define a new llist_head pointing to a given llist_head. |
| 52 | * \param[in] name another llist_head to be pointed. |
| 53 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 54 | #define LLIST_HEAD_INIT(name) { &(name), &(name) } |
| 55 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 56 | /*! Define a statically-initialized variable of type llist_head. |
| 57 | * \param[in] name variable (symbol) name. |
| 58 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 59 | #define LLIST_HEAD(name) \ |
| 60 | struct llist_head name = LLIST_HEAD_INIT(name) |
| 61 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 62 | /*! Initialize a llist_head to point back to itself. |
| 63 | * \param[in] ptr llist_head to be initialized. |
| 64 | */ |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 65 | #define INIT_LLIST_HEAD(ptr) do { \ |
| 66 | (ptr)->next = (ptr); (ptr)->prev = (ptr); \ |
| 67 | } while (0) |
| 68 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 69 | /* |
| 70 | * Insert a new entry between two known consecutive entries. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 71 | * |
| 72 | * This is only for internal llist manipulation where we know |
| 73 | * the prev/next entries already! |
| 74 | */ |
| 75 | static inline void __llist_add(struct llist_head *_new, |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 76 | struct llist_head *prev, |
| 77 | struct llist_head *next) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 78 | { |
| 79 | next->prev = _new; |
| 80 | _new->next = next; |
| 81 | _new->prev = prev; |
| 82 | prev->next = _new; |
| 83 | } |
| 84 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 85 | /*! Add a new entry into a linked list (at head). |
| 86 | * \param _new the entry to be added. |
| 87 | * \param head llist_head to prepend the element to. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 88 | * |
| 89 | * Insert a new entry after the specified head. |
| 90 | * This is good for implementing stacks. |
| 91 | */ |
| 92 | static inline void llist_add(struct llist_head *_new, struct llist_head *head) |
| 93 | { |
| 94 | __llist_add(_new, head, head->next); |
| 95 | } |
| 96 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 97 | /*! Add a new entry into a linked list (at tail). |
| 98 | * \param _new the entry to be added. |
| 99 | * \param head llist_head to append the element to. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 100 | * |
| 101 | * Insert a new entry before the specified head. |
| 102 | * This is useful for implementing queues. |
| 103 | */ |
| 104 | static inline void llist_add_tail(struct llist_head *_new, struct llist_head *head) |
| 105 | { |
| 106 | __llist_add(_new, head->prev, head); |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Delete a llist entry by making the prev/next entries |
| 111 | * point to each other. |
| 112 | * |
| 113 | * This is only for internal llist manipulation where we know |
| 114 | * the prev/next entries already! |
| 115 | */ |
| 116 | static inline void __llist_del(struct llist_head * prev, struct llist_head * next) |
| 117 | { |
| 118 | next->prev = prev; |
| 119 | prev->next = next; |
| 120 | } |
| 121 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 122 | /*! Delete a single entry from a linked list. |
| 123 | * \param entry the element to delete. |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 124 | * |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 125 | * Note: llist_empty on entry does not return true after this, the entry is |
| 126 | * in an undefined state. |
| 127 | */ |
| 128 | static inline void llist_del(struct llist_head *entry) |
| 129 | { |
| 130 | __llist_del(entry->prev, entry->next); |
| 131 | entry->next = (struct llist_head *)LLIST_POISON1; |
| 132 | entry->prev = (struct llist_head *)LLIST_POISON2; |
| 133 | } |
| 134 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 135 | /*! Delete a single entry from a linked list and reinitialize it. |
| 136 | * \param entry the element to delete and reinitialize. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 137 | */ |
| 138 | static inline void llist_del_init(struct llist_head *entry) |
| 139 | { |
| 140 | __llist_del(entry->prev, entry->next); |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 141 | INIT_LLIST_HEAD(entry); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 142 | } |
| 143 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 144 | /*! Delete from one llist and add as another's head. |
| 145 | * \param llist the entry to move. |
| 146 | * \param head the head that will precede our entry. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 147 | */ |
| 148 | static inline void llist_move(struct llist_head *llist, struct llist_head *head) |
| 149 | { |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 150 | __llist_del(llist->prev, llist->next); |
| 151 | llist_add(llist, head); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 152 | } |
| 153 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 154 | /*! Delete from one llist and add as another's tail. |
| 155 | * \param llist the entry to move. |
| 156 | * \param head the head that will follow our entry. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 157 | */ |
| 158 | static inline void llist_move_tail(struct llist_head *llist, |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 159 | struct llist_head *head) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 160 | { |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 161 | __llist_del(llist->prev, llist->next); |
| 162 | llist_add_tail(llist, head); |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 165 | /*! Test whether a linked list is empty. |
| 166 | * \param[in] head the llist to test. |
| 167 | * \returns 1 if the list is empty, 0 otherwise. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 168 | */ |
| 169 | static inline int llist_empty(const struct llist_head *head) |
| 170 | { |
| 171 | return head->next == head; |
| 172 | } |
| 173 | |
| 174 | static inline void __llist_splice(struct llist_head *llist, |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 175 | struct llist_head *head) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 176 | { |
| 177 | struct llist_head *first = llist->next; |
| 178 | struct llist_head *last = llist->prev; |
| 179 | struct llist_head *at = head->next; |
| 180 | |
| 181 | first->prev = head; |
| 182 | head->next = first; |
| 183 | |
| 184 | last->next = at; |
| 185 | at->prev = last; |
| 186 | } |
| 187 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 188 | /*! Join two linked lists. |
| 189 | * \param llist the new linked list to add. |
| 190 | * \param head the place to add llist within the other list. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 191 | */ |
| 192 | static inline void llist_splice(struct llist_head *llist, struct llist_head *head) |
| 193 | { |
| 194 | if (!llist_empty(llist)) |
| 195 | __llist_splice(llist, head); |
| 196 | } |
| 197 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 198 | /*! Join two llists and reinitialise the emptied llist. |
| 199 | * \param llist the new linked list to add. |
| 200 | * \param head the place to add it within the first llist. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 201 | * |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 202 | * The llist is reinitialised. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 203 | */ |
| 204 | static inline void llist_splice_init(struct llist_head *llist, |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 205 | struct llist_head *head) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 206 | { |
| 207 | if (!llist_empty(llist)) { |
| 208 | __llist_splice(llist, head); |
| 209 | INIT_LLIST_HEAD(llist); |
| 210 | } |
| 211 | } |
| 212 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 213 | /*! Get the struct containing this list entry. |
| 214 | * \param ptr the llist_head pointer. |
| 215 | * \param type the type of the struct this is embedded in. |
| 216 | * \param member the name of the llist_head within the struct. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 217 | */ |
| 218 | #define llist_entry(ptr, type, member) \ |
| 219 | container_of(ptr, type, member) |
| 220 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 221 | /*! Get the first element from a linked list. |
Neels Hofmeyr | 4cb0c8b | 2017-03-14 22:48:02 +0100 | [diff] [blame] | 222 | * \param ptr the list head to take the element from. |
| 223 | * \param type the type of the struct this is embedded in. |
| 224 | * \param member the name of the list_head within the struct. |
| 225 | * |
| 226 | * Note, that list is expected to be not empty. |
| 227 | */ |
| 228 | #define llist_first_entry(ptr, type, member) \ |
| 229 | llist_entry((ptr)->next, type, member) |
| 230 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 231 | /*! Get the last element from a list. |
Neels Hofmeyr | 4cb0c8b | 2017-03-14 22:48:02 +0100 | [diff] [blame] | 232 | * \param ptr the list head to take the element from. |
| 233 | * \param type the type of the struct this is embedded in. |
| 234 | * \param member the name of the llist_head within the struct. |
| 235 | * |
| 236 | * Note, that list is expected to be not empty. |
| 237 | */ |
| 238 | #define llist_last_entry(ptr, type, member) \ |
| 239 | llist_entry((ptr)->prev, type, member) |
| 240 | |
Philipp Maier | ff84de7 | 2021-06-30 17:06:16 +0200 | [diff] [blame] | 241 | /*! Return the last element of the list. |
| 242 | * \param head the llist head of the list. |
| 243 | * \returns last element of the list, head if the list is empty. |
| 244 | */ |
| 245 | #define llist_last(head) (head)->prev |
| 246 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 247 | /*! Get the first element from a list, or NULL. |
Neels Hofmeyr | 4cb0c8b | 2017-03-14 22:48:02 +0100 | [diff] [blame] | 248 | * \param ptr the list head to take the element from. |
| 249 | * \param type the type of the struct this is embedded in. |
| 250 | * \param member the name of the list_head within the struct. |
| 251 | * |
| 252 | * Note that if the list is empty, it returns NULL. |
| 253 | */ |
| 254 | #define llist_first_entry_or_null(ptr, type, member) \ |
| 255 | (!llist_empty(ptr) ? llist_first_entry(ptr, type, member) : NULL) |
| 256 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 257 | /*! Iterate over a linked list. |
| 258 | * \param pos the llist_head to use as a loop counter. |
| 259 | * \param head the head of the list over which to iterate. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 260 | */ |
| 261 | #define llist_for_each(pos, head) \ |
| 262 | for (pos = (head)->next, prefetch(pos->next); pos != (head); \ |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 263 | pos = pos->next, prefetch(pos->next)) |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 264 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 265 | /*! Iterate over a linked list (no prefetch). |
| 266 | * \param pos the llist_head to use as a loop counter. |
| 267 | * \param head the head of the list over which to iterate. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 268 | * |
| 269 | * This variant differs from llist_for_each() in that it's the |
| 270 | * simplest possible llist iteration code, no prefetching is done. |
| 271 | * Use this for code that knows the llist to be very short (empty |
| 272 | * or 1 entry) most of the time. |
| 273 | */ |
| 274 | #define __llist_for_each(pos, head) \ |
| 275 | for (pos = (head)->next; pos != (head); pos = pos->next) |
| 276 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 277 | /*! Iterate over a linked list backwards. |
| 278 | * \param pos the llist_head to use as a loop counter. |
| 279 | * \param head the head of the list over which to iterate. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 280 | */ |
| 281 | #define llist_for_each_prev(pos, head) \ |
| 282 | for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \ |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 283 | pos = pos->prev, prefetch(pos->prev)) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 284 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 285 | /*! Iterate over a linked list, safe against removal of llist entry. |
| 286 | * \param pos the llist_head to use as a loop counter. |
| 287 | * \param n another llist_head to use as temporary storage. |
| 288 | * \param head the head of the list over which to iterate. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 289 | */ |
| 290 | #define llist_for_each_safe(pos, n, head) \ |
| 291 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
| 292 | pos = n, n = pos->next) |
| 293 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 294 | /*! Iterate over a linked list of a given type. |
| 295 | * \param pos the 'type *' to use as a loop counter. |
| 296 | * \param head the head of the list over which to iterate. |
| 297 | * \param member the name of the llist_head within the struct pos. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 298 | */ |
| 299 | #define llist_for_each_entry(pos, head, member) \ |
| 300 | for (pos = llist_entry((head)->next, typeof(*pos), member), \ |
| 301 | prefetch(pos->member.next); \ |
| 302 | &pos->member != (head); \ |
| 303 | pos = llist_entry(pos->member.next, typeof(*pos), member), \ |
| 304 | prefetch(pos->member.next)) |
| 305 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 306 | /*! Iterate backwards over a linked list of a given type. |
| 307 | * \param pos the 'type *' to use as a loop counter. |
| 308 | * \param head the head of the list over which to iterate. |
| 309 | * \param member the name of the llist_head within the struct pos. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 310 | */ |
| 311 | #define llist_for_each_entry_reverse(pos, head, member) \ |
| 312 | for (pos = llist_entry((head)->prev, typeof(*pos), member), \ |
| 313 | prefetch(pos->member.prev); \ |
| 314 | &pos->member != (head); \ |
| 315 | pos = llist_entry(pos->member.prev, typeof(*pos), member), \ |
| 316 | prefetch(pos->member.prev)) |
| 317 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 318 | /*! Iterate over a linked list of a given type, |
| 319 | * continuing after an existing point. |
| 320 | * \param pos the 'type *' to use as a loop counter. |
| 321 | * \param head the head of the list over which to iterate. |
| 322 | * \param member the name of the llist_head within the struct pos. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 323 | */ |
| 324 | #define llist_for_each_entry_continue(pos, head, member) \ |
| 325 | for (pos = llist_entry(pos->member.next, typeof(*pos), member), \ |
| 326 | prefetch(pos->member.next); \ |
| 327 | &pos->member != (head); \ |
| 328 | pos = llist_entry(pos->member.next, typeof(*pos), member), \ |
| 329 | prefetch(pos->member.next)) |
| 330 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 331 | /*! Iterate over llist of given type, safe against removal of |
| 332 | * non-consecutive(!) llist entries. |
| 333 | * \param pos the 'type *' to use as a loop counter. |
| 334 | * \param n another 'type *' to use as temporary storage. |
| 335 | * \param head the head of the list over which to iterate. |
| 336 | * \param member the name of the llist_head within the struct pos. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 337 | */ |
| 338 | #define llist_for_each_entry_safe(pos, n, head, member) \ |
| 339 | for (pos = llist_entry((head)->next, typeof(*pos), member), \ |
| 340 | n = llist_entry(pos->member.next, typeof(*pos), member); \ |
| 341 | &pos->member != (head); \ |
| 342 | pos = n, n = llist_entry(n->member.next, typeof(*n), member)) |
| 343 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 344 | /*! Iterate over an rcu-protected llist. |
| 345 | * \param pos the llist_head to use as a loop counter. |
| 346 | * \param head the head of the list over which to iterate. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 347 | */ |
| 348 | #define llist_for_each_rcu(pos, head) \ |
| 349 | for (pos = (head)->next, prefetch(pos->next); pos != (head); \ |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 350 | pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next)) |
| 351 | |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 352 | #define __llist_for_each_rcu(pos, head) \ |
| 353 | for (pos = (head)->next; pos != (head); \ |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 354 | pos = pos->next, ({ smp_read_barrier_depends(); 0;})) |
| 355 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 356 | /*! Iterate over an rcu-protected llist, safe against removal of llist entry. |
| 357 | * \param pos the llist_head to use as a loop counter. |
| 358 | * \param n another llist_head to use as temporary storage. |
| 359 | * \param head the head of the list over which to iterate. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 360 | */ |
| 361 | #define llist_for_each_safe_rcu(pos, n, head) \ |
| 362 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
| 363 | pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next) |
| 364 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 365 | /*! Iterate over an rcu-protected llist of a given type. |
| 366 | * \param pos the 'type *' to use as a loop counter. |
| 367 | * \param head the head of the list over which to iterate. |
| 368 | * \param member the name of the llist_struct within the struct. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 369 | */ |
| 370 | #define llist_for_each_entry_rcu(pos, head, member) \ |
| 371 | for (pos = llist_entry((head)->next, typeof(*pos), member), \ |
| 372 | prefetch(pos->member.next); \ |
| 373 | &pos->member != (head); \ |
| 374 | pos = llist_entry(pos->member.next, typeof(*pos), member), \ |
| 375 | ({ smp_read_barrier_depends(); 0;}), \ |
| 376 | prefetch(pos->member.next)) |
| 377 | |
| 378 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 379 | /*! Iterate over an rcu-protected llist, continuing after existing point. |
| 380 | * \param pos the llist_head to use as a loop counter. |
| 381 | * \param head the head of the list over which to iterate. |
Harald Welte | ec8b450 | 2010-02-20 20:34:29 +0100 | [diff] [blame] | 382 | */ |
| 383 | #define llist_for_each_continue_rcu(pos, head) \ |
| 384 | for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \ |
Vadim Yanitskiy | 095a396 | 2019-03-25 21:13:03 +0700 | [diff] [blame] | 385 | (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next)) |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 386 | |
Vadim Yanitskiy | 72ea919 | 2019-03-25 20:58:16 +0700 | [diff] [blame] | 387 | /*! Count number of llist items by iterating. |
| 388 | * \param head the llist head to count items of. |
Neels Hofmeyr | 505a22f | 2017-01-11 00:33:10 +0100 | [diff] [blame] | 389 | * \returns Number of items. |
| 390 | * |
| 391 | * This function is not efficient, mostly useful for small lists and non time |
| 392 | * critical cases like unit tests. |
| 393 | */ |
Max | c01cff1 | 2018-12-10 12:46:03 +0100 | [diff] [blame] | 394 | static inline unsigned int llist_count(const struct llist_head *head) |
Neels Hofmeyr | 505a22f | 2017-01-11 00:33:10 +0100 | [diff] [blame] | 395 | { |
| 396 | struct llist_head *entry; |
| 397 | unsigned int i = 0; |
| 398 | llist_for_each(entry, head) |
| 399 | i++; |
| 400 | return i; |
| 401 | } |
| 402 | |
Harald Welte | 7101ca2 | 2020-12-04 21:44:44 +0100 | [diff] [blame] | 403 | |
| 404 | |
| 405 | /*! Double linked lists with a single pointer list head. |
| 406 | * Mostly useful for hash tables where the two pointer list head is |
| 407 | * too wasteful. |
| 408 | * You lose the ability to access the tail in O(1). |
| 409 | */ |
| 410 | |
| 411 | struct hlist_head { |
| 412 | struct hlist_node *first; |
| 413 | }; |
| 414 | |
| 415 | struct hlist_node { |
| 416 | struct hlist_node *next, **pprev; |
| 417 | }; |
| 418 | |
| 419 | #define HLIST_HEAD_INIT { .first = NULL } |
| 420 | #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } |
| 421 | #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) |
| 422 | static inline void INIT_HLIST_NODE(struct hlist_node *h) |
| 423 | { |
| 424 | h->next = NULL; |
| 425 | h->pprev = NULL; |
| 426 | } |
| 427 | |
| 428 | #define READ_ONCE(x) x |
| 429 | #define WRITE_ONCE(a, b) a = b |
| 430 | |
| 431 | /*! Has node been removed from list and reinitialized?. |
| 432 | * \param[in] h: Node to be checked |
| 433 | * \return 1 if node is unhashed; 0 if not |
| 434 | * |
| 435 | * Not that not all removal functions will leave a node in unhashed |
| 436 | * state. For example, hlist_nulls_del_init_rcu() does leave the |
| 437 | * node in unhashed state, but hlist_nulls_del() does not. |
| 438 | */ |
| 439 | static inline int hlist_unhashed(const struct hlist_node *h) |
| 440 | { |
| 441 | return !h->pprev; |
| 442 | } |
| 443 | |
| 444 | /*! Version of hlist_unhashed for lockless use. |
| 445 | * \param[in] n Node to be checked |
| 446 | * \return 1 if node is unhashed; 0 if not |
| 447 | * |
| 448 | * This variant of hlist_unhashed() must be used in lockless contexts |
| 449 | * to avoid potential load-tearing. The READ_ONCE() is paired with the |
| 450 | * various WRITE_ONCE() in hlist helpers that are defined below. |
| 451 | */ |
| 452 | static inline int hlist_unhashed_lockless(const struct hlist_node *h) |
| 453 | { |
| 454 | return !READ_ONCE(h->pprev); |
| 455 | } |
| 456 | |
| 457 | /*!Is the specified hlist_head structure an empty hlist?. |
| 458 | * \param[in] h Structure to check. |
| 459 | * \return 1 if hlist is empty; 0 if not |
| 460 | */ |
| 461 | static inline int hlist_empty(const struct hlist_head *h) |
| 462 | { |
| 463 | return !READ_ONCE(h->first); |
| 464 | } |
| 465 | |
| 466 | static inline void __hlist_del(struct hlist_node *n) |
| 467 | { |
| 468 | struct hlist_node *next = n->next; |
| 469 | struct hlist_node **pprev = n->pprev; |
| 470 | |
| 471 | WRITE_ONCE(*pprev, next); |
| 472 | if (next) |
| 473 | WRITE_ONCE(next->pprev, pprev); |
| 474 | } |
| 475 | |
| 476 | /*! Delete the specified hlist_node from its list. |
| 477 | * \param[in] n: Node to delete. |
| 478 | * |
| 479 | * Note that this function leaves the node in hashed state. Use |
| 480 | * hlist_del_init() or similar instead to unhash @n. |
| 481 | */ |
| 482 | static inline void hlist_del(struct hlist_node *n) |
| 483 | { |
| 484 | __hlist_del(n); |
Harald Welte | 77530b4 | 2020-12-05 20:14:54 +0100 | [diff] [blame] | 485 | n->next = (struct hlist_node *)LLIST_POISON1; |
| 486 | n->pprev = (struct hlist_node **)LLIST_POISON2; |
Harald Welte | 7101ca2 | 2020-12-04 21:44:44 +0100 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /*! Delete the specified hlist_node from its list and initialize. |
| 490 | * \param[in] n Node to delete. |
| 491 | * |
| 492 | * Note that this function leaves the node in unhashed state. |
| 493 | */ |
| 494 | static inline void hlist_del_init(struct hlist_node *n) |
| 495 | { |
| 496 | if (!hlist_unhashed(n)) { |
| 497 | __hlist_del(n); |
| 498 | INIT_HLIST_NODE(n); |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /*! add a new entry at the beginning of the hlist. |
| 503 | * \param[in] n new entry to be added |
| 504 | * \param[in] h hlist head to add it after |
| 505 | * |
| 506 | * Insert a new entry after the specified head. |
| 507 | * This is good for implementing stacks. |
| 508 | */ |
| 509 | static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) |
| 510 | { |
| 511 | struct hlist_node *first = h->first; |
| 512 | WRITE_ONCE(n->next, first); |
| 513 | if (first) |
| 514 | WRITE_ONCE(first->pprev, &n->next); |
| 515 | WRITE_ONCE(h->first, n); |
| 516 | WRITE_ONCE(n->pprev, &h->first); |
| 517 | } |
| 518 | |
| 519 | /*! add a new entry before the one specified. |
| 520 | * @n: new entry to be added |
| 521 | * @next: hlist node to add it before, which must be non-NULL |
| 522 | */ |
| 523 | static inline void hlist_add_before(struct hlist_node *n, |
| 524 | struct hlist_node *next) |
| 525 | { |
| 526 | WRITE_ONCE(n->pprev, next->pprev); |
| 527 | WRITE_ONCE(n->next, next); |
| 528 | WRITE_ONCE(next->pprev, &n->next); |
| 529 | WRITE_ONCE(*(n->pprev), n); |
| 530 | } |
| 531 | |
| 532 | /*! add a new entry after the one specified |
| 533 | * \param[in] n new entry to be added |
| 534 | * \param[in] prev hlist node to add it after, which must be non-NULL |
| 535 | */ |
| 536 | static inline void hlist_add_behind(struct hlist_node *n, |
| 537 | struct hlist_node *prev) |
| 538 | { |
| 539 | WRITE_ONCE(n->next, prev->next); |
| 540 | WRITE_ONCE(prev->next, n); |
| 541 | WRITE_ONCE(n->pprev, &prev->next); |
| 542 | |
| 543 | if (n->next) |
| 544 | WRITE_ONCE(n->next->pprev, &n->next); |
| 545 | } |
| 546 | |
| 547 | /*! create a fake hlist consisting of a single headless node. |
| 548 | * \param[in] n Node to make a fake list out of |
| 549 | * |
| 550 | * This makes @n appear to be its own predecessor on a headless hlist. |
| 551 | * The point of this is to allow things like hlist_del() to work correctly |
| 552 | * in cases where there is no list. |
| 553 | */ |
| 554 | static inline void hlist_add_fake(struct hlist_node *n) |
| 555 | { |
| 556 | n->pprev = &n->next; |
| 557 | } |
| 558 | |
| 559 | /*! Is this node a fake hlist?. |
| 560 | * \param[in] h Node to check for being a self-referential fake hlist. |
| 561 | */ |
| 562 | static inline bool hlist_fake(struct hlist_node *h) |
| 563 | { |
| 564 | return h->pprev == &h->next; |
| 565 | } |
| 566 | |
| 567 | /*!is node the only element of the specified hlist?. |
| 568 | * \param[in] n Node to check for singularity. |
| 569 | * \param[in] h Header for potentially singular list. |
| 570 | * |
| 571 | * Check whether the node is the only node of the head without |
| 572 | * accessing head, thus avoiding unnecessary cache misses. |
| 573 | */ |
| 574 | static inline bool |
| 575 | hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h) |
| 576 | { |
| 577 | return !n->next && n->pprev == &h->first; |
| 578 | } |
| 579 | |
| 580 | /*! Move an hlist. |
| 581 | * \param[in] old hlist_head for old list. |
| 582 | * \param[in] new hlist_head for new list. |
| 583 | * |
| 584 | * Move a list from one list head to another. Fixup the pprev |
| 585 | * reference of the first entry if it exists. |
| 586 | */ |
| 587 | static inline void hlist_move_list(struct hlist_head *old, |
Vadim Yanitskiy | 59e13e4 | 2020-12-05 14:51:22 +0100 | [diff] [blame] | 588 | struct hlist_head *_new) |
Harald Welte | 7101ca2 | 2020-12-04 21:44:44 +0100 | [diff] [blame] | 589 | { |
Vadim Yanitskiy | 59e13e4 | 2020-12-05 14:51:22 +0100 | [diff] [blame] | 590 | _new->first = old->first; |
| 591 | if (_new->first) |
| 592 | _new->first->pprev = &_new->first; |
Harald Welte | 7101ca2 | 2020-12-04 21:44:44 +0100 | [diff] [blame] | 593 | old->first = NULL; |
| 594 | } |
| 595 | |
| 596 | #define hlist_entry(ptr, type, member) container_of(ptr,type,member) |
| 597 | |
| 598 | #define hlist_for_each(pos, head) \ |
| 599 | for (pos = (head)->first; pos ; pos = pos->next) |
| 600 | |
| 601 | #define hlist_for_each_safe(pos, n, head) \ |
| 602 | for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ |
| 603 | pos = n) |
| 604 | |
| 605 | #define hlist_entry_safe(ptr, type, member) \ |
| 606 | ({ typeof(ptr) ____ptr = (ptr); \ |
| 607 | ____ptr ? hlist_entry(____ptr, type, member) : NULL; \ |
| 608 | }) |
| 609 | |
| 610 | /*! iterate over list of given type. |
| 611 | * \param[out] pos the type * to use as a loop cursor. |
| 612 | * \param[in] head the head for your list. |
| 613 | * \param[in] member the name of the hlist_node within the struct. |
| 614 | */ |
| 615 | #define hlist_for_each_entry(pos, head, member) \ |
| 616 | for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\ |
| 617 | pos; \ |
| 618 | pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) |
| 619 | |
| 620 | /*! iterate over a hlist continuing after current point. |
| 621 | * \param[out] pos the type * to use as a loop cursor. |
| 622 | * \param[in] member the name of the hlist_node within the struct. |
| 623 | */ |
| 624 | #define hlist_for_each_entry_continue(pos, member) \ |
| 625 | for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\ |
| 626 | pos; \ |
| 627 | pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) |
| 628 | |
| 629 | /*! iterate over a hlist continuing from current point. |
| 630 | * \param[out] pos the type * to use as a loop cursor. |
| 631 | * \param[in] member the name of the hlist_node within the struct. |
| 632 | */ |
| 633 | #define hlist_for_each_entry_from(pos, member) \ |
| 634 | for (; pos; \ |
| 635 | pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member)) |
| 636 | |
| 637 | /*! hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry. |
| 638 | * \param[out] pos the type * to use as a loop cursor. |
| 639 | * \param[out] n a &struct hlist_node to use as temporary storage |
| 640 | * \param[in] head the head for your list. |
| 641 | * \param[in] member the name of the hlist_node within the struct |
| 642 | */ |
| 643 | #define hlist_for_each_entry_safe(pos, n, head, member) \ |
| 644 | for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\ |
| 645 | pos && ({ n = pos->member.next; 1; }); \ |
| 646 | pos = hlist_entry_safe(n, typeof(*pos), member)) |
| 647 | |
| 648 | |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 649 | /*! |
Neels Hofmeyr | b525b9e | 2017-10-16 16:46:43 +0200 | [diff] [blame] | 650 | * @} |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 651 | */ |