]> git.lizzy.rs Git - rudp.git/blob - linux_list.h
Switch to CMake
[rudp.git] / linux_list.h
1 #ifndef __LIST_H
2 #define __LIST_H
3
4 #if defined(_WIN32)
5 #define INLINE __inline
6 #else
7 #define INLINE inline
8 #endif
9
10 /* This file is from Linux Kernel (include/linux/list.h) 
11  * and modified by simply removing hardware prefetching of list items. 
12  * Here by copyright, credits attributed to where ever they belong.
13  * Get from http://isis.poly.edu/kulesh/stuff/src/klist/
14  */
15
16 /*
17  * Simple doubly linked list implementation.
18  *
19  * Some of the internal functions ("__xxx") are useful when
20  * manipulating whole lists rather than single entries, as
21  * sometimes we already know the next/prev entries and we can
22  * generate better code by using them directly rather than
23  * using the generic single-entry routines.
24  */
25
26 struct list_head {
27         struct list_head *next, *prev;
28 };
29
30 #define LIST_HEAD_INIT(name) { &(name), &(name) }
31
32 #define LIST_HEAD(name) \
33         struct list_head name = LIST_HEAD_INIT(name)
34
35 #define INIT_LIST_HEAD(ptr) do { \
36         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
37 } while (0)
38
39 /*
40  * Insert a newl entry between two known consecutive entries. 
41  *
42  * This is only for internal list manipulation where we know
43  * the prev/next entries already!
44  */
45 static INLINE void __list_add(struct list_head *newl,
46                               struct list_head *prev,
47                               struct list_head *next)
48 {
49         newl->next = next;
50         newl->prev = prev;
51         next->prev = newl;
52         prev->next = newl;
53 }
54
55 /**
56  * list_add - add a newl entry
57  * @newl: newl entry to be added
58  * @head: list head to add it after
59  *
60  * Insert a newl entry after the specified head.
61  * This is good for implementing stacks.
62  */
63 static INLINE void list_add(struct list_head *newl, struct list_head *head)
64 {
65         __list_add(newl, head, head->next);
66 }
67
68 /**
69  * list_add_tail - add a newl entry
70  * @newl: newl entry to be added
71  * @head: list head to add it before
72  *
73  * Insert a newl entry before the specified head.
74  * This is useful for implementing queues.
75  */
76 static INLINE void list_add_tail(struct list_head *newl, struct list_head *head)
77 {
78         __list_add(newl, head->prev, head);
79 }
80
81 /*
82  * Delete a list entry by making the prev/next entries
83  * point to each other.
84  *
85  * This is only for internal list manipulation where we know
86  * the prev/next entries already!
87  */
88 static INLINE void __list_del(struct list_head *prev, struct list_head *next)
89 {
90         next->prev = prev;
91         prev->next = next;
92 }
93
94 /**
95  * list_del - deletes entry from list.
96  * @entry: the element to delete from the list.
97  * Note: list_empty on entry does not return true after this, the entry is in an undefined state.
98  */
99 static INLINE void list_del(struct list_head *entry)
100 {
101         __list_del(entry->prev, entry->next);
102         entry->next = NULL;
103         entry->prev = (struct list_head *) 0;
104 }
105
106 /**
107  * list_del_init - deletes entry from list and reinitialize it.
108  * @entry: the element to delete from the list.
109  */
110 static INLINE void list_del_init(struct list_head *entry)
111 {
112         __list_del(entry->prev, entry->next);
113         INIT_LIST_HEAD(entry); 
114 }
115
116 /**
117  * list_move - delete from one list and add as another's head
118  * @list: the entry to move
119  * @head: the head that will precede our entry
120  */
121 static INLINE void list_move(struct list_head *list, struct list_head *head)
122 {
123         __list_del(list->prev, list->next);
124         list_add(list, head);
125 }
126
127 /**
128  * list_move_tail - delete from one list and add as another's tail
129  * @list: the entry to move
130  * @head: the head that will follow our entry
131  */
132 static INLINE void list_move_tail(struct list_head *list,
133                                   struct list_head *head)
134 {
135         __list_del(list->prev, list->next);
136         list_add_tail(list, head);
137 }
138
139 /**
140  * list_empty - tests whether a list is empty
141  * @head: the list to test.
142  */
143 static INLINE int list_empty(struct list_head *head)
144 {
145         return head->next == head;
146 }
147
148 static INLINE void __list_splice(struct list_head *list,
149                                  struct list_head *head)
150 {
151         struct list_head *first = list->next;
152         struct list_head *last = list->prev;
153         struct list_head *at = head->next;
154
155         first->prev = head;
156         head->next = first;
157
158         last->next = at;
159         at->prev = last;
160 }
161
162 /**
163  * list_splice - join two lists
164  * @list: the newl list to add.
165  * @head: the place to add it in the first list.
166  */
167 static INLINE void list_splice(struct list_head *list, struct list_head *head)
168 {
169         if (!list_empty(list))
170                 __list_splice(list, head);
171 }
172
173 /**
174  * list_splice_init - join two lists and reinitialise the emptied list.
175  * @list: the newl list to add.
176  * @head: the place to add it in the first list.
177  *
178  * The list at @list is reinitialised
179  */
180 static INLINE void list_splice_init(struct list_head *list,
181                                     struct list_head *head)
182 {
183         if (!list_empty(list)) {
184                 __list_splice(list, head);
185                 INIT_LIST_HEAD(list);
186         }
187 }
188
189 /**
190  * list_entry - get the struct for this entry
191  * @ptr:        the &struct list_head pointer.
192  * @type:       the type of the struct this is embedded in.
193  * @member:     the name of the list_struct within the struct.
194  */
195 #define list_entry(ptr, type, member) \
196         ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
197
198 /**
199  * list_for_each        -       iterate over a list
200  * @pos:        the &struct list_head to use as a loop counter.
201  * @head:       the head for your list.
202  */
203 #define list_for_each(pos, head) \
204         for (pos = (head)->next; pos != (head); \
205                 pos = pos->next)
206 /**
207  * list_for_each_prev   -       iterate over a list backwards
208  * @pos:        the &struct list_head to use as a loop counter.
209  * @head:       the head for your list.
210  */
211 #define list_for_each_prev(pos, head) \
212         for (pos = (head)->prev; pos != (head); \
213                 pos = pos->prev)
214                 
215 /**
216  * list_for_each_safe   -       iterate over a list safe against removal of list entry
217  * @pos:        the &struct list_head to use as a loop counter.
218  * @n:          another &struct list_head to use as temporary storage
219  * @head:       the head for your list.
220  */
221 #define list_for_each_safe(pos, n, head) \
222         for (pos = (head)->next, n = pos->next; pos != (head); \
223                 pos = n, n = pos->next)
224
225 /**
226  * list_for_each_entry  -       iterate over list of given type
227  * @pos:        the type * to use as a loop counter.
228  * @head:       the head for your list.
229  * @member:     the name of the list_struct within the struct.
230  */
231 #define list_for_each_entry(pos, head, member)                          \
232         for (pos = list_entry((head)->next, typeof(*pos), member);      \
233              &pos->member != (head);                                    \
234              pos = list_entry(pos->member.next, typeof(*pos), member))
235
236 /**
237  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
238  * @pos:        the type * to use as a loop counter.
239  * @n:          another type * to use as temporary storage
240  * @head:       the head for your list.
241  * @member:     the name of the list_struct within the struct.
242  */
243 #define list_for_each_entry_safe(pos, n, head, member)                  \
244         for (pos = list_entry((head)->next, typeof(*pos), member),      \
245                 n = list_entry(pos->member.next, typeof(*pos), member); \
246              &pos->member != (head);                                    \
247              pos = n, n = list_entry(n->member.next, typeof(*n), member))
248
249
250 #endif