~ubuntu-branches/ubuntu/trusty/picviz/trusty

« back to all changes in this revision

Viewing changes to src/libpicviz/include/linuxlist.h

  • Committer: Bazaar Package Importer
  • Author(s): Pierre Chifflier
  • Date: 2009-03-30 10:42:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090330104208-j095obwkp574t1lm
Tags: upstream-0.5
ImportĀ upstreamĀ versionĀ 0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: linuxlist.h 174 2008-09-20 07:57:08Z toady $ */
 
2
#ifndef _LINUX_LLIST_H
 
3
#define _LINUX_LLIST_H
 
4
 
 
5
#include <stddef.h>
 
6
 
 
7
#ifdef __cplusplus
 
8
 extern "C" {
 
9
#endif
 
10
 
 
11
#ifndef inline
 
12
#define inline __inline__
 
13
#endif
 
14
 
 
15
static inline void prefetch(const void *x __attribute__((unused))) {;}
 
16
 
 
17
/**
 
18
 * container_of - cast a member of a structure out to the containing structure
 
19
 *
 
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.
 
23
 *
 
24
 */
 
25
#define container_of(ptr, type, member) ({                      \
 
26
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
 
27
        (type *)( (char *)__mptr - offsetof(type,member) );})
 
28
 
 
29
 
 
30
/*
 
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.
 
34
 */
 
35
#define LLIST_POISON1  ((void *) 0x00100100)
 
36
#define LLIST_POISON2  ((void *) 0x00200200)
 
37
 
 
38
/*
 
39
 * Simple doubly linked llist implementation.
 
40
 *
 
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.
 
46
 */
 
47
 
 
48
struct llist_head {
 
49
        struct llist_head *next, *prev;
 
50
};
 
51
 
 
52
#define LLIST_HEAD_INIT(name) { &(name), &(name) }
 
53
 
 
54
#define LLIST_HEAD(name) \
 
55
        struct llist_head name = LLIST_HEAD_INIT(name)
 
56
 
 
57
#define INIT_LLIST_HEAD(ptr) do { \
 
58
        (ptr)->next = (ptr); (ptr)->prev = (ptr); \
 
59
} while (0)
 
60
 
 
61
/*
 
62
 * Insert a new entry between two known consecutive entries.
 
63
 *
 
64
 * This is only for internal llist manipulation where we know
 
65
 * the prev/next entries already!
 
66
 */
 
67
static inline void __llist_add(struct llist_head *new,
 
68
                                struct llist_head *prev,
 
69
                                struct llist_head *next)
 
70
{
 
71
        next->prev = new;
 
72
        new->next = next;
 
73
        new->prev = prev;
 
74
        prev->next = new;
 
75
}
 
76
 
 
77
/**
 
78
 * llist_add - add a new entry
 
79
 * @new: new entry to be added
 
80
 * @head: llist head to add it after
 
81
 *
 
82
 * Insert a new entry after the specified head.
 
83
 * This is good for implementing stacks.
 
84
 */
 
85
static inline void llist_add(struct llist_head *new, struct llist_head *head)
 
86
{
 
87
        __llist_add(new, head, head->next);
 
88
}
 
89
 
 
90
/**
 
91
 * llist_add_tail - add a new entry
 
92
 * @new: new entry to be added
 
93
 * @head: llist head to add it before
 
94
 *
 
95
 * Insert a new entry before the specified head.
 
96
 * This is useful for implementing queues.
 
97
 */
 
98
static inline void llist_add_tail(struct llist_head *new, struct llist_head *head)
 
99
{
 
100
        __llist_add(new, head->prev, head);
 
101
}
 
102
 
 
103
/*
 
104
 * Delete a llist entry by making the prev/next entries
 
105
 * point to each other.
 
106
 *
 
107
 * This is only for internal llist manipulation where we know
 
108
 * the prev/next entries already!
 
109
 */
 
110
static inline void __llist_del(struct llist_head * prev, struct llist_head * next)
 
111
{
 
112
        next->prev = prev;
 
113
        prev->next = next;
 
114
}
 
115
 
 
116
/**
 
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.
 
121
 */
 
122
static inline void llist_del(struct llist_head *entry)
 
123
{
 
124
        __llist_del(entry->prev, entry->next);
 
125
        entry->next = LLIST_POISON1;
 
126
        entry->prev = LLIST_POISON2;
 
127
}
 
128
 
 
129
/**
 
130
 * llist_del_init - deletes entry from llist and reinitialize it.
 
131
 * @entry: the element to delete from the llist.
 
132
 */
 
133
static inline void llist_del_init(struct llist_head *entry)
 
134
{
 
135
        __llist_del(entry->prev, entry->next);
 
136
        INIT_LLIST_HEAD(entry);
 
137
}
 
138
 
 
139
/**
 
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
 
143
 */
 
144
static 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
 
 
150
/**
 
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
 
154
 */
 
155
static 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
 
 
162
/**
 
163
 * llist_empty - tests whether a llist is empty
 
164
 * @head: the llist to test.
 
165
 */
 
166
static inline int llist_empty(const struct llist_head *head)
 
167
{
 
168
        return head->next == head;
 
169
}
 
170
 
 
171
static 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
 
 
185
/**
 
186
 * llist_splice - join two llists
 
187
 * @llist: the new llist to add.
 
188
 * @head: the place to add it in the first llist.
 
189
 */
 
190
static inline void llist_splice(struct llist_head *llist, struct llist_head *head)
 
191
{
 
192
        if (!llist_empty(llist))
 
193
                __llist_splice(llist, head);
 
194
}
 
195
 
 
196
/**
 
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.
 
200
 *
 
201
 * The llist at @llist is reinitialised
 
202
 */
 
203
static inline void llist_splice_init(struct llist_head *llist,
 
204
                                    struct llist_head *head)
 
205
{
 
206
        if (!llist_empty(llist)) {
 
207
                __llist_splice(llist, head);
 
208
                INIT_LLIST_HEAD(llist);
 
209
        }
 
210
}
 
211
 
 
212
/**
 
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.
 
217
 */
 
218
#define llist_entry(ptr, type, member) \
 
219
        container_of(ptr, type, member)
 
220
 
 
221
/**
 
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.
 
225
 */
 
226
#define llist_for_each(pos, head) \
 
227
        for (pos = (head)->next, prefetch(pos->next); pos != (head); \
 
228
                pos = pos->next, prefetch(pos->next))
 
229
 
 
230
/**
 
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.
 
234
 *
 
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.
 
239
 */
 
240
#define __llist_for_each(pos, head) \
 
241
        for (pos = (head)->next; pos != (head); pos = pos->next)
 
242
 
 
243
/**
 
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.
 
247
 */
 
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))
 
251
 
 
252
/**
 
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.
 
257
 */
 
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)
 
261
 
 
262
/**
 
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.
 
267
 */
 
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))
 
274
 
 
275
/**
 
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.
 
280
 */
 
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))
 
287
 
 
288
/**
 
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.
 
294
 */
 
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))
 
301
 
 
302
/**
 
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.
 
308
 */
 
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))
 
314
 
 
315
/**
 
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.
 
319
 */
 
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))
 
323
 
 
324
#define __llist_for_each_rcu(pos, head) \
 
325
        for (pos = (head)->next; pos != (head); \
 
326
                pos = pos->next, ({ smp_read_barrier_depends(); 0;}))
 
327
 
 
328
/**
 
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.
 
334
 */
 
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)
 
338
 
 
339
/**
 
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.
 
344
 */
 
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))
 
352
 
 
353
 
 
354
/**
 
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.
 
359
 */
 
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))
 
363
 
 
364
#ifdef __cplusplus
 
365
 }
 
366
#endif
 
367
 
 
368
#endif