Commit | Line | Data |
---|---|---|
01743ee1 JA |
1 | #ifndef _LINUX_FLIST_H |
2 | #define _LINUX_FLIST_H | |
ebac4655 | 3 | |
79d16311 JA |
4 | #include <stdlib.h> |
5 | ||
ebac4655 JA |
6 | #undef offsetof |
7 | #ifdef __compiler_offsetof | |
8 | #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER) | |
9 | #else | |
10 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) | |
11 | #endif | |
12 | ||
13 | #define container_of(ptr, type, member) ({ \ | |
14 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ | |
15 | (type *)( (char *)__mptr - offsetof(type,member) );}) | |
16 | ||
17 | /* | |
18 | * Simple doubly linked list implementation. | |
19 | * | |
20 | * Some of the internal functions ("__xxx") are useful when | |
21 | * manipulating whole lists rather than single entries, as | |
22 | * sometimes we already know the next/prev entries and we can | |
23 | * generate better code by using them directly rather than | |
24 | * using the generic single-entry routines. | |
25 | */ | |
26 | ||
01743ee1 JA |
27 | struct flist_head { |
28 | struct flist_head *next, *prev; | |
ebac4655 JA |
29 | }; |
30 | ||
01743ee1 | 31 | #define FLIST_HEAD_INIT(name) { &(name), &(name) } |
ebac4655 | 32 | |
01743ee1 JA |
33 | #define FLIST_HEAD(name) \ |
34 | struct flist_head name = FLIST_HEAD_INIT(name) | |
5f350952 | 35 | |
01743ee1 | 36 | #define INIT_FLIST_HEAD(ptr) do { \ |
ebac4655 JA |
37 | (ptr)->next = (ptr); (ptr)->prev = (ptr); \ |
38 | } while (0) | |
39 | ||
40 | /* | |
41 | * Insert a new entry between two known consecutive entries. | |
42 | * | |
43 | * This is only for internal list manipulation where we know | |
44 | * the prev/next entries already! | |
45 | */ | |
2b52511f | 46 | static inline void __flist_add(struct flist_head *new_entry, |
01743ee1 JA |
47 | struct flist_head *prev, |
48 | struct flist_head *next) | |
ebac4655 | 49 | { |
2b52511f DR |
50 | next->prev = new_entry; |
51 | new_entry->next = next; | |
52 | new_entry->prev = prev; | |
53 | prev->next = new_entry; | |
ebac4655 JA |
54 | } |
55 | ||
56 | /** | |
01743ee1 | 57 | * flist_add - add a new entry |
2b52511f | 58 | * @new_entry: new entry to be added |
ebac4655 JA |
59 | * @head: list head to add it after |
60 | * | |
61 | * Insert a new entry after the specified head. | |
62 | * This is good for implementing stacks. | |
63 | */ | |
2b52511f DR |
64 | static inline void flist_add(struct flist_head *new_entry, |
65 | struct flist_head *head) | |
ebac4655 | 66 | { |
2b52511f | 67 | __flist_add(new_entry, head, head->next); |
ebac4655 JA |
68 | } |
69 | ||
2b52511f | 70 | static inline void flist_add_tail(struct flist_head *new_entry, |
01743ee1 | 71 | struct flist_head *head) |
ebac4655 | 72 | { |
2b52511f | 73 | __flist_add(new_entry, head->prev, head); |
ebac4655 JA |
74 | } |
75 | ||
76 | /* | |
77 | * Delete a list entry by making the prev/next entries | |
78 | * point to each other. | |
79 | * | |
80 | * This is only for internal list manipulation where we know | |
81 | * the prev/next entries already! | |
82 | */ | |
01743ee1 JA |
83 | static inline void __flist_del(struct flist_head *prev, |
84 | struct flist_head * next) | |
ebac4655 JA |
85 | { |
86 | next->prev = prev; | |
87 | prev->next = next; | |
88 | } | |
89 | ||
90 | /** | |
01743ee1 | 91 | * flist_del - deletes entry from list. |
ebac4655 | 92 | * @entry: the element to delete from the list. |
01743ee1 | 93 | * Note: flist_empty on entry does not return true after this, the entry is |
ebac4655 JA |
94 | * in an undefined state. |
95 | */ | |
01743ee1 | 96 | static inline void flist_del(struct flist_head *entry) |
ebac4655 | 97 | { |
01743ee1 | 98 | __flist_del(entry->prev, entry->next); |
ebac4655 JA |
99 | entry->next = NULL; |
100 | entry->prev = NULL; | |
101 | } | |
102 | ||
94a08ec6 | 103 | /** |
01743ee1 | 104 | * flist_del_init - deletes entry from list and reinitialize it. |
94a08ec6 JA |
105 | * @entry: the element to delete from the list. |
106 | */ | |
01743ee1 | 107 | static inline void flist_del_init(struct flist_head *entry) |
94a08ec6 | 108 | { |
01743ee1 JA |
109 | __flist_del(entry->prev, entry->next); |
110 | INIT_FLIST_HEAD(entry); | |
94a08ec6 JA |
111 | } |
112 | ||
ebac4655 | 113 | /** |
01743ee1 | 114 | * flist_empty - tests whether a list is empty |
ebac4655 JA |
115 | * @head: the list to test. |
116 | */ | |
01743ee1 | 117 | static inline int flist_empty(const struct flist_head *head) |
ebac4655 JA |
118 | { |
119 | return head->next == head; | |
120 | } | |
121 | ||
e53ab27c JA |
122 | static inline void __flist_splice(const struct flist_head *list, |
123 | struct flist_head *prev, | |
124 | struct flist_head *next) | |
125 | { | |
126 | struct flist_head *first = list->next; | |
127 | struct flist_head *last = list->prev; | |
128 | ||
129 | first->prev = prev; | |
130 | prev->next = first; | |
131 | ||
132 | last->next = next; | |
133 | next->prev = last; | |
134 | } | |
135 | ||
136 | static inline void flist_splice(const struct flist_head *list, | |
137 | struct flist_head *head) | |
138 | { | |
139 | if (!flist_empty(list)) | |
140 | __flist_splice(list, head, head->next); | |
141 | } | |
142 | ||
aee2ab67 JA |
143 | static inline void flist_splice_tail(struct flist_head *list, |
144 | struct flist_head *head) | |
145 | { | |
146 | if (!flist_empty(list)) | |
147 | __flist_splice(list, head->prev, head); | |
148 | } | |
149 | ||
9342d5f8 JA |
150 | static inline void flist_splice_tail_init(struct flist_head *list, |
151 | struct flist_head *head) | |
152 | { | |
153 | if (!flist_empty(list)) { | |
154 | __flist_splice(list, head->prev, head); | |
155 | INIT_FLIST_HEAD(list); | |
156 | } | |
157 | } | |
158 | ||
e53ab27c JA |
159 | static inline void flist_splice_init(struct flist_head *list, |
160 | struct flist_head *head) | |
161 | { | |
162 | if (!flist_empty(list)) { | |
163 | __flist_splice(list, head, head->next); | |
164 | INIT_FLIST_HEAD(list); | |
165 | } | |
166 | } | |
167 | ||
ebac4655 | 168 | /** |
01743ee1 JA |
169 | * flist_entry - get the struct for this entry |
170 | * @ptr: the &struct flist_head pointer. | |
ebac4655 | 171 | * @type: the type of the struct this is embedded in. |
01743ee1 | 172 | * @member: the name of the flist_struct within the struct. |
ebac4655 | 173 | */ |
01743ee1 | 174 | #define flist_entry(ptr, type, member) \ |
ebac4655 JA |
175 | container_of(ptr, type, member) |
176 | ||
9342d5f8 JA |
177 | #define flist_first_entry(ptr, type, member) \ |
178 | flist_entry((ptr)->next, type, member) | |
179 | ||
ebac4655 | 180 | /** |
01743ee1 JA |
181 | * flist_for_each - iterate over a list |
182 | * @pos: the &struct flist_head to use as a loop counter. | |
ebac4655 JA |
183 | * @head: the head for your list. |
184 | */ | |
01743ee1 | 185 | #define flist_for_each(pos, head) \ |
ebac4655 JA |
186 | for (pos = (head)->next; pos != (head); pos = pos->next) |
187 | ||
188 | /** | |
01743ee1 JA |
189 | * flist_for_each_safe - iterate over a list safe against removal of list entry |
190 | * @pos: the &struct flist_head to use as a loop counter. | |
191 | * @n: another &struct flist_head to use as temporary storage | |
ebac4655 JA |
192 | * @head: the head for your list. |
193 | */ | |
01743ee1 | 194 | #define flist_for_each_safe(pos, n, head) \ |
ebac4655 JA |
195 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
196 | pos = n, n = pos->next) | |
197 | ||
1ae83d45 JA |
198 | extern void flist_sort(void *priv, struct flist_head *head, |
199 | int (*cmp)(void *priv, struct flist_head *a, struct flist_head *b)); | |
200 | ||
ebac4655 | 201 | #endif |