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