Commit | Line | Data |
---|---|---|
9e066e23 CM |
1 | /* |
2 | * Copyright (C) 2012 Fusion-io. All rights reserved. | |
3 | * | |
4 | * This header was taken from the Linux kernel | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public | |
8 | * License v2 as published by the Free Software Foundation. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public | |
16 | * License along with this program; if not, write to the | |
660b0411 CM |
17 | * Free Software Foundation, Inc., |
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
9e066e23 CM |
19 | */ |
20 | ||
21 | #ifndef _LINUX_LIST_H | |
22 | #define _LINUX_LIST_H | |
23 | ||
24 | #define LIST_POISON1 ((void *) 0x00100100) | |
25 | #define LIST_POISON2 ((void *) 0x00200200) | |
26 | ||
27 | #undef offsetof | |
28 | #ifdef __compiler_offsetof | |
29 | #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER) | |
30 | #else | |
31 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) | |
32 | #endif | |
33 | ||
34 | #define container_of(ptr, type, member) ({ \ | |
35 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ | |
36 | (type *)( (char *)__mptr - offsetof(type,member) );}) | |
37 | ||
38 | /* | |
39 | * Simple doubly linked list implementation. | |
40 | * | |
41 | * Some of the internal functions ("__xxx") are useful when | |
42 | * manipulating whole lists rather than single entries, as | |
43 | * sometimes we already know the next/prev entries and we can | |
44 | * generate better code by using them directly rather than | |
45 | * using the generic single-entry routines. | |
46 | */ | |
47 | ||
48 | struct list_head { | |
49 | struct list_head *next, *prev; | |
50 | }; | |
51 | ||
52 | #define LIST_HEAD_INIT(name) { &(name), &(name) } | |
53 | ||
54 | #define LIST_HEAD(name) \ | |
55 | struct list_head name = LIST_HEAD_INIT(name) | |
56 | ||
57 | static inline void INIT_LIST_HEAD(struct list_head *list) | |
58 | { | |
59 | list->next = list; | |
60 | list->prev = list; | |
61 | } | |
62 | ||
63 | /* | |
64 | * Insert a new entry between two known consecutive entries. | |
65 | * | |
66 | * This is only for internal list manipulation where we know | |
67 | * the prev/next entries already! | |
68 | */ | |
69 | #ifndef CONFIG_DEBUG_LIST | |
70 | static inline void __list_add(struct list_head *new, | |
71 | struct list_head *prev, | |
72 | struct list_head *next) | |
73 | { | |
74 | next->prev = new; | |
75 | new->next = next; | |
76 | new->prev = prev; | |
77 | prev->next = new; | |
78 | } | |
79 | #else | |
80 | extern void __list_add(struct list_head *new, | |
81 | struct list_head *prev, | |
82 | struct list_head *next); | |
83 | #endif | |
84 | ||
85 | /** | |
86 | * list_add - add a new entry | |
87 | * @new: new entry to be added | |
88 | * @head: list head to add it after | |
89 | * | |
90 | * Insert a new entry after the specified head. | |
91 | * This is good for implementing stacks. | |
92 | */ | |
93 | #ifndef CONFIG_DEBUG_LIST | |
94 | static inline void list_add(struct list_head *new, struct list_head *head) | |
95 | { | |
96 | __list_add(new, head, head->next); | |
97 | } | |
98 | #else | |
99 | extern void list_add(struct list_head *new, struct list_head *head); | |
100 | #endif | |
101 | ||
102 | ||
103 | /** | |
104 | * list_add_tail - add a new entry | |
105 | * @new: new entry to be added | |
106 | * @head: list head to add it before | |
107 | * | |
108 | * Insert a new entry before the specified head. | |
109 | * This is useful for implementing queues. | |
110 | */ | |
111 | static inline void list_add_tail(struct list_head *new, struct list_head *head) | |
112 | { | |
113 | __list_add(new, head->prev, head); | |
114 | } | |
115 | ||
116 | /* | |
117 | * Delete a list entry by making the prev/next entries | |
118 | * point to each other. | |
119 | * | |
120 | * This is only for internal list manipulation where we know | |
121 | * the prev/next entries already! | |
122 | */ | |
123 | static inline void __list_del(struct list_head * prev, struct list_head * next) | |
124 | { | |
125 | next->prev = prev; | |
126 | prev->next = next; | |
127 | } | |
128 | ||
129 | /** | |
130 | * list_del - deletes entry from list. | |
131 | * @entry: the element to delete from the list. | |
132 | * Note: list_empty on entry does not return true after this, the entry is | |
133 | * in an undefined state. | |
134 | */ | |
135 | #ifndef CONFIG_DEBUG_LIST | |
136 | static inline void list_del(struct list_head *entry) | |
137 | { | |
138 | __list_del(entry->prev, entry->next); | |
139 | entry->next = LIST_POISON1; | |
140 | entry->prev = LIST_POISON2; | |
141 | } | |
142 | #else | |
143 | extern void list_del(struct list_head *entry); | |
144 | #endif | |
145 | ||
146 | /** | |
147 | * list_replace - replace old entry by new one | |
148 | * @old : the element to be replaced | |
149 | * @new : the new element to insert | |
150 | * Note: if 'old' was empty, it will be overwritten. | |
151 | */ | |
152 | static inline void list_replace(struct list_head *old, | |
153 | struct list_head *new) | |
154 | { | |
155 | new->next = old->next; | |
156 | new->next->prev = new; | |
157 | new->prev = old->prev; | |
158 | new->prev->next = new; | |
159 | } | |
160 | ||
161 | static inline void list_replace_init(struct list_head *old, | |
162 | struct list_head *new) | |
163 | { | |
164 | list_replace(old, new); | |
165 | INIT_LIST_HEAD(old); | |
166 | } | |
167 | /** | |
168 | * list_del_init - deletes entry from list and reinitialize it. | |
169 | * @entry: the element to delete from the list. | |
170 | */ | |
171 | static inline void list_del_init(struct list_head *entry) | |
172 | { | |
173 | __list_del(entry->prev, entry->next); | |
174 | INIT_LIST_HEAD(entry); | |
175 | } | |
176 | ||
177 | /** | |
178 | * list_move - delete from one list and add as another's head | |
179 | * @list: the entry to move | |
180 | * @head: the head that will precede our entry | |
181 | */ | |
182 | static inline void list_move(struct list_head *list, struct list_head *head) | |
183 | { | |
184 | __list_del(list->prev, list->next); | |
185 | list_add(list, head); | |
186 | } | |
187 | ||
188 | /** | |
189 | * list_move_tail - delete from one list and add as another's tail | |
190 | * @list: the entry to move | |
191 | * @head: the head that will follow our entry | |
192 | */ | |
193 | static inline void list_move_tail(struct list_head *list, | |
194 | struct list_head *head) | |
195 | { | |
196 | __list_del(list->prev, list->next); | |
197 | list_add_tail(list, head); | |
198 | } | |
199 | ||
200 | /** | |
201 | * list_is_last - tests whether @list is the last entry in list @head | |
202 | * @list: the entry to test | |
203 | * @head: the head of the list | |
204 | */ | |
205 | static inline int list_is_last(const struct list_head *list, | |
206 | const struct list_head *head) | |
207 | { | |
208 | return list->next == head; | |
209 | } | |
210 | ||
211 | /** | |
212 | * list_empty - tests whether a list is empty | |
213 | * @head: the list to test. | |
214 | */ | |
215 | static inline int list_empty(const struct list_head *head) | |
216 | { | |
217 | return head->next == head; | |
218 | } | |
219 | ||
220 | /** | |
221 | * list_empty_careful - tests whether a list is empty and not being modified | |
222 | * @head: the list to test | |
223 | * | |
224 | * Description: | |
225 | * tests whether a list is empty _and_ checks that no other CPU might be | |
226 | * in the process of modifying either member (next or prev) | |
227 | * | |
228 | * NOTE: using list_empty_careful() without synchronization | |
229 | * can only be safe if the only activity that can happen | |
230 | * to the list entry is list_del_init(). Eg. it cannot be used | |
231 | * if another CPU could re-list_add() it. | |
232 | */ | |
233 | static inline int list_empty_careful(const struct list_head *head) | |
234 | { | |
235 | struct list_head *next = head->next; | |
236 | return (next == head) && (next == head->prev); | |
237 | } | |
238 | ||
239 | static inline void __list_splice(struct list_head *list, | |
240 | struct list_head *head) | |
241 | { | |
242 | struct list_head *first = list->next; | |
243 | struct list_head *last = list->prev; | |
244 | struct list_head *at = head->next; | |
245 | ||
246 | first->prev = head; | |
247 | head->next = first; | |
248 | ||
249 | last->next = at; | |
250 | at->prev = last; | |
251 | } | |
252 | ||
253 | /** | |
254 | * list_splice - join two lists | |
255 | * @list: the new list to add. | |
256 | * @head: the place to add it in the first list. | |
257 | */ | |
258 | static inline void list_splice(struct list_head *list, struct list_head *head) | |
259 | { | |
260 | if (!list_empty(list)) | |
261 | __list_splice(list, head); | |
262 | } | |
263 | ||
264 | /** | |
265 | * list_splice_init - join two lists and reinitialise the emptied list. | |
266 | * @list: the new list to add. | |
267 | * @head: the place to add it in the first list. | |
268 | * | |
269 | * The list at @list is reinitialised | |
270 | */ | |
271 | static inline void list_splice_init(struct list_head *list, | |
272 | struct list_head *head) | |
273 | { | |
274 | if (!list_empty(list)) { | |
275 | __list_splice(list, head); | |
276 | INIT_LIST_HEAD(list); | |
277 | } | |
278 | } | |
279 | ||
280 | /** | |
281 | * list_entry - get the struct for this entry | |
282 | * @ptr: the &struct list_head pointer. | |
283 | * @type: the type of the struct this is embedded in. | |
284 | * @member: the name of the list_struct within the struct. | |
285 | */ | |
286 | #define list_entry(ptr, type, member) \ | |
287 | container_of(ptr, type, member) | |
288 | ||
289 | /** | |
290 | * list_for_each - iterate over a list | |
291 | * @pos: the &struct list_head to use as a loop cursor. | |
292 | * @head: the head for your list. | |
293 | */ | |
294 | #define list_for_each(pos, head) \ | |
295 | for (pos = (head)->next; pos != (head); \ | |
296 | pos = pos->next) | |
297 | ||
298 | /** | |
299 | * __list_for_each - iterate over a list | |
300 | * @pos: the &struct list_head to use as a loop cursor. | |
301 | * @head: the head for your list. | |
302 | * | |
303 | * This variant differs from list_for_each() in that it's the | |
304 | * simplest possible list iteration code, no prefetching is done. | |
305 | * Use this for code that knows the list to be very short (empty | |
306 | * or 1 entry) most of the time. | |
307 | */ | |
308 | #define __list_for_each(pos, head) \ | |
309 | for (pos = (head)->next; pos != (head); pos = pos->next) | |
310 | ||
311 | /** | |
312 | * list_for_each_prev - iterate over a list backwards | |
313 | * @pos: the &struct list_head to use as a loop cursor. | |
314 | * @head: the head for your list. | |
315 | */ | |
316 | #define list_for_each_prev(pos, head) \ | |
317 | for (pos = (head)->prev; pos != (head); \ | |
318 | pos = pos->prev) | |
319 | ||
320 | /** | |
321 | * list_for_each_safe - iterate over a list safe against removal of list entry | |
322 | * @pos: the &struct list_head to use as a loop cursor. | |
323 | * @n: another &struct list_head to use as temporary storage | |
324 | * @head: the head for your list. | |
325 | */ | |
326 | #define list_for_each_safe(pos, n, head) \ | |
327 | for (pos = (head)->next, n = pos->next; pos != (head); \ | |
328 | pos = n, n = pos->next) | |
329 | ||
330 | /** | |
331 | * list_for_each_entry - iterate over list of given type | |
332 | * @pos: the type * to use as a loop cursor. | |
333 | * @head: the head for your list. | |
334 | * @member: the name of the list_struct within the struct. | |
335 | */ | |
336 | #define list_for_each_entry(pos, head, member) \ | |
337 | for (pos = list_entry((head)->next, typeof(*pos), member); \ | |
338 | &pos->member != (head); \ | |
339 | pos = list_entry(pos->member.next, typeof(*pos), member)) | |
340 | ||
341 | /** | |
342 | * list_for_each_entry_reverse - iterate backwards over list of given type. | |
343 | * @pos: the type * to use as a loop cursor. | |
344 | * @head: the head for your list. | |
345 | * @member: the name of the list_struct within the struct. | |
346 | */ | |
347 | #define list_for_each_entry_reverse(pos, head, member) \ | |
348 | for (pos = list_entry((head)->prev, typeof(*pos), member); \ | |
349 | &pos->member != (head); \ | |
350 | pos = list_entry(pos->member.prev, typeof(*pos), member)) | |
351 | ||
352 | /** | |
353 | * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue | |
354 | * @pos: the type * to use as a start point | |
355 | * @head: the head of the list | |
356 | * @member: the name of the list_struct within the struct. | |
357 | * | |
358 | * Prepares a pos entry for use as a start point in list_for_each_entry_continue. | |
359 | */ | |
360 | #define list_prepare_entry(pos, head, member) \ | |
361 | ((pos) ? : list_entry(head, typeof(*pos), member)) | |
362 | ||
363 | /** | |
364 | * list_for_each_entry_continue - continue iteration over list of given type | |
365 | * @pos: the type * to use as a loop cursor. | |
366 | * @head: the head for your list. | |
367 | * @member: the name of the list_struct within the struct. | |
368 | * | |
369 | * Continue to iterate over list of given type, continuing after | |
370 | * the current position. | |
371 | */ | |
372 | #define list_for_each_entry_continue(pos, head, member) \ | |
373 | for (pos = list_entry(pos->member.next, typeof(*pos), member); \ | |
374 | &pos->member != (head); \ | |
375 | pos = list_entry(pos->member.next, typeof(*pos), member)) | |
376 | ||
377 | /** | |
378 | * list_for_each_entry_from - iterate over list of given type from the current point | |
379 | * @pos: the type * to use as a loop cursor. | |
380 | * @head: the head for your list. | |
381 | * @member: the name of the list_struct within the struct. | |
382 | * | |
383 | * Iterate over list of given type, continuing from current position. | |
384 | */ | |
385 | #define list_for_each_entry_from(pos, head, member) \ | |
386 | for (; &pos->member != (head); \ | |
387 | pos = list_entry(pos->member.next, typeof(*pos), member)) | |
388 | ||
389 | /** | |
390 | * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry | |
391 | * @pos: the type * to use as a loop cursor. | |
392 | * @n: another type * to use as temporary storage | |
393 | * @head: the head for your list. | |
394 | * @member: the name of the list_struct within the struct. | |
395 | */ | |
396 | #define list_for_each_entry_safe(pos, n, head, member) \ | |
397 | for (pos = list_entry((head)->next, typeof(*pos), member), \ | |
398 | n = list_entry(pos->member.next, typeof(*pos), member); \ | |
399 | &pos->member != (head); \ | |
400 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |
401 | ||
402 | /** | |
403 | * list_for_each_entry_safe_continue | |
404 | * @pos: the type * to use as a loop cursor. | |
405 | * @n: another type * to use as temporary storage | |
406 | * @head: the head for your list. | |
407 | * @member: the name of the list_struct within the struct. | |
408 | * | |
409 | * Iterate over list of given type, continuing after current point, | |
410 | * safe against removal of list entry. | |
411 | */ | |
412 | #define list_for_each_entry_safe_continue(pos, n, head, member) \ | |
413 | for (pos = list_entry(pos->member.next, typeof(*pos), member), \ | |
414 | n = list_entry(pos->member.next, typeof(*pos), member); \ | |
415 | &pos->member != (head); \ | |
416 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |
417 | ||
418 | /** | |
419 | * list_for_each_entry_safe_from | |
420 | * @pos: the type * to use as a loop cursor. | |
421 | * @n: another type * to use as temporary storage | |
422 | * @head: the head for your list. | |
423 | * @member: the name of the list_struct within the struct. | |
424 | * | |
425 | * Iterate over list of given type from current point, safe against | |
426 | * removal of list entry. | |
427 | */ | |
428 | #define list_for_each_entry_safe_from(pos, n, head, member) \ | |
429 | for (n = list_entry(pos->member.next, typeof(*pos), member); \ | |
430 | &pos->member != (head); \ | |
431 | pos = n, n = list_entry(n->member.next, typeof(*n), member)) | |
432 | ||
433 | /** | |
434 | * list_for_each_entry_safe_reverse | |
435 | * @pos: the type * to use as a loop cursor. | |
436 | * @n: another type * to use as temporary storage | |
437 | * @head: the head for your list. | |
438 | * @member: the name of the list_struct within the struct. | |
439 | * | |
440 | * Iterate backwards over list of given type, safe against removal | |
441 | * of list entry. | |
442 | */ | |
443 | #define list_for_each_entry_safe_reverse(pos, n, head, member) \ | |
444 | for (pos = list_entry((head)->prev, typeof(*pos), member), \ | |
445 | n = list_entry(pos->member.prev, typeof(*pos), member); \ | |
446 | &pos->member != (head); \ | |
447 | pos = n, n = list_entry(n->member.prev, typeof(*n), member)) | |
448 | ||
449 | #endif |