blob: 8d0533f64dd292bf7d2d7d4d50806415e05dc907 [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
Neels Hofmeyr87e45502017-06-20 00:17:59 +020026/*! cast a member of a structure out to the containing structure
Harald Welte2d2e2cc2016-04-25 12:11:20 +020027 *
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.
31 */
32#define container_of(ptr, type, member) ({ \
33 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
34 (type *)( (char *)__mptr - offsetof(type, member) );})
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 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
51#define LLIST_HEAD_INIT(name) { &(name), &(name) }
52
Neels Hofmeyr87e45502017-06-20 00:17:59 +020053/*! define a statically-initialized \ref llist_head
Harald Welte2d2e2cc2016-04-25 12:11:20 +020054 * \param[in] name Variable name
55 *
56 * This is a helper macro that will define a named variable of type
57 * \ref llist_head and initialize it */
Harald Welteec8b4502010-02-20 20:34:29 +010058#define LLIST_HEAD(name) \
59 struct llist_head name = LLIST_HEAD_INIT(name)
60
Neels Hofmeyr87e45502017-06-20 00:17:59 +020061/*! initialize a \ref llist_head to point back to self */
Harald Welteec8b4502010-02-20 20:34:29 +010062#define INIT_LLIST_HEAD(ptr) do { \
63 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
64} while (0)
65
Neels Hofmeyr87e45502017-06-20 00:17:59 +020066/*! Insert a new entry between two known consecutive entries.
Harald Welteec8b4502010-02-20 20:34:29 +010067 *
68 * This is only for internal llist manipulation where we know
69 * the prev/next entries already!
70 */
71static inline void __llist_add(struct llist_head *_new,
72 struct llist_head *prev,
73 struct llist_head *next)
74{
75 next->prev = _new;
76 _new->next = next;
77 _new->prev = prev;
78 prev->next = _new;
79}
80
Neels Hofmeyr87e45502017-06-20 00:17:59 +020081/*! add a new entry into a linked list (at head)
Harald Welte2d2e2cc2016-04-25 12:11:20 +020082 * \param _new New entry to be added
83 * \param head \ref llist_head to add it after
Harald Welteec8b4502010-02-20 20:34:29 +010084 *
85 * Insert a new entry after the specified head.
86 * This is good for implementing stacks.
87 */
88static inline void llist_add(struct llist_head *_new, struct llist_head *head)
89{
90 __llist_add(_new, head, head->next);
91}
92
Neels Hofmeyr87e45502017-06-20 00:17:59 +020093/*! add a new entry into a linked list (at tail)
Harald Welte2d2e2cc2016-04-25 12:11:20 +020094 * \param _new New entry to be added
95 * \param head Head of linked list to whose tail we shall add \a _new
Harald Welteec8b4502010-02-20 20:34:29 +010096 *
97 * Insert a new entry before the specified head.
98 * This is useful for implementing queues.
99 */
100static inline void llist_add_tail(struct llist_head *_new, struct llist_head *head)
101{
102 __llist_add(_new, head->prev, head);
103}
104
105/*
106 * Delete a llist entry by making the prev/next entries
107 * point to each other.
108 *
109 * This is only for internal llist manipulation where we know
110 * the prev/next entries already!
111 */
112static inline void __llist_del(struct llist_head * prev, struct llist_head * next)
113{
114 next->prev = prev;
115 prev->next = next;
116}
117
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200118/*! Delete entry from linked list
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200119 * \param entry The element to delete from the llist
120 *
Harald Welteec8b4502010-02-20 20:34:29 +0100121 * Note: llist_empty on entry does not return true after this, the entry is
122 * in an undefined state.
123 */
124static inline void llist_del(struct llist_head *entry)
125{
126 __llist_del(entry->prev, entry->next);
127 entry->next = (struct llist_head *)LLIST_POISON1;
128 entry->prev = (struct llist_head *)LLIST_POISON2;
129}
130
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200131/*! Delete entry from linked list and reinitialize it
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200132 * \param entry The element to delete from the list
Harald Welteec8b4502010-02-20 20:34:29 +0100133 */
134static inline void llist_del_init(struct llist_head *entry)
135{
136 __llist_del(entry->prev, entry->next);
137 INIT_LLIST_HEAD(entry);
138}
139
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200140/*! Delete from one llist and add as another's head
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200141 * \param llist The entry to move
142 * \param head The head that will precede our entry
Harald Welteec8b4502010-02-20 20:34:29 +0100143 */
144static inline void llist_move(struct llist_head *llist, struct llist_head *head)
145{
146 __llist_del(llist->prev, llist->next);
147 llist_add(llist, head);
148}
149
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200150/*! Delete from one llist and add as another's tail
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200151 * \param llist The entry to move
152 * \param head The head that will follow our entry
Harald Welteec8b4502010-02-20 20:34:29 +0100153 */
154static inline void llist_move_tail(struct llist_head *llist,
155 struct llist_head *head)
156{
157 __llist_del(llist->prev, llist->next);
158 llist_add_tail(llist, head);
159}
160
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200161/*! Test whether a linked list is empty
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200162 * \param[in] head The llist to test.
163 * \returns 1 if the list is empty, 0 otherwise
Harald Welteec8b4502010-02-20 20:34:29 +0100164 */
165static inline int llist_empty(const struct llist_head *head)
166{
167 return head->next == head;
168}
169
170static inline void __llist_splice(struct llist_head *llist,
171 struct llist_head *head)
172{
173 struct llist_head *first = llist->next;
174 struct llist_head *last = llist->prev;
175 struct llist_head *at = head->next;
176
177 first->prev = head;
178 head->next = first;
179
180 last->next = at;
181 at->prev = last;
182}
183
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200184/*! Join two llists
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200185 * \param llist The new linked list to add
186 * \param head The place to add \a llist in the other list
Harald Welteec8b4502010-02-20 20:34:29 +0100187 */
188static inline void llist_splice(struct llist_head *llist, struct llist_head *head)
189{
190 if (!llist_empty(llist))
191 __llist_splice(llist, head);
192}
193
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200194/*! join two llists and reinitialise the emptied llist.
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200195 * \param llist The new linked list to add.
196 * \param head The place to add it in the first llist.
Harald Welteec8b4502010-02-20 20:34:29 +0100197 *
198 * The llist at @llist is reinitialised
199 */
200static inline void llist_splice_init(struct llist_head *llist,
201 struct llist_head *head)
202{
203 if (!llist_empty(llist)) {
204 __llist_splice(llist, head);
205 INIT_LLIST_HEAD(llist);
206 }
207}
208
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200209/*! Get the struct containing this list entry
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200210 * \param ptr The \ref llist_head pointer
211 * \param type The type of the struct this is embedded in
212 * \param @member The name of the \ref llist_head within the struct
Harald Welteec8b4502010-02-20 20:34:29 +0100213 */
214#define llist_entry(ptr, type, member) \
215 container_of(ptr, type, member)
216
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200217/*! Get the first element from a list
Neels Hofmeyr4cb0c8b2017-03-14 22:48:02 +0100218 * \param ptr the list head to take the element from.
219 * \param type the type of the struct this is embedded in.
220 * \param member the name of the list_head within the struct.
221 *
222 * Note, that list is expected to be not empty.
223 */
224#define llist_first_entry(ptr, type, member) \
225 llist_entry((ptr)->next, type, member)
226
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200227/*! Get the last element from a list
Neels Hofmeyr4cb0c8b2017-03-14 22:48:02 +0100228 * \param ptr the list head to take the element from.
229 * \param type the type of the struct this is embedded in.
230 * \param member the name of the llist_head within the struct.
231 *
232 * Note, that list is expected to be not empty.
233 */
234#define llist_last_entry(ptr, type, member) \
235 llist_entry((ptr)->prev, type, member)
236
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200237/*! Get the first element from a list, or NULL
Neels Hofmeyr4cb0c8b2017-03-14 22:48:02 +0100238 * \param ptr the list head to take the element from.
239 * \param type the type of the struct this is embedded in.
240 * \param member the name of the list_head within the struct.
241 *
242 * Note that if the list is empty, it returns NULL.
243 */
244#define llist_first_entry_or_null(ptr, type, member) \
245 (!llist_empty(ptr) ? llist_first_entry(ptr, type, member) : NULL)
246
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200247/*! Iterate over a linked list
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200248 * \param pos The \ref llist_head to use as a loop counter
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(pos, head) \
252 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
253 pos = pos->next, prefetch(pos->next))
254
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200255/*! Iterate over a llist (no prefetch)
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200256 * \param pos The \ref llist_head to use as a loop counter
257 * \param head The head of the list over which to iterate
Harald Welteec8b4502010-02-20 20:34:29 +0100258 *
259 * This variant differs from llist_for_each() in that it's the
260 * simplest possible llist iteration code, no prefetching is done.
261 * Use this for code that knows the llist to be very short (empty
262 * or 1 entry) most of the time.
263 */
264#define __llist_for_each(pos, head) \
265 for (pos = (head)->next; pos != (head); pos = pos->next)
266
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200267/*! Iterate over a llist backwards
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200268 * \param pos The \ref llist_head to use as a loop counter
269 * \param head The head of the list over which to iterate
Harald Welteec8b4502010-02-20 20:34:29 +0100270 */
271#define llist_for_each_prev(pos, head) \
272 for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
273 pos = pos->prev, prefetch(pos->prev))
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200274
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200275/*! Iterate over a list; safe against removal of llist entry
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200276 * \param pos The \ref llist_head to use as a loop counter
277 * \param n Another \ref llist_head to use as temporary storage
278 * \param head The head of the list over which to iterate
Harald Welteec8b4502010-02-20 20:34:29 +0100279 */
280#define llist_for_each_safe(pos, n, head) \
281 for (pos = (head)->next, n = pos->next; pos != (head); \
282 pos = n, n = pos->next)
283
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200284/*! Iterate over llist of given type
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200285 * \param pos The 'type *' to use as a loop counter
286 * \param head The head of the list over which to iterate
287 * \param member The name of the \ref llist_head within struct \a pos
Harald Welteec8b4502010-02-20 20:34:29 +0100288 */
289#define llist_for_each_entry(pos, head, member) \
290 for (pos = llist_entry((head)->next, typeof(*pos), member), \
291 prefetch(pos->member.next); \
292 &pos->member != (head); \
293 pos = llist_entry(pos->member.next, typeof(*pos), member), \
294 prefetch(pos->member.next))
295
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200296/*! Iterate backwards over llist of given type.
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200297 * \param pos The 'type *' to use as a loop counter
298 * \param head The head of the list over which to iterate
299 * \param member The name of the \ref llist_head within struct \a pos
Harald Welteec8b4502010-02-20 20:34:29 +0100300 */
301#define llist_for_each_entry_reverse(pos, head, member) \
302 for (pos = llist_entry((head)->prev, typeof(*pos), member), \
303 prefetch(pos->member.prev); \
304 &pos->member != (head); \
305 pos = llist_entry(pos->member.prev, typeof(*pos), member), \
306 prefetch(pos->member.prev))
307
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200308/*! iterate over llist of given type continuing after existing
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200309 * point
310 * \param pos The 'type *' to use as a loop counter
311 * \param head The head of the list over which to iterate
312 * \param member The name of the \ref llist_head within struct \a pos
Harald Welteec8b4502010-02-20 20:34:29 +0100313 */
314#define llist_for_each_entry_continue(pos, head, member) \
315 for (pos = llist_entry(pos->member.next, typeof(*pos), member), \
316 prefetch(pos->member.next); \
317 &pos->member != (head); \
318 pos = llist_entry(pos->member.next, typeof(*pos), member), \
319 prefetch(pos->member.next))
320
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200321/*! iterate over llist of given type, safe against removal of
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200322 * non-consecutive(!) llist entries
323 * \param pos The 'type *' to use as a loop counter
324 * \param n Another type * to use as temporary storage
325 * \param head The head of the list over which to iterate
326 * \param member The name of the \ref llist_head within struct \a pos
Harald Welteec8b4502010-02-20 20:34:29 +0100327 */
328#define llist_for_each_entry_safe(pos, n, head, member) \
329 for (pos = llist_entry((head)->next, typeof(*pos), member), \
330 n = llist_entry(pos->member.next, typeof(*pos), member); \
331 &pos->member != (head); \
332 pos = n, n = llist_entry(n->member.next, typeof(*n), member))
333
334/**
335 * llist_for_each_rcu - iterate over an rcu-protected llist
336 * @pos: the &struct llist_head to use as a loop counter.
337 * @head: the head for your llist.
338 */
339#define llist_for_each_rcu(pos, head) \
340 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
341 pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
342
343#define __llist_for_each_rcu(pos, head) \
344 for (pos = (head)->next; pos != (head); \
345 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
346
347/**
348 * llist_for_each_safe_rcu - iterate over an rcu-protected llist safe
349 * against removal of llist entry
350 * @pos: the &struct llist_head to use as a loop counter.
351 * @n: another &struct llist_head to use as temporary storage
352 * @head: the head for your llist.
353 */
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
358/**
359 * llist_for_each_entry_rcu - iterate over rcu llist of given type
360 * @pos: the type * to use as a loop counter.
361 * @head: the head for your llist.
362 * @member: the name of the llist_struct within the struct.
363 */
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
373/**
374 * llist_for_each_continue_rcu - iterate over an rcu-protected llist
375 * continuing after existing point.
376 * @pos: the &struct llist_head to use as a loop counter.
377 * @head: the head for your llist.
378 */
379#define llist_for_each_continue_rcu(pos, head) \
380 for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
381 (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200382
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200383/*! count nr of llist items by iterating.
Neels Hofmeyr505a22f2017-01-11 00:33:10 +0100384 * \param head The llist head to count items of.
385 * \returns Number of items.
386 *
387 * This function is not efficient, mostly useful for small lists and non time
388 * critical cases like unit tests.
389 */
390static inline unsigned int llist_count(struct llist_head *head)
391{
392 struct llist_head *entry;
393 unsigned int i = 0;
394 llist_for_each(entry, head)
395 i++;
396 return i;
397}
398
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200399/*!
Neels Hofmeyrb525b9e2017-10-16 16:46:43 +0200400 * @}
Harald Welte2d2e2cc2016-04-25 12:11:20 +0200401 */