1 | /* $Id: list.h,v 1.1.1.1 2003/07/02 13:57:00 eleph Exp $ */
|
---|
2 |
|
---|
3 | #ifndef _LINUX_LIST_H
|
---|
4 | #define _LINUX_LIST_H
|
---|
5 | #include <linux/types.h>
|
---|
6 | #ifdef __KERNEL__
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Simple doubly linked list implementation.
|
---|
10 | *
|
---|
11 | * Some of the internal functions ("__xxx") are useful when
|
---|
12 | * manipulating whole lists rather than single entries, as
|
---|
13 | * sometimes we already know the next/prev entries and we can
|
---|
14 | * generate better code by using them directly rather than
|
---|
15 | * using the generic single-entry routines.
|
---|
16 | */
|
---|
17 |
|
---|
18 | struct list_head {
|
---|
19 | struct list_head *next, *prev;
|
---|
20 | };
|
---|
21 |
|
---|
22 | #define LIST_HEAD_INIT(name) { &(name), &(name) }
|
---|
23 |
|
---|
24 | #define LIST_HEAD(name) \
|
---|
25 | struct list_head name = LIST_HEAD_INIT(name)
|
---|
26 |
|
---|
27 | static inline void INIT_LIST_HEAD(struct list_head *list)
|
---|
28 | {
|
---|
29 | list->next = list;
|
---|
30 | list->prev = list;
|
---|
31 | }
|
---|
32 |
|
---|
33 | /*
|
---|
34 | * Insert a new entry between two known consecutive entries.
|
---|
35 | *
|
---|
36 | * This is only for internal list manipulation where we know
|
---|
37 | * the prev/next entries already!
|
---|
38 | */
|
---|
39 | static __inline__ void __list_add(struct list_head * new,
|
---|
40 | struct list_head * prev,
|
---|
41 | struct list_head * next)
|
---|
42 | {
|
---|
43 | next->prev = new;
|
---|
44 | new->next = next;
|
---|
45 | new->prev = prev;
|
---|
46 | prev->next = new;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /*
|
---|
50 | * Insert a new entry after the specified head..
|
---|
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 | * Insert a new entry before the specified head..
|
---|
59 | */
|
---|
60 | static __inline__ void list_add_tail(struct list_head *new, struct list_head *head)
|
---|
61 | {
|
---|
62 | __list_add(new, head->prev, head);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * Delete a list entry by making the prev/next entries
|
---|
67 | * point to each other.
|
---|
68 | *
|
---|
69 | * This is only for internal list manipulation where we know
|
---|
70 | * the prev/next entries already!
|
---|
71 | */
|
---|
72 | static __inline__ void __list_del(struct list_head * prev,
|
---|
73 | struct list_head * next)
|
---|
74 | {
|
---|
75 | next->prev = prev;
|
---|
76 | prev->next = next;
|
---|
77 | }
|
---|
78 |
|
---|
79 | static __inline__ void list_del(struct list_head *entry)
|
---|
80 | {
|
---|
81 | __list_del(entry->prev, entry->next);
|
---|
82 | }
|
---|
83 |
|
---|
84 | static __inline__ int list_empty(struct list_head *head)
|
---|
85 | {
|
---|
86 | return head->next == head;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * Splice in "list" into "head"
|
---|
91 | */
|
---|
92 | static __inline__ void list_splice(struct list_head *list, struct list_head *head)
|
---|
93 | {
|
---|
94 | struct list_head *first = list->next;
|
---|
95 |
|
---|
96 | if (first != list) {
|
---|
97 | struct list_head *last = list->prev;
|
---|
98 | struct list_head *at = head->next;
|
---|
99 |
|
---|
100 | first->prev = head;
|
---|
101 | head->next = first;
|
---|
102 |
|
---|
103 | last->next = at;
|
---|
104 | at->prev = last;
|
---|
105 | }
|
---|
106 | }
|
---|
107 |
|
---|
108 | _WCRTLINK extern int snprintf( char *__buf, size_t __bufsize,
|
---|
109 | const char *__fmt, ... );
|
---|
110 |
|
---|
111 | #define offsetof(__typ,__id) ((size_t)&(((__typ*)0)->__id))
|
---|
112 |
|
---|
113 | #define list_entry(ptr, type, member) \
|
---|
114 | container_of(ptr, type, member)
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * list_first_entry - get the first element from a list
|
---|
118 | * @ptr: the list head to take the element from.
|
---|
119 | * @type: the type of the struct this is embedded in.
|
---|
120 | * @member: the name of the list_struct within the struct.
|
---|
121 | *
|
---|
122 | * Note, that list is expected to be not empty.
|
---|
123 | */
|
---|
124 | #define list_first_entry(ptr, type, member) \
|
---|
125 | list_entry((ptr)->next, type, member)
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * list_last_entry - get the last element from a list
|
---|
129 | * @ptr: the list head to take the element from.
|
---|
130 | * @type: the type of the struct this is embedded in.
|
---|
131 | * @member: the name of the list_struct within the struct.
|
---|
132 | *
|
---|
133 | * Note, that list is expected to be not empty.
|
---|
134 | */
|
---|
135 | #define list_last_entry(ptr, type, member) \
|
---|
136 | list_entry((ptr)->prev, type, member)
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * list_next_entry - get the next element in list
|
---|
140 | * @pos: the type * to cursor
|
---|
141 | * @member: the name of the list_struct within the struct.
|
---|
142 | */
|
---|
143 | #define list_next_entry(pos, member) \
|
---|
144 | list_entry((pos)->member.next, typeof(*(pos)), member)
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * list_prev_entry - get the prev element in list
|
---|
148 | * @pos: the type * to cursor
|
---|
149 | * @member: the name of the list_struct within the struct.
|
---|
150 | */
|
---|
151 | #define list_prev_entry(pos, member) \
|
---|
152 | list_entry((pos)->member.prev, typeof(*(pos)), member)
|
---|
153 |
|
---|
154 | #define list_for_each(entry, listhead) \
|
---|
155 | for(entry=(listhead)->next;entry != listhead;entry=entry->next)
|
---|
156 |
|
---|
157 | #endif /* __KERNEL__ */
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * list_for_each_prev - iterate over a list backwards
|
---|
161 | * @pos: the &struct list_head to use as a loop cursor.
|
---|
162 | * @head: the head for your list.
|
---|
163 | */
|
---|
164 | #define list_for_each_prev(pos, head) \
|
---|
165 | for (pos = (head)->prev; pos != (head); pos = pos->prev)
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * list_for_each_entry - iterate over list of given type
|
---|
169 | * @pos: the type * to use as a loop cursor.
|
---|
170 | * @head: the head for your list.
|
---|
171 | * @member: the name of the list_struct within the struct.
|
---|
172 | for (pos = list_first_entry(head, typeof(*pos), member); \
|
---|
173 | &pos->member != (head); \
|
---|
174 | pos = list_next_entry(pos, member))
|
---|
175 | */
|
---|
176 | #define list_for_each_entry(itemptr, headptr, struct_listmember_name, container_type) \
|
---|
177 | for (itemptr=(container_type *) \
|
---|
178 | (((char *)((headptr)->next))-offsetof(container_type, struct_listmember_name)); \
|
---|
179 | &(itemptr->struct_listmember_name)!=(headptr); \
|
---|
180 | itemptr=(container_type *) \
|
---|
181 | (((char *)(itemptr->struct_listmember_name.next))-offsetof(container_type, struct_listmember_name)))
|
---|
182 |
|
---|
183 | /**
|
---|
184 | * list_for_each_entry_reverse - iterate backwards over list of given type.
|
---|
185 | * @pos: the type * to use as a loop cursor.
|
---|
186 | * @head: the head for your list.
|
---|
187 | * @member: the name of the list_struct within the struct.
|
---|
188 | for (pos = list_last_entry(head, typeof(*pos), member); \
|
---|
189 | &pos->member != (head); \
|
---|
190 | pos = list_prev_entry(pos, member))
|
---|
191 |
|
---|
192 | */
|
---|
193 |
|
---|
194 | #define list_for_each_entry_reverse(itemptr, headptr, struct_listmember_name, container_type) \
|
---|
195 | for (itemptr=(container_type *) \
|
---|
196 | (((char *)((headptr)->prev))-offsetof(container_type, struct_listmember_name)); \
|
---|
197 | &(itemptr->struct_listmember_name)!=(headptr); \
|
---|
198 | itemptr=(container_type *) \
|
---|
199 | (((char *)(itemptr->struct_listmember_name.prev))-offsetof(container_type, struct_listmember_name)))
|
---|
200 |
|
---|
201 |
|
---|
202 |
|
---|
203 | #define list_for_each_entry_safe(itemptr, n, headptr, struct_listmember_name, container_type) \
|
---|
204 | for (itemptr=(container_type *) \
|
---|
205 | (((char *)((headptr)->next))-offsetof(container_type, struct_listmember_name)), \
|
---|
206 | n=(container_type *) \
|
---|
207 | (((char *)(itemptr->struct_listmember_name.next))-offsetof(container_type, struct_listmember_name)); \
|
---|
208 | &(itemptr->struct_listmember_name)!=(headptr); \
|
---|
209 | itemptr=n, \
|
---|
210 | n=(container_type *) \
|
---|
211 | (((char *)(n->struct_listmember_name.next))-offsetof(container_type, struct_listmember_name)))
|
---|
212 |
|
---|
213 | /**
|
---|
214 | * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
|
---|
215 | * @pos: the type * to use as a loop cursor.
|
---|
216 | * @n: another type * to use as temporary storage
|
---|
217 | * @head: the head for your list.
|
---|
218 | * @member: the name of the list_struct within the struct.
|
---|
219 | *
|
---|
220 | * Iterate backwards over list of given type, safe against removal
|
---|
221 | * of list entry.
|
---|
222 | */
|
---|
223 | #define list_for_each_entry_safe_reverse(itemptr, n, headptr, struct_listmember_name, container_type) \
|
---|
224 | for (itemptr=(container_type *) \
|
---|
225 | (((char *)((headptr)->prev))-offsetof(container_type, struct_listmember_name)), \
|
---|
226 | n=(container_type *) \
|
---|
227 | (((char *)(itemptr->struct_listmember_name.prev))-offsetof(container_type, struct_listmember_name)); \
|
---|
228 | &(itemptr->struct_listmember_name)!=(headptr); \
|
---|
229 | itemptr=n, \
|
---|
230 | n=(container_type *) \
|
---|
231 | (((char *)(n->struct_listmember_name.prev))-offsetof(container_type, struct_listmember_name)))
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * list_move_tail - delete from one list and add as another's tail
|
---|
235 | * @list: the entry to move
|
---|
236 | * @head: the head that will follow our entry
|
---|
237 | */
|
---|
238 | static inline void list_move_tail(struct list_head *list,
|
---|
239 | struct list_head *head)
|
---|
240 | {
|
---|
241 | __list_del(list->prev, list->next);
|
---|
242 | list_add_tail(list, head);
|
---|
243 | }
|
---|
244 |
|
---|
245 | /**
|
---|
246 | * list_for_each_safe - iterate over a list safe against removal of list entry
|
---|
247 | * @pos: the &struct list_head to use as a loop counter.
|
---|
248 | * @n: another &struct list_head to use as temporary storage
|
---|
249 | * @head: the head for your list.
|
---|
250 | */
|
---|
251 | #define list_for_each_safe(pos, n, head) \
|
---|
252 | for (pos = (head)->next, n = pos->next; pos != (head); \
|
---|
253 | pos = n, n = pos->next)
|
---|
254 |
|
---|
255 | /**
|
---|
256 | * list_del_init - deletes entry from list and reinitialize it.
|
---|
257 | * @entry: the element to delete from the list.
|
---|
258 | */
|
---|
259 | static __inline__ void list_del_init(struct list_head *entry)
|
---|
260 | {
|
---|
261 | __list_del(entry->prev, entry->next);
|
---|
262 | INIT_LIST_HEAD(entry);
|
---|
263 | }
|
---|
264 |
|
---|
265 | static inline void __list_del_entry(struct list_head *entry)
|
---|
266 | {
|
---|
267 | __list_del(entry->prev, entry->next);
|
---|
268 | }
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * list_first_entry_or_null - get the first element from a list
|
---|
272 | * @ptr: the list head to take the element from.
|
---|
273 | * @type: the type of the struct this is embedded in.
|
---|
274 | * @member: the name of the list_head within the struct.
|
---|
275 | *
|
---|
276 | * Note that if the list is empty, it returns NULL.
|
---|
277 | */
|
---|
278 | #define list_first_entry_or_null(ptr, type, member) \
|
---|
279 | (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
|
---|
280 |
|
---|
281 | /**
|
---|
282 | * list_move - delete from one list and add as another's head
|
---|
283 | * @list: the entry to move
|
---|
284 | * @head: the head that will precede our entry
|
---|
285 | */
|
---|
286 | static inline void list_move(struct list_head *list, struct list_head *head)
|
---|
287 | {
|
---|
288 | __list_del_entry(list);
|
---|
289 | list_add(list, head);
|
---|
290 | }
|
---|
291 |
|
---|
292 | /**
|
---|
293 | * list_is_singular - tests whether a list has just one entry.
|
---|
294 | * @head: the list to test.
|
---|
295 | */
|
---|
296 | static inline int list_is_singular(struct list_head *head)
|
---|
297 | {
|
---|
298 | return !list_empty(head) && (head->next == head->prev);
|
---|
299 | }
|
---|
300 |
|
---|
301 | #endif
|
---|