blob: 7d8507768e0aad24d746beffb8eef46e0341eeec [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +01002
Harald Welte2d2e2cc2016-04-25 12:11:20 +02003/*! \defgroup linuxlist Simple doubly linked list implementation
4 * @{
Harald Welteec8b4502010-02-20 20:34:29 +01005 */
Harald Welteec8b4502010-02-20 20:34:29 +01006
Harald Welte2d2e2cc2016-04-25 12:11:20 +02007/*!
8 * \file linuxlist.h
9 *
10 * \brief Simple doubly linked list implementation.
Harald Welteec8b4502010-02-20 20:34:29 +010011 *
12 * Some of the internal functions ("__xxx") are useful when
13 * manipulating whole llists rather than single entries, as
14 * sometimes we already know the next/prev entries and we can
15 * generate better code by using them directly rather than
16 * using the generic single-entry routines.
17 */
18
Harald Welte2d2e2cc2016-04-25 12:11:20 +020019#include <stddef.h>
20
21#ifndef inline
22#define inline __inline__
23#endif
24
25static inline void prefetch(const void *x) {;}
26
27/*! \brief cast a member of a structure out to the containing structure
28 *
29 * \param[in] ptr the pointer to the member.
30 * \param[in] type the type of the container struct this is embedded in.
31 * \param[in] member the name of the member within the struct.
32 */
33#define container_of(ptr, type, member) ({ \
34 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
35 (type *)( (char *)__mptr - offsetof(type, member) );})
36
37
38/*!
39 * These are non-NULL pointers that will result in page faults
40 * under normal circumstances, used to verify that nobody uses
41 * non-initialized llist entries.
42 */
43#define LLIST_POISON1 ((void *) 0x00100100)
44#define LLIST_POISON2 ((void *) 0x00200200)
45
46/*! \brief (double) linked list header structure */
Harald Welteec8b4502010-02-20 20:34:29 +010047struct llist_head {
Harald Welte2d2e2cc2016-04-25 12:11:20 +020048 /*! \brief Pointer to next and previous item */
Harald Welteec8b4502010-02-20 20:34:29 +010049 struct llist_head *next, *prev;
50};
51
52#define LLIST_HEAD_INIT(name) { &(name), &(name) }
53
Harald Welte2d2e2cc2016-04-25 12:11:20 +020054/*! \brief define a statically-initialized \ref llist_head
55 * \param[in] name Variable name
56 *
57 * This is a helper macro that will define a named variable of type
58 * \ref llist_head and initialize it */
Harald Welteec8b4502010-02-20 20:34:29 +010059#define LLIST_HEAD(name) \
60 struct llist_head name = LLIST_HEAD_INIT(name)
61
Harald Welte2d2e2cc2016-04-25 12:11:20 +020062/*! \brief initialize a \ref llist_head to point back to self */
Harald Welteec8b4502010-02-20 20:34:29 +010063#define INIT_LLIST_HEAD(ptr) do { \
64 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
65} while (0)
66
Harald Welte2d2e2cc2016-04-25 12:11:20 +020067/*! \brief Insert a new entry between two known consecutive entries.
Harald Welteec8b4502010-02-20 20:34:29 +010068 *
69 * This is only for internal llist manipulation where we know
70 * the prev/next entries already!
71 */
72static inline void __llist_add(struct llist_head *_new,
73 struct llist_head *prev,
74 struct llist_head *next)
75{
76 next->prev = _new;
77 _new->next = next;
78 _new->prev = prev;
79 prev->next = _new;
80}
81
Harald Welte2d2e2cc2016-04-25 12:11:20 +020082/*! \brief add a new entry into a linked list (at head)
83 * \param _new New entry to be added
84 * \param head \ref llist_head to add it after
Harald Welteec8b4502010-02-20 20:34:29 +010085 *
86 * Insert a new entry after the specified head.
87 * This is good for implementing stacks.
88 */
89static inline void llist_add(struct llist_head *_new, struct llist_head *head)
90{
91 __llist_add(_new, head, head->next);
92}
93
Harald Welte2d2e2cc2016-04-25 12:11:20 +020094/*! \brief add a new entry into a linked list (at tail)
95 * \param _new New entry to be added
96 * \param head Head of linked list to whose tail we shall add \a _new
Harald Welteec8b4502010-02-20 20:34:29 +010097 *
98 * Insert a new entry before the specified head.
99 * This is useful for implementing queues.
100 */
101static inline void llist_add_tail(struct llist_head *_new, struct llist_head *head)
102{
103 __llist_add(_new, head->prev, head);
104}
105
106/*
107 * Delete a llist entry by making the prev/next entries
108 * point to each other.
109 *
110 * This is only for internal llist manipulation where we know
111 * the prev/next entries already!
112 */
113static inline void __llist_del(struct llist_head * prev, struct llist_head * next)
114{
115 next->prev = prev;
116 prev->next = next;
117}
118
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200119/*! \brief Delete entry from linked list
120 * \param entry The element to delete from the llist
121 *
Harald Welteec8b4502010-02-20 20:34:29 +0100122 * Note: llist_empty on entry does not return true after this, the entry is
123 * in an undefined state.
124 */
125static inline void llist_del(struct llist_head *entry)
126{
127 __llist_del(entry->prev, entry->next);
128 entry->next = (struct llist_head *)LLIST_POISON1;
129 entry->prev = (struct llist_head *)LLIST_POISON2;
130}
131
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200132/*! \brief Delete entry from linked list and reinitialize it
133 * \param entry The element to delete from the list
Harald Welteec8b4502010-02-20 20:34:29 +0100134 */
135static inline void llist_del_init(struct llist_head *entry)
136{
137 __llist_del(entry->prev, entry->next);
138 INIT_LLIST_HEAD(entry);
139}
140
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200141/*! \brief Delete from one llist and add as another's head
142 * \param llist The entry to move
143 * \param head The head that will precede our entry
Harald Welteec8b4502010-02-20 20:34:29 +0100144 */
145static inline void llist_move(struct llist_head *llist, struct llist_head *head)
146{
147 __llist_del(llist->prev, llist->next);
148 llist_add(llist, head);
149}
150
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200151/*! \brief Delete from one llist and add as another's tail
152 * \param llist The entry to move
153 * \param head The head that will follow our entry
Harald Welteec8b4502010-02-20 20:34:29 +0100154 */
155static inline void llist_move_tail(struct llist_head *llist,
156 struct llist_head *head)
157{
158 __llist_del(llist->prev, llist->next);
159 llist_add_tail(llist, head);
160}
161
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200162/*! \brief Test whether a linked list is empty
163 * \param[in] head The llist to test.
164 * \returns 1 if the list is empty, 0 otherwise
Harald Welteec8b4502010-02-20 20:34:29 +0100165 */
166static inline int llist_empty(const struct llist_head *head)
167{
168 return head->next == head;
169}
170
171static inline void __llist_splice(struct llist_head *llist,
172 struct llist_head *head)
173{
174 struct llist_head *first = llist->next;
175 struct llist_head *last = llist->prev;
176 struct llist_head *at = head->next;
177
178 first->prev = head;
179 head->next = first;
180
181 last->next = at;
182 at->prev = last;
183}
184
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200185/*! \brief Join two llists
186 * \param llist The new linked list to add
187 * \param head The place to add \a llist in the other list
Harald Welteec8b4502010-02-20 20:34:29 +0100188 */
189static inline void llist_splice(struct llist_head *llist, struct llist_head *head)
190{
191 if (!llist_empty(llist))
192 __llist_splice(llist, head);
193}
194
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200195/*! \brief join two llists and reinitialise the emptied llist.
196 * \param llist The new linked list to add.
197 * \param head The place to add it in the first llist.
Harald Welteec8b4502010-02-20 20:34:29 +0100198 *
199 * The llist at @llist is reinitialised
200 */
201static inline void llist_splice_init(struct llist_head *llist,
202 struct llist_head *head)
203{
204 if (!llist_empty(llist)) {
205 __llist_splice(llist, head);
206 INIT_LLIST_HEAD(llist);
207 }
208}
209
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200210/*! \brief Get the struct containing this list entry
211 * \param ptr The \ref llist_head pointer
212 * \param type The type of the struct this is embedded in
213 * \param @member The name of the \ref llist_head within the struct
Harald Welteec8b4502010-02-20 20:34:29 +0100214 */
215#define llist_entry(ptr, type, member) \
216 container_of(ptr, type, member)
217
Neels Hofmeyr4cb0c8b2017-03-14 22:48:02 +0100218/*! \brief Get the first element from a list
219 * \param ptr the list head to take the element from.
220 * \param type the type of the struct this is embedded in.
221 * \param member the name of the list_head within the struct.
222 *
223 * Note, that list is expected to be not empty.
224 */
225#define llist_first_entry(ptr, type, member) \
226 llist_entry((ptr)->next, type, member)
227
228/*! \brief Get the last element from a list
229 * \param ptr the list head to take the element from.
230 * \param type the type of the struct this is embedded in.
231 * \param member the name of the llist_head within the struct.
232 *
233 * Note, that list is expected to be not empty.
234 */
235#define llist_last_entry(ptr, type, member) \
236 llist_entry((ptr)->prev, type, member)
237
238/*! \brief Get the first element from a list, or NULL
239 * \param ptr the list head to take the element from.
240 * \param type the type of the struct this is embedded in.
241 * \param member the name of the list_head within the struct.
242 *
243 * Note that if the list is empty, it returns NULL.
244 */
245#define llist_first_entry_or_null(ptr, type, member) \
246 (!llist_empty(ptr) ? llist_first_entry(ptr, type, member) : NULL)
247
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200248/*! \brief Iterate over a linked list
249 * \param pos The \ref llist_head to use as a loop counter
250 * \param head The head of the list over which to iterate
Harald Welteec8b4502010-02-20 20:34:29 +0100251 */
252#define llist_for_each(pos, head) \
253 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
254 pos = pos->next, prefetch(pos->next))
255
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200256/*! \brief Iterate over a llist (no prefetch)
257 * \param pos The \ref llist_head to use as a loop counter
258 * \param head The head of the list over which to iterate
Harald Welteec8b4502010-02-20 20:34:29 +0100259 *
260 * This variant differs from llist_for_each() in that it's the
261 * simplest possible llist iteration code, no prefetching is done.
262 * Use this for code that knows the llist to be very short (empty
263 * or 1 entry) most of the time.
264 */
265#define __llist_for_each(pos, head) \
266 for (pos = (head)->next; pos != (head); pos = pos->next)
267
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200268/*! \brief Iterate over a llist backwards
269 * \param pos The \ref llist_head to use as a loop counter
270 * \param head The head of the list over which to iterate
Harald Welteec8b4502010-02-20 20:34:29 +0100271 */
272#define llist_for_each_prev(pos, head) \
273 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
274 pos = pos->prev, prefetch(pos->prev))
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200275
276/*! \brief Iterate over a list; safe against removal of llist entry
277 * \param pos The \ref llist_head to use as a loop counter
278 * \param n Another \ref llist_head to use as temporary storage
279 * \param head The head of the list over which to iterate
Harald Welteec8b4502010-02-20 20:34:29 +0100280 */
281#define llist_for_each_safe(pos, n, head) \
282 for (pos = (head)->next, n = pos->next; pos != (head); \
283 pos = n, n = pos->next)
284
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200285/*! \brief Iterate over llist of given type
286 * \param pos The 'type *' to use as a loop counter
287 * \param head The head of the list over which to iterate
288 * \param member The name of the \ref llist_head within struct \a pos
Harald Welteec8b4502010-02-20 20:34:29 +0100289 */
290#define llist_for_each_entry(pos, head, member) \
291 for (pos = llist_entry((head)->next, typeof(*pos), member), \
292 prefetch(pos->member.next); \
293 &pos->member != (head); \
294 pos = llist_entry(pos->member.next, typeof(*pos), member), \
295 prefetch(pos->member.next))
296
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200297/*! \brief Iterate backwards over llist of given type.
298 * \param pos The 'type *' to use as a loop counter
299 * \param head The head of the list over which to iterate
300 * \param member The name of the \ref llist_head within struct \a pos
Harald Welteec8b4502010-02-20 20:34:29 +0100301 */
302#define llist_for_each_entry_reverse(pos, head, member) \
303 for (pos = llist_entry((head)->prev, typeof(*pos), member), \
304 prefetch(pos->member.prev); \
305 &pos->member != (head); \
306 pos = llist_entry(pos->member.prev, typeof(*pos), member), \
307 prefetch(pos->member.prev))
308
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200309/*! \brief iterate over llist of given type continuing after existing
310 * point
311 * \param pos The 'type *' to use as a loop counter
312 * \param head The head of the list over which to iterate
313 * \param member The name of the \ref llist_head within struct \a pos
Harald Welteec8b4502010-02-20 20:34:29 +0100314 */
315#define llist_for_each_entry_continue(pos, head, member) \
316 for (pos = llist_entry(pos->member.next, typeof(*pos), member), \
317 prefetch(pos->member.next); \
318 &pos->member != (head); \
319 pos = llist_entry(pos->member.next, typeof(*pos), member), \
320 prefetch(pos->member.next))
321
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200322/*! \brief iterate over llist of given type, safe against removal of
323 * non-consecutive(!) llist entries
324 * \param pos The 'type *' to use as a loop counter
325 * \param n Another type * to use as temporary storage
326 * \param head The head of the list over which to iterate
327 * \param member The name of the \ref llist_head within struct \a pos
Harald Welteec8b4502010-02-20 20:34:29 +0100328 */
329#define llist_for_each_entry_safe(pos, n, head, member) \
330 for (pos = llist_entry((head)->next, typeof(*pos), member), \
331 n = llist_entry(pos->member.next, typeof(*pos), member); \
332 &pos->member != (head); \
333 pos = n, n = llist_entry(n->member.next, typeof(*n), member))
334
335/**
336 * llist_for_each_rcu - iterate over an rcu-protected llist
337 * @pos: the &struct llist_head to use as a loop counter.
338 * @head: the head for your llist.
339 */
340#define llist_for_each_rcu(pos, head) \
341 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
342 pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
343
344#define __llist_for_each_rcu(pos, head) \
345 for (pos = (head)->next; pos != (head); \
346 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
347
348/**
349 * llist_for_each_safe_rcu - iterate over an rcu-protected llist safe
350 * against removal of llist entry
351 * @pos: the &struct llist_head to use as a loop counter.
352 * @n: another &struct llist_head to use as temporary storage
353 * @head: the head for your llist.
354 */
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
359/**
360 * llist_for_each_entry_rcu - iterate over rcu llist of given type
361 * @pos: the type * to use as a loop counter.
362 * @head: the head for your llist.
363 * @member: the name of the llist_struct within the struct.
364 */
365#define llist_for_each_entry_rcu(pos, head, member) \
366 for (pos = llist_entry((head)->next, typeof(*pos), member), \
367 prefetch(pos->member.next); \
368 &pos->member != (head); \
369 pos = llist_entry(pos->member.next, typeof(*pos), member), \
370 ({ smp_read_barrier_depends(); 0;}), \
371 prefetch(pos->member.next))
372
373
374/**
375 * llist_for_each_continue_rcu - iterate over an rcu-protected llist
376 * continuing after existing point.
377 * @pos: the &struct llist_head to use as a loop counter.
378 * @head: the head for your llist.
379 */
380#define llist_for_each_continue_rcu(pos, head) \
381 for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
382 (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200383
Neels Hofmeyr505a22f2017-01-11 00:33:10 +0100384/*! \brief count nr of llist items by iterating.
385 * \param head The llist head to count items of.
386 * \returns Number of items.
387 *
388 * This function is not efficient, mostly useful for small lists and non time
389 * critical cases like unit tests.
390 */
391static inline unsigned int llist_count(struct llist_head *head)
392{
393 struct llist_head *entry;
394 unsigned int i = 0;
395 llist_for_each(entry, head)
396 i++;
397 return i;
398}
399
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200400/*!
401 * }@
402 */