blob: 867605e58c94f8376377550cc3639676d819f2bc [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>
19
20#ifndef inline
21#define inline __inline__
22#endif
23
24static inline void prefetch(const void *x) {;}
25
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +070026/*! 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 Welte2d2e2cc2016-04-25 12:11:20 +020030 */
31#define container_of(ptr, type, member) ({ \
Vadim Yanitskiy095a3962019-03-25 21:13:03 +070032 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
33 (type *)( (char *)__mptr - offsetof(type, member) );})
Harald Welte2d2e2cc2016-04-25 12:11:20 +020034
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 Hofmeyr87e45502017-06-20 00:17:59 +020044/*! (double) linked list header structure */
Harald Welteec8b4502010-02-20 20:34:29 +010045struct llist_head {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020046 /*! Pointer to next and previous item */
Harald Welteec8b4502010-02-20 20:34:29 +010047 struct llist_head *next, *prev;
48};
49
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +070050/*! Define a new llist_head pointing to a given llist_head.
51 * \param[in] name another llist_head to be pointed.
52 */
Harald Welteec8b4502010-02-20 20:34:29 +010053#define LLIST_HEAD_INIT(name) { &(name), &(name) }
54
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +070055/*! Define a statically-initialized variable of type llist_head.
56 * \param[in] name variable (symbol) name.
57 */
Harald Welteec8b4502010-02-20 20:34:29 +010058#define LLIST_HEAD(name) \
59 struct llist_head name = LLIST_HEAD_INIT(name)
60
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +070061/*! Initialize a llist_head to point back to itself.
62 * \param[in] ptr llist_head to be initialized.
63 */
Harald Welteec8b4502010-02-20 20:34:29 +010064#define INIT_LLIST_HEAD(ptr) do { \
65 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
66} while (0)
67
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +070068/*
69 * Insert a new entry between two known consecutive entries.
Harald Welteec8b4502010-02-20 20:34:29 +010070 *
71 * This is only for internal llist manipulation where we know
72 * the prev/next entries already!
73 */
74static inline void __llist_add(struct llist_head *_new,
Vadim Yanitskiy095a3962019-03-25 21:13:03 +070075 struct llist_head *prev,
76 struct llist_head *next)
Harald Welteec8b4502010-02-20 20:34:29 +010077{
78 next->prev = _new;
79 _new->next = next;
80 _new->prev = prev;
81 prev->next = _new;
82}
83
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +070084/*! 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 Welteec8b4502010-02-20 20:34:29 +010087 *
88 * Insert a new entry after the specified head.
89 * This is good for implementing stacks.
90 */
91static inline void llist_add(struct llist_head *_new, struct llist_head *head)
92{
93 __llist_add(_new, head, head->next);
94}
95
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +070096/*! 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 Welteec8b4502010-02-20 20:34:29 +010099 *
100 * Insert a new entry before the specified head.
101 * This is useful for implementing queues.
102 */
103static 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 */
115static inline void __llist_del(struct llist_head * prev, struct llist_head * next)
116{
117 next->prev = prev;
118 prev->next = next;
119}
120
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700121/*! Delete a single entry from a linked list.
122 * \param entry the element to delete.
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200123 *
Harald Welteec8b4502010-02-20 20:34:29 +0100124 * Note: llist_empty on entry does not return true after this, the entry is
125 * in an undefined state.
126 */
127static 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 Yanitskiy72ea9192019-03-25 20:58:16 +0700134/*! Delete a single entry from a linked list and reinitialize it.
135 * \param entry the element to delete and reinitialize.
Harald Welteec8b4502010-02-20 20:34:29 +0100136 */
137static inline void llist_del_init(struct llist_head *entry)
138{
139 __llist_del(entry->prev, entry->next);
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700140 INIT_LLIST_HEAD(entry);
Harald Welteec8b4502010-02-20 20:34:29 +0100141}
142
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700143/*! 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 Welteec8b4502010-02-20 20:34:29 +0100146 */
147static inline void llist_move(struct llist_head *llist, struct llist_head *head)
148{
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700149 __llist_del(llist->prev, llist->next);
150 llist_add(llist, head);
Harald Welteec8b4502010-02-20 20:34:29 +0100151}
152
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700153/*! 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 Welteec8b4502010-02-20 20:34:29 +0100156 */
157static inline void llist_move_tail(struct llist_head *llist,
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700158 struct llist_head *head)
Harald Welteec8b4502010-02-20 20:34:29 +0100159{
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700160 __llist_del(llist->prev, llist->next);
161 llist_add_tail(llist, head);
Harald Welteec8b4502010-02-20 20:34:29 +0100162}
163
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700164/*! 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 Welteec8b4502010-02-20 20:34:29 +0100167 */
168static inline int llist_empty(const struct llist_head *head)
169{
170 return head->next == head;
171}
172
173static inline void __llist_splice(struct llist_head *llist,
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700174 struct llist_head *head)
Harald Welteec8b4502010-02-20 20:34:29 +0100175{
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 Yanitskiy72ea9192019-03-25 20:58:16 +0700187/*! 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 Welteec8b4502010-02-20 20:34:29 +0100190 */
191static 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 Yanitskiy72ea9192019-03-25 20:58:16 +0700197/*! 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 Welteec8b4502010-02-20 20:34:29 +0100200 *
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700201 * The llist is reinitialised.
Harald Welteec8b4502010-02-20 20:34:29 +0100202 */
203static inline void llist_splice_init(struct llist_head *llist,
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700204 struct llist_head *head)
Harald Welteec8b4502010-02-20 20:34:29 +0100205{
206 if (!llist_empty(llist)) {
207 __llist_splice(llist, head);
208 INIT_LLIST_HEAD(llist);
209 }
210}
211
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700212/*! 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 Welteec8b4502010-02-20 20:34:29 +0100216 */
217#define llist_entry(ptr, type, member) \
218 container_of(ptr, type, member)
219
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700220/*! Get the first element from a linked list.
Neels Hofmeyr4cb0c8b2017-03-14 22:48:02 +0100221 * \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 Yanitskiy72ea9192019-03-25 20:58:16 +0700230/*! Get the last element from a list.
Neels Hofmeyr4cb0c8b2017-03-14 22:48:02 +0100231 * \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 Yanitskiy72ea9192019-03-25 20:58:16 +0700240/*! Get the first element from a list, or NULL.
Neels Hofmeyr4cb0c8b2017-03-14 22:48:02 +0100241 * \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 Yanitskiy72ea9192019-03-25 20:58:16 +0700250/*! 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 Welteec8b4502010-02-20 20:34:29 +0100253 */
254#define llist_for_each(pos, head) \
255 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700256 pos = pos->next, prefetch(pos->next))
Harald Welteec8b4502010-02-20 20:34:29 +0100257
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700258/*! 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 Welteec8b4502010-02-20 20:34:29 +0100261 *
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 Yanitskiy72ea9192019-03-25 20:58:16 +0700270/*! 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 Welteec8b4502010-02-20 20:34:29 +0100273 */
274#define llist_for_each_prev(pos, head) \
275 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700276 pos = pos->prev, prefetch(pos->prev))
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200277
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700278/*! 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 Welteec8b4502010-02-20 20:34:29 +0100282 */
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 Yanitskiy72ea9192019-03-25 20:58:16 +0700287/*! 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 Welteec8b4502010-02-20 20:34:29 +0100291 */
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 Yanitskiy72ea9192019-03-25 20:58:16 +0700299/*! 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 Welteec8b4502010-02-20 20:34:29 +0100303 */
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 Yanitskiy72ea9192019-03-25 20:58:16 +0700311/*! 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 Welteec8b4502010-02-20 20:34:29 +0100316 */
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 Yanitskiy72ea9192019-03-25 20:58:16 +0700324/*! 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 Welteec8b4502010-02-20 20:34:29 +0100330 */
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 Yanitskiy72ea9192019-03-25 20:58:16 +0700337/*! 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 Welteec8b4502010-02-20 20:34:29 +0100340 */
341#define llist_for_each_rcu(pos, head) \
342 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700343 pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
344
Harald Welteec8b4502010-02-20 20:34:29 +0100345#define __llist_for_each_rcu(pos, head) \
346 for (pos = (head)->next; pos != (head); \
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700347 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
348
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700349/*! 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 Welteec8b4502010-02-20 20:34:29 +0100353 */
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 Yanitskiy72ea9192019-03-25 20:58:16 +0700358/*! 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 Welteec8b4502010-02-20 20:34:29 +0100362 */
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 Yanitskiy72ea9192019-03-25 20:58:16 +0700372/*! 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 Welteec8b4502010-02-20 20:34:29 +0100375 */
376#define llist_for_each_continue_rcu(pos, head) \
377 for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
Vadim Yanitskiy095a3962019-03-25 21:13:03 +0700378 (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200379
Vadim Yanitskiy72ea9192019-03-25 20:58:16 +0700380/*! Count number of llist items by iterating.
381 * \param head the llist head to count items of.
Neels Hofmeyr505a22f2017-01-11 00:33:10 +0100382 * \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 */
Maxc01cff12018-12-10 12:46:03 +0100387static inline unsigned int llist_count(const struct llist_head *head)
Neels Hofmeyr505a22f2017-01-11 00:33:10 +0100388{
389 struct llist_head *entry;
390 unsigned int i = 0;
391 llist_for_each(entry, head)
392 i++;
393 return i;
394}
395
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200396/*!
Neels Hofmeyrb525b9e2017-10-16 16:46:43 +0200397 * @}
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200398 */