HOWTO: grammar/spelling changes
[fio.git] / flist.h
CommitLineData
01743ee1
JA
1#ifndef _LINUX_FLIST_H
2#define _LINUX_FLIST_H
ebac4655 3
79d16311 4#include <stdlib.h>
15e0d945 5#include <stddef.h>
ebac4655
JA
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
01743ee1
JA
21struct flist_head {
22 struct flist_head *next, *prev;
ebac4655
JA
23};
24
01743ee1 25#define FLIST_HEAD_INIT(name) { &(name), &(name) }
ebac4655 26
01743ee1
JA
27#define FLIST_HEAD(name) \
28 struct flist_head name = FLIST_HEAD_INIT(name)
5f350952 29
01743ee1 30#define INIT_FLIST_HEAD(ptr) do { \
ebac4655
JA
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 */
2b52511f 40static inline void __flist_add(struct flist_head *new_entry,
01743ee1
JA
41 struct flist_head *prev,
42 struct flist_head *next)
ebac4655 43{
2b52511f
DR
44 next->prev = new_entry;
45 new_entry->next = next;
46 new_entry->prev = prev;
47 prev->next = new_entry;
ebac4655
JA
48}
49
50/**
01743ee1 51 * flist_add - add a new entry
2b52511f 52 * @new_entry: new entry to be added
ebac4655
JA
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 */
2b52511f
DR
58static inline void flist_add(struct flist_head *new_entry,
59 struct flist_head *head)
ebac4655 60{
2b52511f 61 __flist_add(new_entry, head, head->next);
ebac4655
JA
62}
63
2b52511f 64static inline void flist_add_tail(struct flist_head *new_entry,
01743ee1 65 struct flist_head *head)
ebac4655 66{
2b52511f 67 __flist_add(new_entry, head->prev, head);
ebac4655
JA
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 */
01743ee1
JA
77static inline void __flist_del(struct flist_head *prev,
78 struct flist_head * next)
ebac4655
JA
79{
80 next->prev = prev;
81 prev->next = next;
82}
83
84/**
01743ee1 85 * flist_del - deletes entry from list.
ebac4655 86 * @entry: the element to delete from the list.
01743ee1 87 * Note: flist_empty on entry does not return true after this, the entry is
ebac4655
JA
88 * in an undefined state.
89 */
01743ee1 90static inline void flist_del(struct flist_head *entry)
ebac4655 91{
01743ee1 92 __flist_del(entry->prev, entry->next);
ebac4655
JA
93 entry->next = NULL;
94 entry->prev = NULL;
95}
96
94a08ec6 97/**
01743ee1 98 * flist_del_init - deletes entry from list and reinitialize it.
94a08ec6
JA
99 * @entry: the element to delete from the list.
100 */
01743ee1 101static inline void flist_del_init(struct flist_head *entry)
94a08ec6 102{
01743ee1
JA
103 __flist_del(entry->prev, entry->next);
104 INIT_FLIST_HEAD(entry);
94a08ec6
JA
105}
106
ebac4655 107/**
01743ee1 108 * flist_empty - tests whether a list is empty
ebac4655
JA
109 * @head: the list to test.
110 */
01743ee1 111static inline int flist_empty(const struct flist_head *head)
ebac4655
JA
112{
113 return head->next == head;
114}
115
e53ab27c
JA
116static 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
130static 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
aee2ab67
JA
137static 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
9342d5f8
JA
144static 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
e53ab27c
JA
153static 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
ebac4655 162/**
01743ee1
JA
163 * flist_entry - get the struct for this entry
164 * @ptr: the &struct flist_head pointer.
ebac4655 165 * @type: the type of the struct this is embedded in.
01743ee1 166 * @member: the name of the flist_struct within the struct.
ebac4655 167 */
01743ee1 168#define flist_entry(ptr, type, member) \
ebac4655
JA
169 container_of(ptr, type, member)
170
9342d5f8
JA
171#define flist_first_entry(ptr, type, member) \
172 flist_entry((ptr)->next, type, member)
173
65342a64
JA
174#define flist_last_entry(ptr, type, member) \
175 flist_entry((ptr)->prev, type, member)
176
ebac4655 177/**
01743ee1
JA
178 * flist_for_each - iterate over a list
179 * @pos: the &struct flist_head to use as a loop counter.
ebac4655
JA
180 * @head: the head for your list.
181 */
01743ee1 182#define flist_for_each(pos, head) \
ebac4655
JA
183 for (pos = (head)->next; pos != (head); pos = pos->next)
184
185/**
01743ee1
JA
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
ebac4655
JA
189 * @head: the head for your list.
190 */
01743ee1 191#define flist_for_each_safe(pos, n, head) \
ebac4655
JA
192 for (pos = (head)->next, n = pos->next; pos != (head); \
193 pos = n, n = pos->next)
194
1ae83d45
JA
195extern void flist_sort(void *priv, struct flist_head *head,
196 int (*cmp)(void *priv, struct flist_head *a, struct flist_head *b));
197
ebac4655 198#endif