vmod/vmodttl: fixed bug related to luns not ordered and/or not starting from zero.
[ht-drivers.git] / include / list.h
blob175d2237958474755a671c9b97687ca4c2b75687
1 /**
2 * @file list.h
4 * @brief Unearth list to the user space. Thxs to @e modinit tools!
6 * @author Yury GEORGIEVSKIY
8 * @date long time ago.
10 * Taken from Linux Kernel Source's list.h -- GPL.
12 * @version 1.0 ygeorige 30/06/2008 Creation date.
15 #ifndef _MODINITTOOLS_LIST_H
16 #define _MODINITTOOLS_LIST_H
18 #undef offsetof
19 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
21 /**
22 * container_of - cast a member of a structure out to the containing structure
24 * @ptr: the pointer to the member.
25 * @type: the type of the container struct this is embedded in.
26 * @member: the name of the member within the struct.
29 #define container_of(ptr, type, member) ({ \
30 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
31 (type *)( (char *)__mptr - offsetof(type,member) );})
34 * Simple doubly linked list implementation.
36 * Some of the internal functions ("__xxx") are useful when
37 * manipulating whole lists rather than single entries, as
38 * sometimes we already know the next/prev entries and we can
39 * generate better code by using them directly rather than
40 * using the generic single-entry routines.
43 struct list_head {
44 struct list_head *next, *prev;
47 #define LIST_HEAD_INIT(name) { &(name), &(name) }
49 #define LIST_HEAD(name) \
50 struct list_head name = LIST_HEAD_INIT(name)
52 #define INIT_LIST_HEAD(ptr) do { \
53 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
54 } while (0)
57 * Insert a new entry between two known consecutive entries.
59 * This is only for internal list manipulation where we know
60 * the prev/next entries already!
62 static inline void __list_add(struct list_head *newl,
63 struct list_head *prev,
64 struct list_head *next)
66 next->prev = newl;
67 newl->next = next;
68 newl->prev = prev;
69 prev->next = newl;
72 /**
73 * list_add - add a new entry
74 * @new: new entry to be added
75 * @head: list head to add it after
77 * Insert a new entry after the specified head.
78 * This is good for implementing stacks.
80 static inline void list_add(struct list_head *newl, struct list_head *head)
82 __list_add(newl, head, head->next);
85 /**
86 * list_add_tail - add a new entry
87 * @new: new entry to be added
88 * @head: list head to add it before
90 * Insert a new entry before the specified head.
91 * This is useful for implementing queues.
93 static inline void list_add_tail(struct list_head *newl, struct list_head *head)
95 __list_add(newl, head->prev, head);
99 * Delete a list entry by making the prev/next entries
100 * point to each other.
102 * This is only for internal list manipulation where we know
103 * the prev/next entries already!
105 static inline void __list_del(struct list_head * prev, struct list_head * next)
107 next->prev = prev;
108 prev->next = next;
112 * list_del - deletes entry from list.
113 * @entry: the element to delete from the list.
114 * Note: list_empty on entry does not return true after this, the entry is
115 * in an undefined state.
117 static inline void list_del(struct list_head *entry)
119 __list_del(entry->prev, entry->next);
123 * list_del_init - deletes entry from list and reinitialize it.
124 * @entry: the element to delete from the list.
126 static inline void list_del_init(struct list_head *entry)
128 __list_del(entry->prev, entry->next);
129 INIT_LIST_HEAD(entry);
133 * list_move - delete from one list and add as another's head
134 * @list: the entry to move
135 * @head: the head that will precede our entry
137 static inline void list_move(struct list_head *list, struct list_head *head)
139 __list_del(list->prev, list->next);
140 list_add(list, head);
144 * list_move_tail - delete from one list and add as another's tail
145 * @list: the entry to move
146 * @head: the head that will follow our entry
148 static inline void list_move_tail(struct list_head *list,
149 struct list_head *head)
151 __list_del(list->prev, list->next);
152 list_add_tail(list, head);
156 * list_empty - tests whether a list is empty
157 * @head: the list to test.
159 static inline int list_empty(struct list_head *head)
161 return head->next == head;
164 static inline void __list_splice(struct list_head *list,
165 struct list_head *head)
167 struct list_head *first = list->next;
168 struct list_head *last = list->prev;
169 struct list_head *at = head->next;
171 first->prev = head;
172 head->next = first;
174 last->next = at;
175 at->prev = last;
179 * list_splice - join two lists
180 * @list: the new list to add.
181 * @head: the place to add it in the first list.
183 static inline void list_splice(struct list_head *list, struct list_head *head)
185 if (!list_empty(list))
186 __list_splice(list, head);
190 * list_splice_init - join two lists and reinitialise the emptied list.
191 * @list: the new list to add.
192 * @head: the place to add it in the first list.
194 * The list at @list is reinitialised
196 static inline void list_splice_init(struct list_head *list, struct list_head *head)
198 if (!list_empty(list)) {
199 __list_splice(list, head);
200 INIT_LIST_HEAD(list);
205 * list_entry - get the struct for this entry
206 * @ptr: the &struct list_head pointer.
207 * @type: the type of the struct this is embedded in.
208 * @member: the name of the list_struct within the struct.
210 #define list_entry(ptr, type, member) \
211 container_of(ptr, type, member)
214 * list_for_each - iterate over a list
215 * @pos: the &struct list_head to use as a loop counter.
216 * @head: the head for your list.
218 #define list_for_each(pos, head) \
219 for (pos = (head)->next; pos != (head); pos = pos->next)
222 * list_for_each_prev - iterate over a list backwards
223 * @pos: the &struct list_head to use as a loop counter.
224 * @head: the head for your list.
226 #define list_for_each_prev(pos, head) \
227 for (pos = (head)->prev; pos != (head); pos = pos->prev)
230 * list_for_each_safe - iterate over a list safe against removal of list entry
231 * @pos: the &struct list_head to use as a loop counter.
232 * @n: another &struct list_head to use as temporary storage
233 * @head: the head for your list.
235 #define list_for_each_safe(pos, n, head) \
236 for (pos = (head)->next, n = pos->next; pos != (head); \
237 pos = n, n = pos->next)
240 * list_for_each_entry - iterate over list of given type
241 * @pos: the type * to use as a loop counter.
242 * @head: the head for your list.
243 * @member: the name of the list_struct within the struct.
245 #define list_for_each_entry(pos, head, member) \
246 for (pos = list_entry((head)->next, typeof(*pos), member); \
247 &pos->member != (head); \
248 pos = list_entry(pos->member.next, typeof(*pos), member))
251 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
252 * @pos: the type * to use as a loop cursor.
253 * @n: another type * to use as temporary storage
254 * @head: the head for your list.
255 * @member: the name of the list_struct within the struct.
257 #define list_for_each_entry_safe(pos, n, head, member) \
258 for (pos = list_entry((head)->next, typeof(*pos), member), \
259 n = list_entry(pos->member.next, typeof(*pos), member); \
260 &pos->member != (head); \
261 pos = n, n = list_entry(n->member.next, typeof(*n), member))
263 /* Extra handy list definitions. (see the header for more detailes)
264 In a separate file, because drivers also are using this marcoses */
265 #include "list_extra.h"
267 #endif