Merge branch 'szaydel/solaris-Wincompatible-pointer-types' of https://github.com...
[fio.git] / flist.h
1 #ifndef _LINUX_FLIST_H
2 #define _LINUX_FLIST_H
3
4 #include <stdlib.h>
5 #include <stddef.h>
6
7 #define container_of(ptr, type, member)  ({                     \
8         const __typeof__( ((type *)0)->member ) *__mptr = (ptr);        \
9         (type *)( (char *)__mptr - offsetof(type,member) );})
10
11 /*
12  * Simple doubly linked list implementation.
13  *
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.
19  */
20
21 struct flist_head {
22         struct flist_head *next, *prev;
23 };
24
25 #define FLIST_HEAD_INIT(name) { &(name), &(name) }
26
27 #define FLIST_HEAD(name) \
28         struct flist_head name = FLIST_HEAD_INIT(name)
29
30 #define INIT_FLIST_HEAD(ptr) do { \
31         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
32 } while (0)
33
34 /*
35  * Insert a new entry between two known consecutive entries.
36  *
37  * This is only for internal list manipulation where we know
38  * the prev/next entries already!
39  */
40 static inline void __flist_add(struct flist_head *new_entry,
41                                struct flist_head *prev,
42                                struct flist_head *next)
43 {
44         next->prev = new_entry;
45         new_entry->next = next;
46         new_entry->prev = prev;
47         prev->next = new_entry;
48 }
49
50 /**
51  * flist_add - add a new entry
52  * @new_entry: new entry to be added
53  * @head: list head to add it after
54  *
55  * Insert a new entry after the specified head.
56  * This is good for implementing stacks.
57  */
58 static inline void flist_add(struct flist_head *new_entry,
59                              struct flist_head *head)
60 {
61         __flist_add(new_entry, head, head->next);
62 }
63
64 static inline void flist_add_tail(struct flist_head *new_entry,
65                                   struct flist_head *head)
66 {
67         __flist_add(new_entry, head->prev, head);
68 }
69
70 /*
71  * Delete a list entry by making the prev/next entries
72  * point to each other.
73  *
74  * This is only for internal list manipulation where we know
75  * the prev/next entries already!
76  */
77 static inline void __flist_del(struct flist_head *prev,
78                                struct flist_head * next)
79 {
80         next->prev = prev;
81         prev->next = next;
82 }
83
84 /**
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.
89  */
90 static inline void flist_del(struct flist_head *entry)
91 {
92         __flist_del(entry->prev, entry->next);
93         entry->next = NULL;
94         entry->prev = NULL;
95 }
96
97 /**
98  * flist_del_init - deletes entry from list and reinitialize it.
99  * @entry: the element to delete from the list.
100  */
101 static inline void flist_del_init(struct flist_head *entry)
102 {
103         __flist_del(entry->prev, entry->next);
104         INIT_FLIST_HEAD(entry);
105 }
106
107 /**
108  * flist_empty - tests whether a list is empty
109  * @head: the list to test.
110  */
111 static inline int flist_empty(const struct flist_head *head)
112 {
113         return head->next == head;
114 }
115
116 static inline void __flist_splice(const struct flist_head *list,
117                                   struct flist_head *prev,
118                                   struct flist_head *next)
119 {
120         struct flist_head *first = list->next;
121         struct flist_head *last = list->prev;
122
123         first->prev = prev;
124         prev->next = first;
125
126         last->next = next;
127         next->prev = last;
128 }
129
130 static inline void flist_splice(const struct flist_head *list,
131                                 struct flist_head *head)
132 {
133         if (!flist_empty(list))
134                 __flist_splice(list, head, head->next);
135 }
136
137 static inline void flist_splice_tail(struct flist_head *list,
138                                      struct flist_head *head)
139 {
140         if (!flist_empty(list))
141                 __flist_splice(list, head->prev, head);
142 }
143
144 static inline void flist_splice_tail_init(struct flist_head *list,
145                                           struct flist_head *head)
146 {
147         if (!flist_empty(list)) {
148                 __flist_splice(list, head->prev, head);
149                 INIT_FLIST_HEAD(list);
150         }
151 }
152
153 static inline void flist_splice_init(struct flist_head *list,
154                                     struct flist_head *head)
155 {
156         if (!flist_empty(list)) {
157                 __flist_splice(list, head, head->next);
158                 INIT_FLIST_HEAD(list);
159         }
160 }
161
162 /**
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.
167  */
168 #define flist_entry(ptr, type, member) \
169         container_of(ptr, type, member)
170
171 #define flist_first_entry(ptr, type, member) \
172         flist_entry((ptr)->next, type, member)
173
174 #define flist_last_entry(ptr, type, member) \
175         flist_entry((ptr)->prev, type, member)
176
177 /**
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.
181  */
182 #define flist_for_each(pos, head) \
183         for (pos = (head)->next; pos != (head); pos = pos->next)
184
185 /**
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.
190  */
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)
194
195 extern void flist_sort(void *priv, struct flist_head *head,
196         int (*cmp)(void *priv, struct flist_head *a, struct flist_head *b));
197
198 #endif