1
/* $Id: linuxlist.h 174 2008-09-20 07:57:08Z toady $ */
12
#define inline __inline__
15
static inline void prefetch(const void *x __attribute__((unused))) {;}
18
* container_of - cast a member of a structure out to the containing structure
20
* @ptr: the pointer to the member.
21
* @type: the type of the container struct this is embedded in.
22
* @member: the name of the member within the struct.
25
#define container_of(ptr, type, member) ({ \
26
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
27
(type *)( (char *)__mptr - offsetof(type,member) );})
31
* These are non-NULL pointers that will result in page faults
32
* under normal circumstances, used to verify that nobody uses
33
* non-initialized llist entries.
35
#define LLIST_POISON1 ((void *) 0x00100100)
36
#define LLIST_POISON2 ((void *) 0x00200200)
39
* Simple doubly linked llist implementation.
41
* Some of the internal functions ("__xxx") are useful when
42
* manipulating whole llists rather than single entries, as
43
* sometimes we already know the next/prev entries and we can
44
* generate better code by using them directly rather than
45
* using the generic single-entry routines.
49
struct llist_head *next, *prev;
52
#define LLIST_HEAD_INIT(name) { &(name), &(name) }
54
#define LLIST_HEAD(name) \
55
struct llist_head name = LLIST_HEAD_INIT(name)
57
#define INIT_LLIST_HEAD(ptr) do { \
58
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
62
* Insert a new entry between two known consecutive entries.
64
* This is only for internal llist manipulation where we know
65
* the prev/next entries already!
67
static inline void __llist_add(struct llist_head *new,
68
struct llist_head *prev,
69
struct llist_head *next)
78
* llist_add - add a new entry
79
* @new: new entry to be added
80
* @head: llist head to add it after
82
* Insert a new entry after the specified head.
83
* This is good for implementing stacks.
85
static inline void llist_add(struct llist_head *new, struct llist_head *head)
87
__llist_add(new, head, head->next);
91
* llist_add_tail - add a new entry
92
* @new: new entry to be added
93
* @head: llist head to add it before
95
* Insert a new entry before the specified head.
96
* This is useful for implementing queues.
98
static inline void llist_add_tail(struct llist_head *new, struct llist_head *head)
100
__llist_add(new, head->prev, head);
104
* Delete a llist entry by making the prev/next entries
105
* point to each other.
107
* This is only for internal llist manipulation where we know
108
* the prev/next entries already!
110
static inline void __llist_del(struct llist_head * prev, struct llist_head * next)
117
* llist_del - deletes entry from llist.
118
* @entry: the element to delete from the llist.
119
* Note: llist_empty on entry does not return true after this, the entry is
120
* in an undefined state.
122
static inline void llist_del(struct llist_head *entry)
124
__llist_del(entry->prev, entry->next);
125
entry->next = LLIST_POISON1;
126
entry->prev = LLIST_POISON2;
130
* llist_del_init - deletes entry from llist and reinitialize it.
131
* @entry: the element to delete from the llist.
133
static inline void llist_del_init(struct llist_head *entry)
135
__llist_del(entry->prev, entry->next);
136
INIT_LLIST_HEAD(entry);
140
* llist_move - delete from one llist and add as another's head
141
* @llist: the entry to move
142
* @head: the head that will precede our entry
144
static inline void llist_move(struct llist_head *llist, struct llist_head *head)
146
__llist_del(llist->prev, llist->next);
147
llist_add(llist, head);
151
* llist_move_tail - delete from one llist and add as another's tail
152
* @llist: the entry to move
153
* @head: the head that will follow our entry
155
static inline void llist_move_tail(struct llist_head *llist,
156
struct llist_head *head)
158
__llist_del(llist->prev, llist->next);
159
llist_add_tail(llist, head);
163
* llist_empty - tests whether a llist is empty
164
* @head: the llist to test.
166
static inline int llist_empty(const struct llist_head *head)
168
return head->next == head;
171
static inline void __llist_splice(struct llist_head *llist,
172
struct llist_head *head)
174
struct llist_head *first = llist->next;
175
struct llist_head *last = llist->prev;
176
struct llist_head *at = head->next;
186
* llist_splice - join two llists
187
* @llist: the new llist to add.
188
* @head: the place to add it in the first llist.
190
static inline void llist_splice(struct llist_head *llist, struct llist_head *head)
192
if (!llist_empty(llist))
193
__llist_splice(llist, head);
197
* llist_splice_init - join two llists and reinitialise the emptied llist.
198
* @llist: the new llist to add.
199
* @head: the place to add it in the first llist.
201
* The llist at @llist is reinitialised
203
static inline void llist_splice_init(struct llist_head *llist,
204
struct llist_head *head)
206
if (!llist_empty(llist)) {
207
__llist_splice(llist, head);
208
INIT_LLIST_HEAD(llist);
213
* llist_entry - get the struct for this entry
214
* @ptr: the &struct llist_head pointer.
215
* @type: the type of the struct this is embedded in.
216
* @member: the name of the llist_struct within the struct.
218
#define llist_entry(ptr, type, member) \
219
container_of(ptr, type, member)
222
* llist_for_each - iterate over a llist
223
* @pos: the &struct llist_head to use as a loop counter.
224
* @head: the head for your llist.
226
#define llist_for_each(pos, head) \
227
for (pos = (head)->next, prefetch(pos->next); pos != (head); \
228
pos = pos->next, prefetch(pos->next))
231
* __llist_for_each - iterate over a llist
232
* @pos: the &struct llist_head to use as a loop counter.
233
* @head: the head for your llist.
235
* This variant differs from llist_for_each() in that it's the
236
* simplest possible llist iteration code, no prefetching is done.
237
* Use this for code that knows the llist to be very short (empty
238
* or 1 entry) most of the time.
240
#define __llist_for_each(pos, head) \
241
for (pos = (head)->next; pos != (head); pos = pos->next)
244
* llist_for_each_prev - iterate over a llist backwards
245
* @pos: the &struct llist_head to use as a loop counter.
246
* @head: the head for your llist.
248
#define llist_for_each_prev(pos, head) \
249
for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
250
pos = pos->prev, prefetch(pos->prev))
253
* llist_for_each_safe - iterate over a llist safe against removal of llist entry
254
* @pos: the &struct llist_head to use as a loop counter.
255
* @n: another &struct llist_head to use as temporary storage
256
* @head: the head for your llist.
258
#define llist_for_each_safe(pos, n, head) \
259
for (pos = (head)->next, n = pos->next; pos != (head); \
260
pos = n, n = pos->next)
263
* llist_for_each_entry - iterate over llist of given type
264
* @pos: the type * to use as a loop counter.
265
* @head: the head for your llist.
266
* @member: the name of the llist_struct within the struct.
268
#define llist_for_each_entry(pos, head, member) \
269
for (pos = llist_entry((head)->next, typeof(*pos), member), \
270
prefetch(pos->member.next); \
271
&pos->member != (head); \
272
pos = llist_entry(pos->member.next, typeof(*pos), member), \
273
prefetch(pos->member.next))
276
* llist_for_each_entry_reverse - iterate backwards over llist of given type.
277
* @pos: the type * to use as a loop counter.
278
* @head: the head for your llist.
279
* @member: the name of the llist_struct within the struct.
281
#define llist_for_each_entry_reverse(pos, head, member) \
282
for (pos = llist_entry((head)->prev, typeof(*pos), member), \
283
prefetch(pos->member.prev); \
284
&pos->member != (head); \
285
pos = llist_entry(pos->member.prev, typeof(*pos), member), \
286
prefetch(pos->member.prev))
289
* llist_for_each_entry_continue - iterate over llist of given type
290
* continuing after existing point
291
* @pos: the type * to use as a loop counter.
292
* @head: the head for your llist.
293
* @member: the name of the llist_struct within the struct.
295
#define llist_for_each_entry_continue(pos, head, member) \
296
for (pos = llist_entry(pos->member.next, typeof(*pos), member), \
297
prefetch(pos->member.next); \
298
&pos->member != (head); \
299
pos = llist_entry(pos->member.next, typeof(*pos), member), \
300
prefetch(pos->member.next))
303
* llist_for_each_entry_safe - iterate over llist of given type safe against removal of llist entry
304
* @pos: the type * to use as a loop counter.
305
* @n: another type * to use as temporary storage
306
* @head: the head for your llist.
307
* @member: the name of the llist_struct within the struct.
309
#define llist_for_each_entry_safe(pos, n, head, member) \
310
for (pos = llist_entry((head)->next, typeof(*pos), member), \
311
n = llist_entry(pos->member.next, typeof(*pos), member); \
312
&pos->member != (head); \
313
pos = n, n = llist_entry(n->member.next, typeof(*n), member))
316
* llist_for_each_rcu - iterate over an rcu-protected llist
317
* @pos: the &struct llist_head to use as a loop counter.
318
* @head: the head for your llist.
320
#define llist_for_each_rcu(pos, head) \
321
for (pos = (head)->next, prefetch(pos->next); pos != (head); \
322
pos = pos->next, ({ smp_read_barrier_depends(); 0;}), prefetch(pos->next))
324
#define __llist_for_each_rcu(pos, head) \
325
for (pos = (head)->next; pos != (head); \
326
pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
329
* llist_for_each_safe_rcu - iterate over an rcu-protected llist safe
330
* against removal of llist entry
331
* @pos: the &struct llist_head to use as a loop counter.
332
* @n: another &struct llist_head to use as temporary storage
333
* @head: the head for your llist.
335
#define llist_for_each_safe_rcu(pos, n, head) \
336
for (pos = (head)->next, n = pos->next; pos != (head); \
337
pos = n, ({ smp_read_barrier_depends(); 0;}), n = pos->next)
340
* llist_for_each_entry_rcu - iterate over rcu llist of given type
341
* @pos: the type * to use as a loop counter.
342
* @head: the head for your llist.
343
* @member: the name of the llist_struct within the struct.
345
#define llist_for_each_entry_rcu(pos, head, member) \
346
for (pos = llist_entry((head)->next, typeof(*pos), member), \
347
prefetch(pos->member.next); \
348
&pos->member != (head); \
349
pos = llist_entry(pos->member.next, typeof(*pos), member), \
350
({ smp_read_barrier_depends(); 0;}), \
351
prefetch(pos->member.next))
355
* llist_for_each_continue_rcu - iterate over an rcu-protected llist
356
* continuing after existing point.
357
* @pos: the &struct llist_head to use as a loop counter.
358
* @head: the head for your llist.
360
#define llist_for_each_continue_rcu(pos, head) \
361
for ((pos) = (pos)->next, prefetch((pos)->next); (pos) != (head); \
362
(pos) = (pos)->next, ({ smp_read_barrier_depends(); 0;}), prefetch((pos)->next))