[GFS2/DLM] Fix trailing whitespace
[pandora-kernel.git] / include / linux / netfilter_ipv4 / listhelp.h
1 #ifndef _LISTHELP_H
2 #define _LISTHELP_H
3 #include <linux/list.h>
4
5 /* Header to do more comprehensive job than linux/list.h; assume list
6    is first entry in structure. */
7
8 /* Return pointer to first true entry, if any, or NULL.  A macro
9    required to allow inlining of cmpfn. */
10 #define LIST_FIND(head, cmpfn, type, args...)           \
11 ({                                                      \
12         const struct list_head *__i, *__j = NULL;       \
13                                                         \
14         ASSERT_READ_LOCK(head);                         \
15         list_for_each(__i, (head))                      \
16                 if (cmpfn((const type)__i , ## args)) { \
17                         __j = __i;                      \
18                         break;                          \
19                 }                                       \
20         (type)__j;                                      \
21 })
22
23 #define LIST_FIND_W(head, cmpfn, type, args...)         \
24 ({                                                      \
25         const struct list_head *__i, *__j = NULL;       \
26                                                         \
27         ASSERT_WRITE_LOCK(head);                        \
28         list_for_each(__i, (head))                      \
29                 if (cmpfn((type)__i , ## args)) {       \
30                         __j = __i;                      \
31                         break;                          \
32                 }                                       \
33         (type)__j;                                      \
34 })
35
36 /* Just like LIST_FIND but we search backwards */
37 #define LIST_FIND_B(head, cmpfn, type, args...)         \
38 ({                                                      \
39         const struct list_head *__i, *__j = NULL;       \
40                                                         \
41         ASSERT_READ_LOCK(head);                         \
42         list_for_each_prev(__i, (head))                 \
43                 if (cmpfn((const type)__i , ## args)) { \
44                         __j = __i;                      \
45                         break;                          \
46                 }                                       \
47         (type)__j;                                      \
48 })
49
50 static inline int
51 __list_cmp_same(const void *p1, const void *p2) { return p1 == p2; }
52
53 /* Is this entry in the list? */
54 static inline int
55 list_inlist(struct list_head *head, const void *entry)
56 {
57         return LIST_FIND(head, __list_cmp_same, void *, entry) != NULL;
58 }
59
60 /* Delete from list. */
61 #ifdef CONFIG_NETFILTER_DEBUG
62 #define LIST_DELETE(head, oldentry)                                     \
63 do {                                                                    \
64         ASSERT_WRITE_LOCK(head);                                        \
65         if (!list_inlist(head, oldentry))                               \
66                 printk("LIST_DELETE: %s:%u `%s'(%p) not in %s.\n",      \
67                        __FILE__, __LINE__, #oldentry, oldentry, #head); \
68         else list_del((struct list_head *)oldentry);                    \
69 } while(0)
70 #else
71 #define LIST_DELETE(head, oldentry) list_del((struct list_head *)oldentry)
72 #endif
73
74 /* Append. */
75 static inline void
76 list_append(struct list_head *head, void *new)
77 {
78         ASSERT_WRITE_LOCK(head);
79         list_add((new), (head)->prev);
80 }
81
82 /* Prepend. */
83 static inline void
84 list_prepend(struct list_head *head, void *new)
85 {
86         ASSERT_WRITE_LOCK(head);
87         list_add(new, head);
88 }
89
90 /* Insert according to ordering function; insert before first true. */
91 #define LIST_INSERT(head, new, cmpfn)                           \
92 do {                                                            \
93         struct list_head *__i;                                  \
94         ASSERT_WRITE_LOCK(head);                                \
95         list_for_each(__i, (head))                              \
96                 if ((new), (typeof (new))__i)                   \
97                         break;                                  \
98         list_add((struct list_head *)(new), __i->prev);         \
99 } while(0)
100
101 /* If the field after the list_head is a nul-terminated string, you
102    can use these functions. */
103 static inline int __list_cmp_name(const void *i, const char *name)
104 {
105         return strcmp(name, i+sizeof(struct list_head)) == 0;
106 }
107
108 /* Returns false if same name already in list, otherwise does insert. */
109 static inline int
110 list_named_insert(struct list_head *head, void *new)
111 {
112         if (LIST_FIND(head, __list_cmp_name, void *,
113                       new + sizeof(struct list_head)))
114                 return 0;
115         list_prepend(head, new);
116         return 1;
117 }
118
119 /* Find this named element in the list. */
120 #define list_named_find(head, name)                     \
121 LIST_FIND(head, __list_cmp_name, void *, name)
122
123 #endif /*_LISTHELP_H*/