blob: affa8273094380ffb18dcbf9f23ebefd2cf6d26e [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
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200218/*! \brief Iterate over a linked list
219 * \param pos The \ref llist_head to use as a loop counter
220 * \param head The head of the list over which to iterate
Harald Welteec8b4502010-02-20 20:34:29 +0100221 */
222#define llist_for_each(pos, head) \
223 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
224 pos = pos->next, prefetch(pos->next))
225
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200226/*! \brief Iterate over a llist (no prefetch)
227 * \param pos The \ref llist_head to use as a loop counter
228 * \param head The head of the list over which to iterate
Harald Welteec8b4502010-02-20 20:34:29 +0100229 *
230 * This variant differs from llist_for_each() in that it's the
231 * simplest possible llist iteration code, no prefetching is done.
232 * Use this for code that knows the llist to be very short (empty
233 * or 1 entry) most of the time.
234 */
235#define __llist_for_each(pos, head) \
236 for (pos = (head)->next; pos != (head); pos = pos->next)
237
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200238/*! \brief Iterate over a llist backwards
239 * \param pos The \ref llist_head to use as a loop counter
240 * \param head The head of the list over which to iterate
Harald Welteec8b4502010-02-20 20:34:29 +0100241 */
242#define llist_for_each_prev(pos, head) \
243 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
244 pos = pos->prev, prefetch(pos->prev))
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200245
246/*! \brief Iterate over a list; safe against removal of llist entry
247 * \param pos The \ref llist_head to use as a loop counter
248 * \param n Another \ref llist_head to use as temporary storage
249 * \param head The head of the list over which to iterate
Harald Welteec8b4502010-02-20 20:34:29 +0100250 */
251#define llist_for_each_safe(pos, n, head) \
252 for (pos = (head)->next, n = pos->next; pos != (head); \
253 pos = n, n = pos->next)
254
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200255/*! \brief Iterate over llist of given type
256 * \param pos The 'type *' to use as a loop counter
257 * \param head The head of the list over which to iterate
258 * \param member The name of the \ref llist_head within struct \a pos
Harald Welteec8b4502010-02-20 20:34:29 +0100259 */
260#define llist_for_each_entry(pos, head, member) \
261 for (pos = llist_entry((head)->next, typeof(*pos), member), \
262 prefetch(pos->member.next); \
263 &pos->member != (head); \
264 pos = llist_entry(pos->member.next, typeof(*pos), member), \
265 prefetch(pos->member.next))
266
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200267/*! \brief Iterate backwards over llist of given type.
268 * \param pos The 'type *' to use as a loop counter
269 * \param head The head of the list over which to iterate
270 * \param member The name of the \ref llist_head within struct \a pos
Harald Welteec8b4502010-02-20 20:34:29 +0100271 */
272#define llist_for_each_entry_reverse(pos, head, member) \
273 for (pos = llist_entry((head)->prev, typeof(*pos), member), \
274 prefetch(pos->member.prev); \
275 &pos->member != (head); \
276 pos = llist_entry(pos->member.prev, typeof(*pos), member), \
277 prefetch(pos->member.prev))
278
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200279/*! \brief iterate over llist of given type continuing after existing
280 * point
281 * \param pos The 'type *' to use as a loop counter
282 * \param head The head of the list over which to iterate
283 * \param member The name of the \ref llist_head within struct \a pos
Harald Welteec8b4502010-02-20 20:34:29 +0100284 */
285#define llist_for_each_entry_continue(pos, head, member) \
286 for (pos = llist_entry(pos->member.next, typeof(*pos), member), \
287 prefetch(pos->member.next); \
288 &pos->member != (head); \
289 pos = llist_entry(pos->member.next, typeof(*pos), member), \
290 prefetch(pos->member.next))
291
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200292/*! \brief iterate over llist of given type, safe against removal of
293 * non-consecutive(!) llist entries
294 * \param pos The 'type *' to use as a loop counter
295 * \param n Another type * to use as temporary storage
296 * \param head The head of the list over which to iterate
297 * \param member The name of the \ref llist_head within struct \a pos
Harald Welteec8b4502010-02-20 20:34:29 +0100298 */
299#define llist_for_each_entry_safe(pos, n, head, member) \
300 for (pos = llist_entry((head)->next, typeof(*pos), member), \
301 n = llist_entry(pos->member.next, typeof(*pos), member); \
302 &pos->member != (head); \
303 pos = n, n = llist_entry(n->member.next, typeof(*n), member))
304
305/**
306 * llist_for_each_rcu - iterate over an rcu-protected llist
307 * @pos: the &struct llist_head to use as a loop counter.
308 * @head: the head for your llist.
309 */
310#define llist_for_each_rcu(pos, head) \
311 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
312 pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
313
314#define __llist_for_each_rcu(pos, head) \
315 for (pos = (head)->next; pos != (head); \
316 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
317
318/**
319 * llist_for_each_safe_rcu - iterate over an rcu-protected llist safe
320 * against removal of llist entry
321 * @pos: the &struct llist_head to use as a loop counter.
322 * @n: another &struct llist_head to use as temporary storage
323 * @head: the head for your llist.
324 */
325#define llist_for_each_safe_rcu(pos, n, head) \
326 for (pos = (head)->next, n = pos->next; pos != (head); \
327 pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
328
329/**
330 * llist_for_each_entry_rcu - iterate over rcu llist of given type
331 * @pos: the type * to use as a loop counter.
332 * @head: the head for your llist.
333 * @member: the name of the llist_struct within the struct.
334 */
335#define llist_for_each_entry_rcu(pos, head, member) \
336 for (pos = llist_entry((head)->next, typeof(*pos), member), \
337 prefetch(pos->member.next); \
338 &pos->member != (head); \
339 pos = llist_entry(pos->member.next, typeof(*pos), member), \
340 ({ smp_read_barrier_depends(); 0;}), \
341 prefetch(pos->member.next))
342
343
344/**
345 * llist_for_each_continue_rcu - iterate over an rcu-protected llist
346 * continuing after existing point.
347 * @pos: the &struct llist_head to use as a loop counter.
348 * @head: the head for your llist.
349 */
350#define llist_for_each_continue_rcu(pos, head) \
351 for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
352 (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200353
Neels Hofmeyr505a22f2017-01-11 00:33:10 +0100354/*! \brief count nr of llist items by iterating.
355 * \param head The llist head to count items of.
356 * \returns Number of items.
357 *
358 * This function is not efficient, mostly useful for small lists and non time
359 * critical cases like unit tests.
360 */
361static inline unsigned int llist_count(struct llist_head *head)
362{
363 struct llist_head *entry;
364 unsigned int i = 0;
365 llist_for_each(entry, head)
366 i++;
367 return i;
368}
369
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200370/*!
371 * }@
372 */