This commit was manufactured by cvs2svn to create branch
[iptables.git] / libiptc2 / linux_list.h
1 #ifndef _LINUXLIST_H
2 #define _LINUXLIST_H
3
4
5 /*
6  * Simple doubly linked list implementation.
7  *
8  * Some of the internal functions ("__xxx") are useful when
9  * manipulating whole lists rather than single entries, as
10  * sometimes we already know the next/prev entries and we can
11  * generate better code by using them directly rather than
12  * using the generic single-entry routines.
13  */
14
15 struct list_head {
16         struct list_head *next, *prev;
17 };
18
19 #define LIST_HEAD_INIT(name) { &(name), &(name) }
20
21 #define LIST_HEAD(name) \
22         struct list_head name = LIST_HEAD_INIT(name)
23
24 #define INIT_LIST_HEAD(ptr) do { \
25         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
26 } while (0)
27
28 /*
29  * Insert a new entry between two known consecutive entries. 
30  *
31  * This is only for internal list manipulation where we know
32  * the prev/next entries already!
33  */
34 static __inline__ void __list_add(struct list_head * new,
35         struct list_head * prev,
36         struct list_head * next)
37 {
38         next->prev = new;
39         new->next = next;
40         new->prev = prev;
41         prev->next = new;
42 }
43
44 /**
45  * list_add - add a new entry
46  * @new: new entry to be added
47  * @head: list head to add it after
48  *
49  * Insert a new entry after the specified head.
50  * This is good for implementing stacks.
51  */
52 static __inline__ void list_add(struct list_head *new, struct list_head *head)
53 {
54         __list_add(new, head, head->next);
55 }
56
57 /**
58  * list_add_tail - add a new entry
59  * @new: new entry to be added
60  * @head: list head to add it before
61  *
62  * Insert a new entry before the specified head.
63  * This is useful for implementing queues.
64  */
65 static __inline__ void list_add_tail(struct list_head *new, struct list_head *head)
66 {
67         __list_add(new, head->prev, head);
68 }
69
70 /*
71  * Delete a list entry by making the prev/next entries
72  * point to each other.
73  *
74  * This is only for internal list manipulation where we know
75  * the prev/next entries already!
76  */
77 static __inline__ void __list_del(struct list_head * prev,
78                                   struct list_head * next)
79 {
80         next->prev = prev;
81         prev->next = next;
82 }
83
84 /**
85  * list_del - deletes entry from list.
86  * @entry: the element to delete from the list.
87  * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
88  */
89 static __inline__ void list_del(struct list_head *entry)
90 {
91         __list_del(entry->prev, entry->next);
92 }
93
94 /**
95  * list_del_init - deletes entry from list and reinitialize it.
96  * @entry: the element to delete from the list.
97  */
98 static __inline__ void list_del_init(struct list_head *entry)
99 {
100         __list_del(entry->prev, entry->next);
101         INIT_LIST_HEAD(entry); 
102 }
103
104 /**
105  * list_empty - tests whether a list is empty
106  * @head: the list to test.
107  */
108 static __inline__ int list_empty(struct list_head *head)
109 {
110         return head->next == head;
111 }
112
113 /**
114  * list_splice - join two lists
115  * @list: the new list to add.
116  * @head: the place to add it in the first list.
117  */
118 static __inline__ void list_splice(struct list_head *list, struct list_head *head)
119 {
120         struct list_head *first = list->next;
121
122         if (first != list) {
123                 struct list_head *last = list->prev;
124                 struct list_head *at = head->next;
125
126                 first->prev = head;
127                 head->next = first;
128
129                 last->next = at;
130                 at->prev = last;
131         }
132 }
133
134 /**
135  * list_entry - get the struct for this entry
136  * @ptr:        the &struct list_head pointer.
137  * @type:       the type of the struct this is embedded in.
138  * @member:     the name of the list_struct within the struct.
139  */
140 #define list_entry(ptr, type, member) \
141         ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
142
143 /**
144  * list_for_each        -       iterate over a list
145  * @pos:        the &struct list_head to use as a loop counter.
146  * @head:       the head for your list.
147  */
148 #define list_for_each(pos, head) \
149         for (pos = (head)->next; pos != (head); pos = pos->next)
150                 
151 /**
152  * list_for_each_safe   -       iterate over a list safe against removal of list entry
153  * @pos:        the &struct list_head to use as a loop counter.
154  * @n:          another &struct list_head to use as temporary storage
155  * @head:       the head for your list.
156  */
157 #define list_for_each_safe(pos, n, head) \
158         for (pos = (head)->next, n = pos->next; pos != (head); \
159                 pos = n, n = pos->next)
160
161 #endif