blob: b6841520640bc8cacd050986fb6868d8174e361d [file] [log] [blame]
Sylvain Munaut12ba7782014-06-16 10:13:40 +02001#pragma once
Harald Welteec8b4502010-02-20 20:34:29 +01002
3#include <stddef.h>
4
5#ifndef inline
6#define inline __inline__
7#endif
8
9static inline void prefetch(const void *x) {;}
10
11/**
12 * container_of - cast a member of a structure out to the containing structure
13 *
14 * @ptr: the pointer to the member.
15 * @type: the type of the container struct this is embedded in.
16 * @member: the name of the member within the struct.
17 *
18 */
19#define container_of(ptr, type, member) ({ \
20 const typeof( ((type *)0)->member ) *__mptr = (typeof( ((type *)0)->member ) *)(ptr); \
21 (type *)( (char *)__mptr - offsetof(type, member) );})
22
23
24/*
25 * These are non-NULL pointers that will result in page faults
26 * under normal circumstances, used to verify that nobody uses
27 * non-initialized llist entries.
28 */
29#define LLIST_POISON1 ((void *) 0x00100100)
30#define LLIST_POISON2 ((void *) 0x00200200)
31
32/*
33 * Simple doubly linked llist implementation.
34 *
35 * Some of the internal functions ("__xxx") are useful when
36 * manipulating whole llists rather than single entries, as
37 * sometimes we already know the next/prev entries and we can
38 * generate better code by using them directly rather than
39 * using the generic single-entry routines.
40 */
41
42struct llist_head {
43 struct llist_head *next, *prev;
44};
45
46#define LLIST_HEAD_INIT(name) { &(name), &(name) }
47
48#define LLIST_HEAD(name) \
49 struct llist_head name = LLIST_HEAD_INIT(name)
50
51#define INIT_LLIST_HEAD(ptr) do { \
52 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
53} while (0)
54
55/*
56 * Insert a new entry between two known consecutive entries.
57 *
58 * This is only for internal llist manipulation where we know
59 * the prev/next entries already!
60 */
61static inline void __llist_add(struct llist_head *_new,
62 struct llist_head *prev,
63 struct llist_head *next)
64{
65 next->prev = _new;
66 _new->next = next;
67 _new->prev = prev;
68 prev->next = _new;
69}
70
71/**
72 * llist_add - add a new entry
73 * @new: new entry to be added
74 * @head: llist head to add it after
75 *
76 * Insert a new entry after the specified head.
77 * This is good for implementing stacks.
78 */
79static inline void llist_add(struct llist_head *_new, struct llist_head *head)
80{
81 __llist_add(_new, head, head->next);
82}
83
84/**
85 * llist_add_tail - add a new entry
86 * @new: new entry to be added
87 * @head: llist head to add it before
88 *
89 * Insert a new entry before the specified head.
90 * This is useful for implementing queues.
91 */
92static inline void llist_add_tail(struct llist_head *_new, struct llist_head *head)
93{
94 __llist_add(_new, head->prev, head);
95}
96
97/*
98 * Delete a llist entry by making the prev/next entries
99 * point to each other.
100 *
101 * This is only for internal llist manipulation where we know
102 * the prev/next entries already!
103 */
104static inline void __llist_del(struct llist_head * prev, struct llist_head * next)
105{
106 next->prev = prev;
107 prev->next = next;
108}
109
110/**
111 * llist_del - deletes entry from llist.
112 * @entry: the element to delete from the llist.
113 * Note: llist_empty on entry does not return true after this, the entry is
114 * in an undefined state.
115 */
116static inline void llist_del(struct llist_head *entry)
117{
118 __llist_del(entry->prev, entry->next);
119 entry->next = (struct llist_head *)LLIST_POISON1;
120 entry->prev = (struct llist_head *)LLIST_POISON2;
121}
122
123/**
124 * llist_del_init - deletes entry from llist and reinitialize it.
125 * @entry: the element to delete from the llist.
126 */
127static inline void llist_del_init(struct llist_head *entry)
128{
129 __llist_del(entry->prev, entry->next);
130 INIT_LLIST_HEAD(entry);
131}
132
133/**
134 * llist_move - delete from one llist and add as another's head
135 * @llist: the entry to move
136 * @head: the head that will precede our entry
137 */
138static inline void llist_move(struct llist_head *llist, struct llist_head *head)
139{
140 __llist_del(llist->prev, llist->next);
141 llist_add(llist, head);
142}
143
144/**
145 * llist_move_tail - delete from one llist and add as another's tail
146 * @llist: the entry to move
147 * @head: the head that will follow our entry
148 */
149static inline void llist_move_tail(struct llist_head *llist,
150 struct llist_head *head)
151{
152 __llist_del(llist->prev, llist->next);
153 llist_add_tail(llist, head);
154}
155
156/**
157 * llist_empty - tests whether a llist is empty
158 * @head: the llist to test.
159 */
160static inline int llist_empty(const struct llist_head *head)
161{
162 return head->next == head;
163}
164
165static inline void __llist_splice(struct llist_head *llist,
166 struct llist_head *head)
167{
168 struct llist_head *first = llist->next;
169 struct llist_head *last = llist->prev;
170 struct llist_head *at = head->next;
171
172 first->prev = head;
173 head->next = first;
174
175 last->next = at;
176 at->prev = last;
177}
178
179/**
180 * llist_splice - join two llists
181 * @llist: the new llist to add.
182 * @head: the place to add it in the first llist.
183 */
184static inline void llist_splice(struct llist_head *llist, struct llist_head *head)
185{
186 if (!llist_empty(llist))
187 __llist_splice(llist, head);
188}
189
190/**
191 * llist_splice_init - join two llists and reinitialise the emptied llist.
192 * @llist: the new llist to add.
193 * @head: the place to add it in the first llist.
194 *
195 * The llist at @llist is reinitialised
196 */
197static inline void llist_splice_init(struct llist_head *llist,
198 struct llist_head *head)
199{
200 if (!llist_empty(llist)) {
201 __llist_splice(llist, head);
202 INIT_LLIST_HEAD(llist);
203 }
204}
205
206/**
207 * llist_entry - get the struct for this entry
208 * @ptr: the &struct llist_head pointer.
209 * @type: the type of the struct this is embedded in.
210 * @member: the name of the llist_struct within the struct.
211 */
212#define llist_entry(ptr, type, member) \
213 container_of(ptr, type, member)
214
215/**
216 * llist_for_each - iterate over a llist
217 * @pos: the &struct llist_head to use as a loop counter.
218 * @head: the head for your llist.
219 */
220#define llist_for_each(pos, head) \
221 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
222 pos = pos->next, prefetch(pos->next))
223
224/**
225 * __llist_for_each - iterate over a llist
226 * @pos: the &struct llist_head to use as a loop counter.
227 * @head: the head for your llist.
228 *
229 * This variant differs from llist_for_each() in that it's the
230 * simplest possible llist iteration code, no prefetching is done.
231 * Use this for code that knows the llist to be very short (empty
232 * or 1 entry) most of the time.
233 */
234#define __llist_for_each(pos, head) \
235 for (pos = (head)->next; pos != (head); pos = pos->next)
236
237/**
238 * llist_for_each_prev - iterate over a llist backwards
239 * @pos: the &struct llist_head to use as a loop counter.
240 * @head: the head for your llist.
241 */
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))
245
246/**
247 * llist_for_each_safe - iterate over a llist safe against removal of llist entry
248 * @pos: the &struct llist_head to use as a loop counter.
249 * @n: another &struct llist_head to use as temporary storage
250 * @head: the head for your llist.
251 */
252#define llist_for_each_safe(pos, n, head) \
253 for (pos = (head)->next, n = pos->next; pos != (head); \
254 pos = n, n = pos->next)
255
256/**
257 * llist_for_each_entry - iterate over llist of given type
258 * @pos: the type * to use as a loop counter.
259 * @head: the head for your llist.
260 * @member: the name of the llist_struct within the struct.
261 */
262#define llist_for_each_entry(pos, head, member) \
263 for (pos = llist_entry((head)->next, typeof(*pos), member), \
264 prefetch(pos->member.next); \
265 &pos->member != (head); \
266 pos = llist_entry(pos->member.next, typeof(*pos), member), \
267 prefetch(pos->member.next))
268
269/**
270 * llist_for_each_entry_reverse - iterate backwards over llist of given type.
271 * @pos: the type * to use as a loop counter.
272 * @head: the head for your llist.
273 * @member: the name of the llist_struct within the struct.
274 */
275#define llist_for_each_entry_reverse(pos, head, member) \
276 for (pos = llist_entry((head)->prev, typeof(*pos), member), \
277 prefetch(pos->member.prev); \
278 &pos->member != (head); \
279 pos = llist_entry(pos->member.prev, typeof(*pos), member), \
280 prefetch(pos->member.prev))
281
282/**
283 * llist_for_each_entry_continue - iterate over llist of given type
284 * continuing after existing point
285 * @pos: the type * to use as a loop counter.
286 * @head: the head for your llist.
287 * @member: the name of the llist_struct within the struct.
288 */
289#define llist_for_each_entry_continue(pos, head, member) \
290 for (pos = llist_entry(pos->member.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
296/**
297 * llist_for_each_entry_safe - iterate over llist of given type safe against removal of llist entry
298 * @pos: the type * to use as a loop counter.
299 * @n: another type * to use as temporary storage
300 * @head: the head for your llist.
301 * @member: the name of the llist_struct within the struct.
302 */
303#define llist_for_each_entry_safe(pos, n, head, member) \
304 for (pos = llist_entry((head)->next, typeof(*pos), member), \
305 n = llist_entry(pos->member.next, typeof(*pos), member); \
306 &pos->member != (head); \
307 pos = n, n = llist_entry(n->member.next, typeof(*n), member))
308
309/**
310 * llist_for_each_rcu - iterate over an rcu-protected llist
311 * @pos: the &struct llist_head to use as a loop counter.
312 * @head: the head for your llist.
313 */
314#define llist_for_each_rcu(pos, head) \
315 for (pos = (head)->next, prefetch(pos->next); pos != (head); \
316 pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
317
318#define __llist_for_each_rcu(pos, head) \
319 for (pos = (head)->next; pos != (head); \
320 pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
321
322/**
323 * llist_for_each_safe_rcu - iterate over an rcu-protected llist safe
324 * against removal of llist entry
325 * @pos: the &struct llist_head to use as a loop counter.
326 * @n: another &struct llist_head to use as temporary storage
327 * @head: the head for your llist.
328 */
329#define llist_for_each_safe_rcu(pos, n, head) \
330 for (pos = (head)->next, n = pos->next; pos != (head); \
331 pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
332
333/**
334 * llist_for_each_entry_rcu - iterate over rcu llist of given type
335 * @pos: the type * to use as a loop counter.
336 * @head: the head for your llist.
337 * @member: the name of the llist_struct within the struct.
338 */
339#define llist_for_each_entry_rcu(pos, head, member) \
340 for (pos = llist_entry((head)->next, typeof(*pos), member), \
341 prefetch(pos->member.next); \
342 &pos->member != (head); \
343 pos = llist_entry(pos->member.next, typeof(*pos), member), \
344 ({ smp_read_barrier_depends(); 0;}), \
345 prefetch(pos->member.next))
346
347
348/**
349 * llist_for_each_continue_rcu - iterate over an rcu-protected llist
350 * continuing after existing point.
351 * @pos: the &struct llist_head to use as a loop counter.
352 * @head: the head for your llist.
353 */
354#define llist_for_each_continue_rcu(pos, head) \
355 for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
356 (pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))