blob: 2c3a0a4c5162575b1280548607efac9448cc19bd [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file linuxlist.h
Harald Welte2d2e2cc2016-04-25 12:11:20 +02002 *
Neels Hofmeyr87e45502017-06-20 00:17:59 +02003 * Simple doubly linked list implementation.
Harald Welteec8b4502010-02-20 20:34:29 +01004 *
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 Hofmeyr17518fe2017-06-20 04:35:06 +020012#pragma once
13
14/*! \defgroup linuxlist Simple doubly linked list implementation
15 * @{
16 * \file linuxlist.h */
17
Harald Welte2d2e2cc2016-04-25 12:11:20 +020018#include <stddef.h>
Harald Welte7101ca22020-12-04 21:44:44 +010019#include <stdbool.h>
Harald Welte2d2e2cc2016-04-25 12:11:20 +020020
21#ifndef inline
22#define inline __inline__
23#endif
24
25static inline void prefetch(const void *x) {;}
26
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +070027/*! 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 Welte2d2e2cc2016-04-25 12:11:20 +020031 */
32#define container_of(ptr, type, member) ({ \
Vadim Yanitskiy095a3962019-03-25 21:13:03 +070033 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
34 (type *)( (char *)__mptr - offsetof(type, member) );})
Harald Welte2d2e2cc2016-04-25 12:11:20 +020035
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 Hofmeyr87e45502017-06-20 00:17:59 +020045/*! (double) linked list header structure */
Harald Welteec8b4502010-02-20 20:34:29 +010046struct llist_head {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020047 /*! Pointer to next and previous item */
Harald Welteec8b4502010-02-20 20:34:29 +010048 struct llist_head *next, *prev;
49};
50
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +070051/*! Define a new llist_head pointing to a given llist_head.
52 * \param[in] name another llist_head to be pointed.
53 */
Harald Welteec8b4502010-02-20 20:34:29 +010054#define LLIST_HEAD_INIT(name) { &(name), &(name) }
55
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +070056/*! Define a statically-initialized variable of type llist_head.
57 * \param[in] name variable (symbol) name.
58 */
Harald Welteec8b4502010-02-20 20:34:29 +010059#define LLIST_HEAD(name) \
60 struct llist_head name = LLIST_HEAD_INIT(name)
61
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +070062/*! Initialize a llist_head to point back to itself.
63 * \param[in] ptr llist_head to be initialized.
64 */
Harald Welteec8b4502010-02-20 20:34:29 +010065#define INIT_LLIST_HEAD(ptr) do { \
66 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
67} while (0)
68
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +070069/*
70 * Insert a new entry between two known consecutive entries.
Harald Welteec8b4502010-02-20 20:34:29 +010071 *
72 * This is only for internal llist manipulation where we know
73 * the prev/next entries already!
74 */
75static inline void __llist_add(struct llist_head *_new,
Vadim Yanitskiy095a3962019-03-25 21:13:03 +070076 struct llist_head *prev,
77 struct llist_head *next)
Harald Welteec8b4502010-02-20 20:34:29 +010078{
79 next->prev = _new;
80 _new->next = next;
81 _new->prev = prev;
82 prev->next = _new;
83}
84
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +070085/*! 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 Welteec8b4502010-02-20 20:34:29 +010088 *
89 * Insert a new entry after the specified head.
90 * This is good for implementing stacks.
91 */
92static inline void llist_add(struct llist_head *_new, struct llist_head *head)
93{
94 __llist_add(_new, head, head->next);
95}
96
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +070097/*! 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 Welteec8b4502010-02-20 20:34:29 +0100100 *
101 * Insert a new entry before the specified head.
102 * This is useful for implementing queues.
103 */
104static 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 */
116static inline void __llist_del(struct llist_head * prev, struct llist_head * next)
117{
118 next->prev = prev;
119 prev->next = next;
120}
121
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700122/*! Delete a single entry from a linked list.
123 * \param entry the element to delete.
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200124 *
Harald Welteec8b4502010-02-20 20:34:29 +0100125 * Note: llist_empty on entry does not return true after this, the entry is
126 * in an undefined state.
127 */
128static 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 Yanitskiy72ea9192019-03-25 20:58:16 +0700135/*! Delete a single entry from a linked list and reinitialize it.
136 * \param entry the element to delete and reinitialize.
Harald Welteec8b4502010-02-20 20:34:29 +0100137 */
138static inline void llist_del_init(struct llist_head *entry)
139{
140 __llist_del(entry->prev, entry->next);
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700141 INIT_LLIST_HEAD(entry);
Harald Welteec8b4502010-02-20 20:34:29 +0100142}
143
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700144/*! 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 Welteec8b4502010-02-20 20:34:29 +0100147 */
148static inline void llist_move(struct llist_head *llist, struct llist_head *head)
149{
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700150 __llist_del(llist->prev, llist->next);
151 llist_add(llist, head);
Harald Welteec8b4502010-02-20 20:34:29 +0100152}
153
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700154/*! 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 Welteec8b4502010-02-20 20:34:29 +0100157 */
158static inline void llist_move_tail(struct llist_head *llist,
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700159 struct llist_head *head)
Harald Welteec8b4502010-02-20 20:34:29 +0100160{
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700161 __llist_del(llist->prev, llist->next);
162 llist_add_tail(llist, head);
Harald Welteec8b4502010-02-20 20:34:29 +0100163}
164
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700165/*! 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 Welteec8b4502010-02-20 20:34:29 +0100168 */
169static inline int llist_empty(const struct llist_head *head)
170{
171 return head->next == head;
172}
173
174static inline void __llist_splice(struct llist_head *llist,
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700175 struct llist_head *head)
Harald Welteec8b4502010-02-20 20:34:29 +0100176{
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 Yanitskiy72ea9192019-03-25 20:58:16 +0700188/*! 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 Welteec8b4502010-02-20 20:34:29 +0100191 */
192static 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 Yanitskiy72ea9192019-03-25 20:58:16 +0700198/*! 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 Welteec8b4502010-02-20 20:34:29 +0100201 *
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700202 * The llist is reinitialised.
Harald Welteec8b4502010-02-20 20:34:29 +0100203 */
204static inline void llist_splice_init(struct llist_head *llist,
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700205 struct llist_head *head)
Harald Welteec8b4502010-02-20 20:34:29 +0100206{
207 if (!llist_empty(llist)) {
208 __llist_splice(llist, head);
209 INIT_LLIST_HEAD(llist);
210 }
211}
212
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700213/*! 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 Welteec8b4502010-02-20 20:34:29 +0100217 */
218#define llist_entry(ptr, type, member) \
219 container_of(ptr, type, member)
220
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700221/*! Get the first element from a linked list.
Neels Hofmeyr4cb0c8b2017-03-14 22:48:02 +0100222 * \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 Yanitskiy72ea9192019-03-25 20:58:16 +0700231/*! Get the last element from a list.
Neels Hofmeyr4cb0c8b2017-03-14 22:48:02 +0100232 * \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
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700241/*! Get the first element from a list, or NULL.
Neels Hofmeyr4cb0c8b2017-03-14 22:48:02 +0100242 * \param ptr the list head to take the element from.
243 * \param type the type of the struct this is embedded in.
244 * \param member the name of the list_head within the struct.
245 *
246 * Note that if the list is empty, it returns NULL.
247 */
248#define llist_first_entry_or_null(ptr, type, member) \
249 (!llist_empty(ptr) ? llist_first_entry(ptr, type, member) : NULL)
250
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700251/*! Iterate over a linked list.
252 * \param pos the llist_head to use as a loop counter.
253 * \param head the head of the list over which to iterate.
Harald Welteec8b4502010-02-20 20:34:29 +0100254 */
255#define llist_for_each(pos, head) \
256 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700257 pos = pos->next, prefetch(pos->next))
Harald Welteec8b4502010-02-20 20:34:29 +0100258
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700259/*! Iterate over a linked list (no prefetch).
260 * \param pos the llist_head to use as a loop counter.
261 * \param head the head of the list over which to iterate.
Harald Welteec8b4502010-02-20 20:34:29 +0100262 *
263 * This variant differs from llist_for_each() in that it's the
264 * simplest possible llist iteration code, no prefetching is done.
265 * Use this for code that knows the llist to be very short (empty
266 * or 1 entry) most of the time.
267 */
268#define __llist_for_each(pos, head) \
269 for (pos = (head)->next; pos != (head); pos = pos->next)
270
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700271/*! Iterate over a linked list backwards.
272 * \param pos the llist_head to use as a loop counter.
273 * \param head the head of the list over which to iterate.
Harald Welteec8b4502010-02-20 20:34:29 +0100274 */
275#define llist_for_each_prev(pos, head) \
276 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700277 pos = pos->prev, prefetch(pos->prev))
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200278
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700279/*! Iterate over a linked list, safe against removal of llist entry.
280 * \param pos the llist_head to use as a loop counter.
281 * \param n another llist_head to use as temporary storage.
282 * \param head the head of the list over which to iterate.
Harald Welteec8b4502010-02-20 20:34:29 +0100283 */
284#define llist_for_each_safe(pos, n, head) \
285 for (pos = (head)->next, n = pos->next; pos != (head); \
286 pos = n, n = pos->next)
287
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700288/*! Iterate over a linked list of a given type.
289 * \param pos the 'type *' to use as a loop counter.
290 * \param head the head of the list over which to iterate.
291 * \param member the name of the llist_head within the struct pos.
Harald Welteec8b4502010-02-20 20:34:29 +0100292 */
293#define llist_for_each_entry(pos, head, member) \
294 for (pos = llist_entry((head)->next, typeof(*pos), member), \
295 prefetch(pos->member.next); \
296 &pos->member != (head); \
297 pos = llist_entry(pos->member.next, typeof(*pos), member), \
298 prefetch(pos->member.next))
299
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700300/*! Iterate backwards over a linked list of a given type.
301 * \param pos the 'type *' to use as a loop counter.
302 * \param head the head of the list over which to iterate.
303 * \param member the name of the llist_head within the struct pos.
Harald Welteec8b4502010-02-20 20:34:29 +0100304 */
305#define llist_for_each_entry_reverse(pos, head, member) \
306 for (pos = llist_entry((head)->prev, typeof(*pos), member), \
307 prefetch(pos->member.prev); \
308 &pos->member != (head); \
309 pos = llist_entry(pos->member.prev, typeof(*pos), member), \
310 prefetch(pos->member.prev))
311
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700312/*! Iterate over a linked list of a given type,
313 * continuing after an existing point.
314 * \param pos the 'type *' to use as a loop counter.
315 * \param head the head of the list over which to iterate.
316 * \param member the name of the llist_head within the struct pos.
Harald Welteec8b4502010-02-20 20:34:29 +0100317 */
318#define llist_for_each_entry_continue(pos, head, member) \
319 for (pos = llist_entry(pos->member.next, typeof(*pos), member), \
320 prefetch(pos->member.next); \
321 &pos->member != (head); \
322 pos = llist_entry(pos->member.next, typeof(*pos), member), \
323 prefetch(pos->member.next))
324
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700325/*! Iterate over llist of given type, safe against removal of
326 * non-consecutive(!) llist entries.
327 * \param pos the 'type *' to use as a loop counter.
328 * \param n another 'type *' to use as temporary storage.
329 * \param head the head of the list over which to iterate.
330 * \param member the name of the llist_head within the struct pos.
Harald Welteec8b4502010-02-20 20:34:29 +0100331 */
332#define llist_for_each_entry_safe(pos, n, head, member) \
333 for (pos = llist_entry((head)->next, typeof(*pos), member), \
334 n = llist_entry(pos->member.next, typeof(*pos), member); \
335 &pos->member != (head); \
336 pos = n, n = llist_entry(n->member.next, typeof(*n), member))
337
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700338/*! Iterate over an rcu-protected llist.
339 * \param pos the llist_head to use as a loop counter.
340 * \param head the head of the list over which to iterate.
Harald Welteec8b4502010-02-20 20:34:29 +0100341 */
342#define llist_for_each_rcu(pos, head) \
343 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700344 pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
345
Harald Welteec8b4502010-02-20 20:34:29 +0100346#define __llist_for_each_rcu(pos, head) \
347 for (pos = (head)->next; pos != (head); \
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700348 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
349
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700350/*! Iterate over an rcu-protected llist, safe against removal of llist entry.
351 * \param pos the llist_head to use as a loop counter.
352 * \param n another llist_head to use as temporary storage.
353 * \param head the head of the list over which to iterate.
Harald Welteec8b4502010-02-20 20:34:29 +0100354 */
355#define llist_for_each_safe_rcu(pos, n, head) \
356 for (pos = (head)->next, n = pos->next; pos != (head); \
357 pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
358
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700359/*! Iterate over an rcu-protected llist of a given type.
360 * \param pos the 'type *' to use as a loop counter.
361 * \param head the head of the list over which to iterate.
362 * \param member the name of the llist_struct within the struct.
Harald Welteec8b4502010-02-20 20:34:29 +0100363 */
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
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700373/*! Iterate over an rcu-protected llist, continuing after existing point.
374 * \param pos the llist_head to use as a loop counter.
375 * \param head the head of the list over which to iterate.
Harald Welteec8b4502010-02-20 20:34:29 +0100376 */
377#define llist_for_each_continue_rcu(pos, head) \
378 for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700379 (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200380
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700381/*! Count number of llist items by iterating.
382 * \param head the llist head to count items of.
Neels Hofmeyr505a22f2017-01-11 00:33:10 +0100383 * \returns Number of items.
384 *
385 * This function is not efficient, mostly useful for small lists and non time
386 * critical cases like unit tests.
387 */
Maxc01cff12018-12-10 12:46:03 +0100388static inline unsigned int llist_count(const struct llist_head *head)
Neels Hofmeyr505a22f2017-01-11 00:33:10 +0100389{
390 struct llist_head *entry;
391 unsigned int i = 0;
392 llist_for_each(entry, head)
393 i++;
394 return i;
395}
396
Harald Welte7101ca22020-12-04 21:44:44 +0100397
398
399/*! Double linked lists with a single pointer list head.
400 * Mostly useful for hash tables where the two pointer list head is
401 * too wasteful.
402 * You lose the ability to access the tail in O(1).
403 */
404
405struct hlist_head {
406 struct hlist_node *first;
407};
408
409struct hlist_node {
410 struct hlist_node *next, **pprev;
411};
412
413#define HLIST_HEAD_INIT { .first = NULL }
414#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
415#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
416static inline void INIT_HLIST_NODE(struct hlist_node *h)
417{
418 h->next = NULL;
419 h->pprev = NULL;
420}
421
422#define READ_ONCE(x) x
423#define WRITE_ONCE(a, b) a = b
424
425/*! Has node been removed from list and reinitialized?.
426 * \param[in] h: Node to be checked
427 * \return 1 if node is unhashed; 0 if not
428 *
429 * Not that not all removal functions will leave a node in unhashed
430 * state. For example, hlist_nulls_del_init_rcu() does leave the
431 * node in unhashed state, but hlist_nulls_del() does not.
432 */
433static inline int hlist_unhashed(const struct hlist_node *h)
434{
435 return !h->pprev;
436}
437
438/*! Version of hlist_unhashed for lockless use.
439 * \param[in] n Node to be checked
440 * \return 1 if node is unhashed; 0 if not
441 *
442 * This variant of hlist_unhashed() must be used in lockless contexts
443 * to avoid potential load-tearing. The READ_ONCE() is paired with the
444 * various WRITE_ONCE() in hlist helpers that are defined below.
445 */
446static inline int hlist_unhashed_lockless(const struct hlist_node *h)
447{
448 return !READ_ONCE(h->pprev);
449}
450
451/*!Is the specified hlist_head structure an empty hlist?.
452 * \param[in] h Structure to check.
453 * \return 1 if hlist is empty; 0 if not
454 */
455static inline int hlist_empty(const struct hlist_head *h)
456{
457 return !READ_ONCE(h->first);
458}
459
460static inline void __hlist_del(struct hlist_node *n)
461{
462 struct hlist_node *next = n->next;
463 struct hlist_node **pprev = n->pprev;
464
465 WRITE_ONCE(*pprev, next);
466 if (next)
467 WRITE_ONCE(next->pprev, pprev);
468}
469
470/*! Delete the specified hlist_node from its list.
471 * \param[in] n: Node to delete.
472 *
473 * Note that this function leaves the node in hashed state. Use
474 * hlist_del_init() or similar instead to unhash @n.
475 */
476static inline void hlist_del(struct hlist_node *n)
477{
478 __hlist_del(n);
479 n->next = LLIST_POISON1;
480 n->pprev = LLIST_POISON2;
481}
482
483/*! Delete the specified hlist_node from its list and initialize.
484 * \param[in] n Node to delete.
485 *
486 * Note that this function leaves the node in unhashed state.
487 */
488static inline void hlist_del_init(struct hlist_node *n)
489{
490 if (!hlist_unhashed(n)) {
491 __hlist_del(n);
492 INIT_HLIST_NODE(n);
493 }
494}
495
496/*! add a new entry at the beginning of the hlist.
497 * \param[in] n new entry to be added
498 * \param[in] h hlist head to add it after
499 *
500 * Insert a new entry after the specified head.
501 * This is good for implementing stacks.
502 */
503static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
504{
505 struct hlist_node *first = h->first;
506 WRITE_ONCE(n->next, first);
507 if (first)
508 WRITE_ONCE(first->pprev, &n->next);
509 WRITE_ONCE(h->first, n);
510 WRITE_ONCE(n->pprev, &h->first);
511}
512
513/*! add a new entry before the one specified.
514 * @n: new entry to be added
515 * @next: hlist node to add it before, which must be non-NULL
516 */
517static inline void hlist_add_before(struct hlist_node *n,
518 struct hlist_node *next)
519{
520 WRITE_ONCE(n->pprev, next->pprev);
521 WRITE_ONCE(n->next, next);
522 WRITE_ONCE(next->pprev, &n->next);
523 WRITE_ONCE(*(n->pprev), n);
524}
525
526/*! add a new entry after the one specified
527 * \param[in] n new entry to be added
528 * \param[in] prev hlist node to add it after, which must be non-NULL
529 */
530static inline void hlist_add_behind(struct hlist_node *n,
531 struct hlist_node *prev)
532{
533 WRITE_ONCE(n->next, prev->next);
534 WRITE_ONCE(prev->next, n);
535 WRITE_ONCE(n->pprev, &prev->next);
536
537 if (n->next)
538 WRITE_ONCE(n->next->pprev, &n->next);
539}
540
541/*! create a fake hlist consisting of a single headless node.
542 * \param[in] n Node to make a fake list out of
543 *
544 * This makes @n appear to be its own predecessor on a headless hlist.
545 * The point of this is to allow things like hlist_del() to work correctly
546 * in cases where there is no list.
547 */
548static inline void hlist_add_fake(struct hlist_node *n)
549{
550 n->pprev = &n->next;
551}
552
553/*! Is this node a fake hlist?.
554 * \param[in] h Node to check for being a self-referential fake hlist.
555 */
556static inline bool hlist_fake(struct hlist_node *h)
557{
558 return h->pprev == &h->next;
559}
560
561/*!is node the only element of the specified hlist?.
562 * \param[in] n Node to check for singularity.
563 * \param[in] h Header for potentially singular list.
564 *
565 * Check whether the node is the only node of the head without
566 * accessing head, thus avoiding unnecessary cache misses.
567 */
568static inline bool
569hlist_is_singular_node(struct hlist_node *n, struct hlist_head *h)
570{
571 return !n->next && n->pprev == &h->first;
572}
573
574/*! Move an hlist.
575 * \param[in] old hlist_head for old list.
576 * \param[in] new hlist_head for new list.
577 *
578 * Move a list from one list head to another. Fixup the pprev
579 * reference of the first entry if it exists.
580 */
581static inline void hlist_move_list(struct hlist_head *old,
582 struct hlist_head *new)
583{
584 new->first = old->first;
585 if (new->first)
586 new->first->pprev = &new->first;
587 old->first = NULL;
588}
589
590#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
591
592#define hlist_for_each(pos, head) \
593 for (pos = (head)->first; pos ; pos = pos->next)
594
595#define hlist_for_each_safe(pos, n, head) \
596 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
597 pos = n)
598
599#define hlist_entry_safe(ptr, type, member) \
600 ({ typeof(ptr) ____ptr = (ptr); \
601 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
602 })
603
604/*! iterate over list of given type.
605 * \param[out] pos the type * to use as a loop cursor.
606 * \param[in] head the head for your list.
607 * \param[in] member the name of the hlist_node within the struct.
608 */
609#define hlist_for_each_entry(pos, head, member) \
610 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
611 pos; \
612 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
613
614/*! iterate over a hlist continuing after current point.
615 * \param[out] pos the type * to use as a loop cursor.
616 * \param[in] member the name of the hlist_node within the struct.
617 */
618#define hlist_for_each_entry_continue(pos, member) \
619 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
620 pos; \
621 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
622
623/*! iterate over a hlist continuing from current point.
624 * \param[out] pos the type * to use as a loop cursor.
625 * \param[in] member the name of the hlist_node within the struct.
626 */
627#define hlist_for_each_entry_from(pos, member) \
628 for (; pos; \
629 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
630
631/*! hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry.
632 * \param[out] pos the type * to use as a loop cursor.
633 * \param[out] n a &struct hlist_node to use as temporary storage
634 * \param[in] head the head for your list.
635 * \param[in] member the name of the hlist_node within the struct
636 */
637#define hlist_for_each_entry_safe(pos, n, head, member) \
638 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
639 pos && ({ n = pos->member.next; 1; }); \
640 pos = hlist_entry_safe(n, typeof(*pos), member))
641
642
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200643/*!
Neels Hofmeyrb525b9e2017-10-16 16:46:43 +0200644 * @}
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200645 */