7 #define container_of(ptr, type, member) ({ \
8 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
9 (type *)( (char *)__mptr - offsetof(type,member) );})
12 * Simple doubly linked list implementation.
14 * Some of the internal functions ("__xxx") are useful when
15 * manipulating whole lists rather than single entries, as
16 * sometimes we already know the next/prev entries and we can
17 * generate better code by using them directly rather than
18 * using the generic single-entry routines.
22 struct flist_head *next, *prev;
25 #define FLIST_HEAD_INIT(name) { &(name), &(name) }
27 #define FLIST_HEAD(name) \
28 struct flist_head name = FLIST_HEAD_INIT(name)
30 #define INIT_FLIST_HEAD(ptr) do { \
31 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
35 * Insert a new entry between two known consecutive entries.
37 * This is only for internal list manipulation where we know
38 * the prev/next entries already!
40 static inline void __flist_add(struct flist_head *new_entry,
41 struct flist_head *prev,
42 struct flist_head *next)
44 next->prev = new_entry;
45 new_entry->next = next;
46 new_entry->prev = prev;
47 prev->next = new_entry;
51 * flist_add - add a new entry
52 * @new_entry: new entry to be added
53 * @head: list head to add it after
55 * Insert a new entry after the specified head.
56 * This is good for implementing stacks.
58 static inline void flist_add(struct flist_head *new_entry,
59 struct flist_head *head)
61 __flist_add(new_entry, head, head->next);
64 static inline void flist_add_tail(struct flist_head *new_entry,
65 struct flist_head *head)
67 __flist_add(new_entry, head->prev, head);
71 * Delete a list entry by making the prev/next entries
72 * point to each other.
74 * This is only for internal list manipulation where we know
75 * the prev/next entries already!
77 static inline void __flist_del(struct flist_head *prev,
78 struct flist_head * next)
85 * flist_del - deletes entry from list.
86 * @entry: the element to delete from the list.
87 * Note: flist_empty on entry does not return true after this, the entry is
88 * in an undefined state.
90 static inline void flist_del(struct flist_head *entry)
92 __flist_del(entry->prev, entry->next);
98 * flist_del_init - deletes entry from list and reinitialize it.
99 * @entry: the element to delete from the list.
101 static inline void flist_del_init(struct flist_head *entry)
103 __flist_del(entry->prev, entry->next);
104 INIT_FLIST_HEAD(entry);
108 * flist_empty - tests whether a list is empty
109 * @head: the list to test.
111 static inline int flist_empty(const struct flist_head *head)
113 return head->next == head;
116 static inline void __flist_splice(const struct flist_head *list,
117 struct flist_head *prev,
118 struct flist_head *next)
120 struct flist_head *first = list->next;
121 struct flist_head *last = list->prev;
130 static inline void flist_splice(const struct flist_head *list,
131 struct flist_head *head)
133 if (!flist_empty(list))
134 __flist_splice(list, head, head->next);
137 static inline void flist_splice_tail(struct flist_head *list,
138 struct flist_head *head)
140 if (!flist_empty(list))
141 __flist_splice(list, head->prev, head);
144 static inline void flist_splice_tail_init(struct flist_head *list,
145 struct flist_head *head)
147 if (!flist_empty(list)) {
148 __flist_splice(list, head->prev, head);
149 INIT_FLIST_HEAD(list);
153 static inline void flist_splice_init(struct flist_head *list,
154 struct flist_head *head)
156 if (!flist_empty(list)) {
157 __flist_splice(list, head, head->next);
158 INIT_FLIST_HEAD(list);
163 * flist_entry - get the struct for this entry
164 * @ptr: the &struct flist_head pointer.
165 * @type: the type of the struct this is embedded in.
166 * @member: the name of the flist_struct within the struct.
168 #define flist_entry(ptr, type, member) \
169 container_of(ptr, type, member)
171 #define flist_first_entry(ptr, type, member) \
172 flist_entry((ptr)->next, type, member)
174 #define flist_last_entry(ptr, type, member) \
175 flist_entry((ptr)->prev, type, member)
178 * flist_for_each - iterate over a list
179 * @pos: the &struct flist_head to use as a loop counter.
180 * @head: the head for your list.
182 #define flist_for_each(pos, head) \
183 for (pos = (head)->next; pos != (head); pos = pos->next)
186 * flist_for_each_safe - iterate over a list safe against removal of list entry
187 * @pos: the &struct flist_head to use as a loop counter.
188 * @n: another &struct flist_head to use as temporary storage
189 * @head: the head for your list.
191 #define flist_for_each_safe(pos, n, head) \
192 for (pos = (head)->next, n = pos->next; pos != (head); \
193 pos = n, n = pos->next)
195 extern void flist_sort(void *priv, struct flist_head *head,
196 int (*cmp)(void *priv, struct flist_head *a, struct flist_head *b));