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