2 * Copyright (C) 2012 Fusion-io. All rights reserved.
4 * This header was taken from the Linux kernel
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.
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.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 021110-1307, USA.
24 #define LIST_POISON1 ((void *) 0x00100100)
25 #define LIST_POISON2 ((void *) 0x00200200)
28 #ifdef __compiler_offsetof
29 #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
31 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
34 #define container_of(ptr, type, member) ({ \
35 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
36 (type *)( (char *)__mptr - offsetof(type,member) );})
39 * Simple doubly linked list implementation.
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.
49 struct list_head *next, *prev;
52 #define LIST_HEAD_INIT(name) { &(name), &(name) }
54 #define LIST_HEAD(name) \
55 struct list_head name = LIST_HEAD_INIT(name)
57 static inline void INIT_LIST_HEAD(struct list_head *list)
64 * Insert a new entry between two known consecutive entries.
66 * This is only for internal list manipulation where we know
67 * the prev/next entries already!
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)
80 extern void __list_add(struct list_head *new,
81 struct list_head *prev,
82 struct list_head *next);
86 * list_add - add a new entry
87 * @new: new entry to be added
88 * @head: list head to add it after
90 * Insert a new entry after the specified head.
91 * This is good for implementing stacks.
93 #ifndef CONFIG_DEBUG_LIST
94 static inline void list_add(struct list_head *new, struct list_head *head)
96 __list_add(new, head, head->next);
99 extern void list_add(struct list_head *new, struct list_head *head);
104 * list_add_tail - add a new entry
105 * @new: new entry to be added
106 * @head: list head to add it before
108 * Insert a new entry before the specified head.
109 * This is useful for implementing queues.
111 static inline void list_add_tail(struct list_head *new, struct list_head *head)
113 __list_add(new, head->prev, head);
117 * Delete a list entry by making the prev/next entries
118 * point to each other.
120 * This is only for internal list manipulation where we know
121 * the prev/next entries already!
123 static inline void __list_del(struct list_head * prev, struct list_head * next)
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.
135 #ifndef CONFIG_DEBUG_LIST
136 static inline void list_del(struct list_head *entry)
138 __list_del(entry->prev, entry->next);
139 entry->next = LIST_POISON1;
140 entry->prev = LIST_POISON2;
143 extern void list_del(struct list_head *entry);
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.
152 static inline void list_replace(struct list_head *old,
153 struct list_head *new)
155 new->next = old->next;
156 new->next->prev = new;
157 new->prev = old->prev;
158 new->prev->next = new;
161 static inline void list_replace_init(struct list_head *old,
162 struct list_head *new)
164 list_replace(old, new);
168 * list_del_init - deletes entry from list and reinitialize it.
169 * @entry: the element to delete from the list.
171 static inline void list_del_init(struct list_head *entry)
173 __list_del(entry->prev, entry->next);
174 INIT_LIST_HEAD(entry);
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
182 static inline void list_move(struct list_head *list, struct list_head *head)
184 __list_del(list->prev, list->next);
185 list_add(list, head);
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
193 static inline void list_move_tail(struct list_head *list,
194 struct list_head *head)
196 __list_del(list->prev, list->next);
197 list_add_tail(list, head);
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
205 static inline int list_is_last(const struct list_head *list,
206 const struct list_head *head)
208 return list->next == head;
212 * list_empty - tests whether a list is empty
213 * @head: the list to test.
215 static inline int list_empty(const struct list_head *head)
217 return head->next == head;
221 * list_empty_careful - tests whether a list is empty and not being modified
222 * @head: the list to test
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)
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.
233 static inline int list_empty_careful(const struct list_head *head)
235 struct list_head *next = head->next;
236 return (next == head) && (next == head->prev);
239 static inline void __list_splice(struct list_head *list,
240 struct list_head *head)
242 struct list_head *first = list->next;
243 struct list_head *last = list->prev;
244 struct list_head *at = head->next;
254 * list_splice - join two lists
255 * @list: the new list to add.
256 * @head: the place to add it in the first list.
258 static inline void list_splice(struct list_head *list, struct list_head *head)
260 if (!list_empty(list))
261 __list_splice(list, head);
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.
269 * The list at @list is reinitialised
271 static inline void list_splice_init(struct list_head *list,
272 struct list_head *head)
274 if (!list_empty(list)) {
275 __list_splice(list, head);
276 INIT_LIST_HEAD(list);
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.
286 #define list_entry(ptr, type, member) \
287 container_of(ptr, type, member)
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.
294 #define list_for_each(pos, head) \
295 for (pos = (head)->next; pos != (head); \
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.
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.
308 #define __list_for_each(pos, head) \
309 for (pos = (head)->next; pos != (head); pos = pos->next)
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.
316 #define list_for_each_prev(pos, head) \
317 for (pos = (head)->prev; pos != (head); \
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.
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)
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.
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))
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.
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))
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.
358 * Prepares a pos entry for use as a start point in list_for_each_entry_continue.
360 #define list_prepare_entry(pos, head, member) \
361 ((pos) ? : list_entry(head, typeof(*pos), member))
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.
369 * Continue to iterate over list of given type, continuing after
370 * the current position.
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))
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.
383 * Iterate over list of given type, continuing from current position.
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))
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.
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))
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.
409 * Iterate over list of given type, continuing after current point,
410 * safe against removal of list entry.
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))
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.
425 * Iterate over list of given type from current point, safe against
426 * removal of list entry.
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))
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.
440 * Iterate backwards over list of given type, safe against removal
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))