]>
Commit | Line | Data |
---|---|---|
1e6d2d09 VD |
1 | /** |
2 | * | |
3 | * I grub it from linux kernel source code and fix it for user space | |
4 | * program. Of course, this is a GPL licensed header file. | |
5 | * | |
6 | * Here is a recipe to cook list.h for user space program | |
7 | * | |
8 | * 1. copy list.h from linux/include/list.h | |
9 | * 2. remove | |
10 | * - #ifdef __KERNE__ and its #endif | |
11 | * - all #include line | |
12 | * - prefetch() and rcu related functions | |
13 | * 3. add macro offsetof() and container_of | |
14 | * | |
15 | * - kazutomo@mcs.anl.gov | |
16 | */ | |
17 | #ifndef _LINUX_LIST_H | |
18 | #define _LINUX_LIST_H | |
19 | ||
20 | /** | |
21 | * @name from other kernel headers | |
22 | */ | |
23 | /*@{*/ | |
24 | ||
25 | /** | |
26 | * Get offset of a member | |
27 | */ | |
28 | #ifndef offsetof | |
29 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) | |
30 | #endif | |
31 | ||
32 | /** | |
33 | * Casts a member of a structure out to the containing structure | |
34 | * @param ptr the pointer to the member. | |
35 | * @param type the type of the container struct this is embedded in. | |
36 | * @param member the name of the member within the struct. | |
37 | * | |
38 | */ | |
39 | #define container_of(ptr, type, member) ({ \ | |
40 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ | |
41 | (type *)( (char *)__mptr - offsetof(type,member) );}) | |
42 | /*@}*/ | |
43 | ||
44 | ||
45 | /* | |
46 | * These are non-NULL pointers that will result in page faults | |
47 | * under normal circumstances, used to verify that nobody uses | |
48 | * non-initialized list entries. | |
49 | */ | |
50 | #define LIST_POISON1 ((void *) 0x00100100) | |
51 | #define LIST_POISON2 ((void *) 0x00200200) | |
52 | ||
53 | /** | |
54 | * Simple doubly linked list implementation. | |
55 | * | |
56 | * Some of the internal functions ("__xxx") are useful when | |
57 | * manipulating whole lists rather than single entries, as | |
58 | * sometimes we already know the next/prev entries and we can | |
59 | * generate better code by using them directly rather than | |
60 | * using the generic single-entry routines. | |
61 | */ | |
62 | struct list_head { | |
63 | struct list_head *next, *prev; | |
64 | }; | |
65 | ||
66 | #define LIST_HEAD_INIT(name) { &(name), &(name) } | |
67 | ||
68 | #define LIST_HEAD(name) \ | |
69 | struct list_head name = LIST_HEAD_INIT(name) | |
70 | ||
71 | #define INIT_LIST_HEAD(ptr) do { \ | |
72 | (ptr)->next = (ptr); (ptr)->prev = (ptr); \ | |
73 | } while (0) | |
74 | ||
75 | /* | |
76 | * Insert a new entry between two known consecutive entries. | |
77 | * | |
78 | * This is only for internal list manipulation where we know | |
79 | * the prev/next entries already! | |
80 | */ | |
81 | static inline void __list_add(struct list_head *new, | |
82 | struct list_head *prev, | |
83 | struct list_head *next) | |
84 | { | |
85 | next->prev = new; | |
86 | new->next = next; | |
87 | new->prev = prev; | |
88 | prev->next = new; | |
89 | } | |
90 | ||
91 | /** | |
92 | * list_add - add a new entry | |
93 | * @new: new entry to be added | |
94 | * @head: list head to add it after | |
95 | * | |
96 | * Insert a new entry after the specified head. | |
97 | * This is good for implementing stacks. | |
98 | */ | |
99 | static inline void list_add(struct list_head *new, struct list_head *head) | |
100 | { | |
101 | __list_add(new, head, head->next); | |
102 | } | |
103 | ||
104 | /** | |
105 | * list_add_tail - add a new entry | |
106 | * @new: new entry to be added | |
107 | * @head: list head to add it before | |
108 | * | |
109 | * Insert a new entry before the specified head. | |
110 | * This is useful for implementing queues. | |
111 | */ | |
112 | static inline void list_add_tail(struct list_head *new, struct list_head *head) | |
113 | { | |
114 | __list_add(new, head->prev, head); | |
115 | } | |
116 | ||
117 | ||
118 | /* | |
119 | * Delete a list entry by making the prev/next entries | |
120 | * point to each other. | |
121 | * | |
122 | * This is only for internal list manipulation where we know | |
123 | * the prev/next entries already! | |
124 | */ | |
125 | static inline void __list_del(struct list_head * prev, struct list_head * next) | |
126 | { | |
127 | next->prev = prev; | |
128 | prev->next = next; | |
129 | } | |
130 | ||
131 | /** | |
132 | * list_del - deletes entry from list. | |
133 | * @entry: the element to delete from the list. | |
134 | * Note: list_empty on entry does not return true after this, the entry is | |
135 | * in an undefined state. | |
136 | */ | |
137 | static inline void list_del(struct list_head *entry) | |
138 | { | |
139 | __list_del(entry->prev, entry->next); | |
140 | entry->next = LIST_POISON1; | |
141 | entry->prev = LIST_POISON2; | |
142 | } | |
143 | ||
144 | ||
145 | ||
146 | /** | |
147 | * list_del_init - deletes entry from list and reinitialize it. | |
148 | * @entry: the element to delete from the list. | |
149 | */ | |
150 | static inline void list_del_init(struct list_head *entry) | |
151 | { | |
152 | __list_del(entry->prev, entry->next); | |
153 | INIT_LIST_HEAD(entry); | |
154 | } | |
155 | ||
156 | /** | |
157 | * list_move - delete from one list and add as another's head | |
158 | * @list: the entry to move | |
159 | * @head: the head that will precede our entry | |
160 | */ | |
161 | static inline void list_move(struct list_head *list, struct list_head *head) | |
162 | { | |
163 | __list_del(list->prev, list->next); | |
164 | list_add(list, head); | |
165 | } | |
166 | ||
167 | /** | |
168 | * list_move_tail - delete from one list and add as another's tail | |
169 | * @list: the entry to move | |
170 | * @head: the head that will follow our entry | |
171 | */ | |
172 | static inline void list_move_tail(struct list_head *list, | |
173 | struct list_head *head) | |
174 | { | |
175 | __list_del(list->prev, list->next); | |
176 | list_add_tail(list, head); | |
177 | } | |
178 | ||
179 | /** | |
180 | * list_empty - tests whether a list is empty | |
181 | * @head: the list to test. | |
182 | */ | |
183 | static inline int list_empty(const struct list_head *head) | |
184 | { | |
185 | return head->next == head; | |
186 | } | |
187 | ||
188 | static inline void __list_splice(struct list_head *list, | |
189 | struct list_head *head) | |
190 | { | |
191 | struct list_head *first = list->next; | |
192 | struct list_head *last = list->prev; | |
193 | struct list_head *at = head->next; | |
194 | ||
195 | first->prev = head; | |
196 | head->next = first; | |
197 | ||
198 | last->next = at; | |
199 | at->prev = last; | |
200 | } | |
201 | ||
202 | /** | |
203 | * list_splice - join two lists | |
204 | * @list: the new list to add. | |
205 | * @head: the place to add it in the first list. | |
206 | */ | |
207 | static inline void list_splice(struct list_head *list, struct list_head *head) | |
208 | { | |
209 | if (!list_empty(list)) | |
210 | __list_splice(list, head); | |
211 | } | |
212 | ||
213 | /** | |
214 | * list_splice_init - join two lists and reinitialise the emptied list. | |
215 | * @list: the new list to add. | |
216 | * @head: the place to add it in the first list. | |
217 | * | |
218 | * The list at @list is reinitialised | |
219 | */ | |
220 | static inline void list_splice_init(struct list_head *list, | |
221 | struct list_head *head) | |
222 | { | |
223 | if (!list_empty(list)) { | |
224 | __list_splice(list, head); | |
225 | INIT_LIST_HEAD(list); | |
226 | } | |
227 | } | |
228 | ||
229 | /** | |
230 | * list_entry - get the struct for this entry | |
231 | * @ptr: the &struct list_head pointer. | |
232 | * @type: the type of the struct this is embedded in. | |
233 | * @member: the name of the list_struct within the struct. | |
234 | */ | |
235 | #define list_entry(ptr, type, member) \ | |
236 | container_of(ptr, type, member) | |
237 | ||
238 | /** | |
239 | * list_for_each - iterate over a list | |
240 | * @pos: the &struct list_head to use as a loop counter. | |
241 | * @head: the head for your list. | |
242 | */ | |
243 | ||
244 | #define list_for_each(pos, head) \ | |
245 | for (pos = (head)->next; pos != (head); \ | |
246 | pos = pos->next) | |
247 | ||
248 | /** | |
249 | * __list_for_each - iterate over a list | |
250 | * @pos: the &struct list_head to use as a loop counter. | |
251 | * @head: the head for your list. | |
252 | * | |
253 | * This variant differs from list_for_each() in that it's the | |
254 | * simplest possible list iteration code, no prefetching is done. | |
255 | * Use this for code that knows the list to be very short (empty | |
256 | * or 1 entry) most of the time. | |
257 | */ | |
258 | #define __list_for_each(pos, head) \ | |
259 | for (pos = (head)->next; pos != (head); pos = pos->next) | |
260 | ||
261 | /** | |
262 | * list_for_each_prev - iterate over a list backwards | |
263 | * @pos: the &struct list_head to use as a loop counter. | |
264 | * @head: the head for your list. | |
265 | */ | |
266 | #define list_for_each_prev(pos, head) \ | |
267 | for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \ | |
268 | pos = pos->prev) | |
269 | ||
270 | /** | |
271 | * list_for_each_safe - iterate over a list safe against removal of list entry | |
272 | * @pos: the &struct list_head to use as a loop counter. | |
273 | * @n: another &struct list_head to use as temporary storage | |
274 | * @head: the head for your list. | |
275 | */ | |
276 | #define list_for_each_safe(pos, n, head) \ | |
277 | for (pos = (head)->next, n = pos->next; pos != (head); \ | |
278 | pos = n, n = pos->next) | |
279 | ||
280 | /** | |
281 | * list_for_each_entry - iterate over list of given type | |
282 | * @pos: the type * to use as a loop counter. | |
283 | * @head: the head for your list. | |
284 | * @member: the name of the list_struct within the struct. | |
285 | */ | |
286 | #define list_for_each_entry(pos, head, member) \ | |
287 | for (pos = list_entry((head)->next, typeof(*pos), member); \ | |
288 | &pos->member != (head); \ | |
289 | pos = list_entry(pos->member.next, typeof(*pos), member)) | |
290 | ||
291 | /** | |
292 | * list_for_each_entry_reverse - iterate backwards over list of given type. | |
293 | * @pos: the type * to use as a loop counter. | |
294 | * @head: the head for your list. | |
295 | * @member: the name of the list_struct within the struct. | |
296 | */ | |
297 | #define list_for_each_entry_reverse(pos, head, member) \ | |
298 | for (pos = list_entry((head)->prev, typeof(*pos), member); \ | |
299 | &pos->member != (head); \ | |
300 | pos = list_entry(pos->member.prev, typeof(*pos), member)) | |
301 | ||
302 | /** | |
303 | * list_prepare_entry - prepare a pos entry for use as a start point in | |
304 | * list_for_each_entry_continue | |
305 | * @pos: the type * to use as a start point | |
306 | * @head: the head of the list | |
307 | * @member: the name of the list_struct within the struct. | |
308 | */ | |
309 | #define list_prepare_entry(pos, head, member) \ | |
310 | ((pos) ? : list_entry(head, typeof(*pos), member)) | |
311 | ||
312 | /** | |
313 | * list_for_each_entry_continue - iterate over list of given type | |
314 | * continuing after existing point | |
315 | * @pos: the type * to use as a loop counter. | |
316 | * @head: the head for your list. | |
317 | * @member: the name of the list_struct within the struct. | |
318 | */ | |
319 | #define list_for_each_entry_continue(pos, head, member) \ | |
320 | for (pos = list_entry(pos->member.next, typeof(*pos), member); \ | |
321 | &pos->member != (head); \ | |
322 | pos = list_entry(pos->member.next, typeof(*pos), member)) | |
323 | ||
324 | /** | |
325 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry | |
326 | * @pos: the type * to use as a loop counter. | |
327 | * @n: another type * to use as temporary storage | |
328 | * @head: the head for your list. | |
329 | * @member: the name of the list_struct within the struct. | |
330 | */ | |
331 | #define list_for_each_entry_safe(pos, n, head, member) \ | |
332 | for (pos = list_entry((head)->next, typeof(*pos), member), \ | |
333 | n = list_entry(pos->member.next, typeof(*pos), member); \ | |
334 | &pos->member != (head); \ | |
335 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |
336 | ||
337 | /** | |
338 | * list_for_each_entry_safe_continue - iterate over list of given type | |
339 | * continuing after existing point safe against removal of list entry | |
340 | * @pos: the type * to use as a loop counter. | |
341 | * @n: another type * to use as temporary storage | |
342 | * @head: the head for your list. | |
343 | * @member: the name of the list_struct within the struct. | |
344 | */ | |
345 | #define list_for_each_entry_safe_continue(pos, n, head, member) \ | |
346 | for (pos = list_entry(pos->member.next, typeof(*pos), member), \ | |
347 | n = list_entry(pos->member.next, typeof(*pos), member); \ | |
348 | &pos->member != (head); \ | |
349 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |
350 | ||
351 | /** | |
352 | * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against | |
353 | * removal of list entry | |
354 | * @pos: the type * to use as a loop counter. | |
355 | * @n: another type * to use as temporary storage | |
356 | * @head: the head for your list. | |
357 | * @member: the name of the list_struct within the struct. | |
358 | */ | |
359 | #define list_for_each_entry_safe_reverse(pos, n, head, member) \ | |
360 | for (pos = list_entry((head)->prev, typeof(*pos), member), \ | |
361 | n = list_entry(pos->member.prev, typeof(*pos), member); \ | |
362 | &pos->member != (head); \ | |
363 | pos = n, n = list_entry(n->member.prev, typeof(*n), member)) | |
364 | ||
365 | ||
366 | ||
367 | ||
368 | /* | |
369 | * Double linked lists with a single pointer list head. | |
370 | * Mostly useful for hash tables where the two pointer list head is | |
371 | * too wasteful. | |
372 | * You lose the ability to access the tail in O(1). | |
373 | */ | |
374 | ||
375 | struct hlist_head { | |
376 | struct hlist_node *first; | |
377 | }; | |
378 | ||
379 | struct hlist_node { | |
380 | struct hlist_node *next, **pprev; | |
381 | }; | |
382 | ||
383 | #define HLIST_HEAD_INIT { .first = NULL } | |
384 | #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL } | |
385 | #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) | |
386 | #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL) | |
387 | ||
388 | static inline int hlist_unhashed(const struct hlist_node *h) | |
389 | { | |
390 | return !h->pprev; | |
391 | } | |
392 | ||
393 | static inline int hlist_empty(const struct hlist_head *h) | |
394 | { | |
395 | return !h->first; | |
396 | } | |
397 | ||
398 | static inline void __hlist_del(struct hlist_node *n) | |
399 | { | |
400 | struct hlist_node *next = n->next; | |
401 | struct hlist_node **pprev = n->pprev; | |
402 | *pprev = next; | |
403 | if (next) | |
404 | next->pprev = pprev; | |
405 | } | |
406 | ||
407 | static inline void hlist_del(struct hlist_node *n) | |
408 | { | |
409 | __hlist_del(n); | |
410 | n->next = LIST_POISON1; | |
411 | n->pprev = LIST_POISON2; | |
412 | } | |
413 | ||
414 | ||
415 | static inline void hlist_del_init(struct hlist_node *n) | |
416 | { | |
417 | if (n->pprev) { | |
418 | __hlist_del(n); | |
419 | INIT_HLIST_NODE(n); | |
420 | } | |
421 | } | |
422 | ||
423 | static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h) | |
424 | { | |
425 | struct hlist_node *first = h->first; | |
426 | n->next = first; | |
427 | if (first) | |
428 | first->pprev = &n->next; | |
429 | h->first = n; | |
430 | n->pprev = &h->first; | |
431 | } | |
432 | ||
433 | ||
434 | ||
435 | /* next must be != NULL */ | |
436 | static inline void hlist_add_before(struct hlist_node *n, | |
437 | struct hlist_node *next) | |
438 | { | |
439 | n->pprev = next->pprev; | |
440 | n->next = next; | |
441 | next->pprev = &n->next; | |
442 | *(n->pprev) = n; | |
443 | } | |
444 | ||
445 | static inline void hlist_add_after(struct hlist_node *n, | |
446 | struct hlist_node *next) | |
447 | { | |
448 | next->next = n->next; | |
449 | n->next = next; | |
450 | next->pprev = &n->next; | |
451 | ||
452 | if(next->next) | |
453 | next->next->pprev = &next->next; | |
454 | } | |
455 | ||
456 | ||
457 | ||
458 | #define hlist_entry(ptr, type, member) container_of(ptr,type,member) | |
459 | ||
460 | #define hlist_for_each(pos, head) \ | |
461 | for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \ | |
462 | pos = pos->next) | |
463 | ||
464 | #define hlist_for_each_safe(pos, n, head) \ | |
465 | for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \ | |
466 | pos = n) | |
467 | ||
468 | /** | |
469 | * hlist_for_each_entry - iterate over list of given type | |
470 | * @tpos: the type * to use as a loop counter. | |
471 | * @pos: the &struct hlist_node to use as a loop counter. | |
472 | * @head: the head for your list. | |
473 | * @member: the name of the hlist_node within the struct. | |
474 | */ | |
475 | #define hlist_for_each_entry(tpos, pos, head, member) \ | |
476 | for (pos = (head)->first; \ | |
477 | pos && ({ prefetch(pos->next); 1;}) && \ | |
478 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ | |
479 | pos = pos->next) | |
480 | ||
481 | /** | |
482 | * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point | |
483 | * @tpos: the type * to use as a loop counter. | |
484 | * @pos: the &struct hlist_node to use as a loop counter. | |
485 | * @member: the name of the hlist_node within the struct. | |
486 | */ | |
487 | #define hlist_for_each_entry_continue(tpos, pos, member) \ | |
488 | for (pos = (pos)->next; \ | |
489 | pos && ({ prefetch(pos->next); 1;}) && \ | |
490 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ | |
491 | pos = pos->next) | |
492 | ||
493 | /** | |
494 | * hlist_for_each_entry_from - iterate over a hlist continuing from existing point | |
495 | * @tpos: the type * to use as a loop counter. | |
496 | * @pos: the &struct hlist_node to use as a loop counter. | |
497 | * @member: the name of the hlist_node within the struct. | |
498 | */ | |
499 | #define hlist_for_each_entry_from(tpos, pos, member) \ | |
500 | for (; pos && ({ prefetch(pos->next); 1;}) && \ | |
501 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ | |
502 | pos = pos->next) | |
503 | ||
504 | /** | |
505 | * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry | |
506 | * @tpos: the type * to use as a loop counter. | |
507 | * @pos: the &struct hlist_node to use as a loop counter. | |
508 | * @n: another &struct hlist_node to use as temporary storage | |
509 | * @head: the head for your list. | |
510 | * @member: the name of the hlist_node within the struct. | |
511 | */ | |
512 | #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \ | |
513 | for (pos = (head)->first; \ | |
514 | pos && ({ n = pos->next; 1; }) && \ | |
515 | ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \ | |
516 | pos = n) | |
517 | ||
518 | ||
519 | #endif |