Commit | Line | Data |
---|---|---|
01743ee1 JA |
1 | #ifndef _LINUX_FLIST_H |
2 | #define _LINUX_FLIST_H | |
ebac4655 JA |
3 | |
4 | #undef offsetof | |
5 | #ifdef __compiler_offsetof | |
6 | #define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER) | |
7 | #else | |
8 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) | |
9 | #endif | |
10 | ||
11 | #define container_of(ptr, type, member) ({ \ | |
12 | const typeof( ((type *)0)->member ) *__mptr = (ptr); \ | |
13 | (type *)( (char *)__mptr - offsetof(type,member) );}) | |
14 | ||
15 | /* | |
16 | * Simple doubly linked list implementation. | |
17 | * | |
18 | * Some of the internal functions ("__xxx") are useful when | |
19 | * manipulating whole lists rather than single entries, as | |
20 | * sometimes we already know the next/prev entries and we can | |
21 | * generate better code by using them directly rather than | |
22 | * using the generic single-entry routines. | |
23 | */ | |
24 | ||
01743ee1 JA |
25 | struct flist_head { |
26 | struct flist_head *next, *prev; | |
ebac4655 JA |
27 | }; |
28 | ||
01743ee1 | 29 | #define FLIST_HEAD_INIT(name) { &(name), &(name) } |
ebac4655 | 30 | |
01743ee1 JA |
31 | #define FLIST_HEAD(name) \ |
32 | struct flist_head name = FLIST_HEAD_INIT(name) | |
5f350952 | 33 | |
01743ee1 | 34 | #define INIT_FLIST_HEAD(ptr) do { \ |
ebac4655 JA |
35 | (ptr)->next = (ptr); (ptr)->prev = (ptr); \ |
36 | } while (0) | |
37 | ||
38 | /* | |
39 | * Insert a new entry between two known consecutive entries. | |
40 | * | |
41 | * This is only for internal list manipulation where we know | |
42 | * the prev/next entries already! | |
43 | */ | |
2b52511f | 44 | static inline void __flist_add(struct flist_head *new_entry, |
01743ee1 JA |
45 | struct flist_head *prev, |
46 | struct flist_head *next) | |
ebac4655 | 47 | { |
2b52511f DR |
48 | next->prev = new_entry; |
49 | new_entry->next = next; | |
50 | new_entry->prev = prev; | |
51 | prev->next = new_entry; | |
ebac4655 JA |
52 | } |
53 | ||
54 | /** | |
01743ee1 | 55 | * flist_add - add a new entry |
2b52511f | 56 | * @new_entry: new entry to be added |
ebac4655 JA |
57 | * @head: list head to add it after |
58 | * | |
59 | * Insert a new entry after the specified head. | |
60 | * This is good for implementing stacks. | |
61 | */ | |
2b52511f DR |
62 | static inline void flist_add(struct flist_head *new_entry, |
63 | struct flist_head *head) | |
ebac4655 | 64 | { |
2b52511f | 65 | __flist_add(new_entry, head, head->next); |
ebac4655 JA |
66 | } |
67 | ||
2b52511f | 68 | static inline void flist_add_tail(struct flist_head *new_entry, |
01743ee1 | 69 | struct flist_head *head) |
ebac4655 | 70 | { |
2b52511f | 71 | __flist_add(new_entry, head->prev, head); |
ebac4655 JA |
72 | } |
73 | ||
74 | /* | |
75 | * Delete a list entry by making the prev/next entries | |
76 | * point to each other. | |
77 | * | |
78 | * This is only for internal list manipulation where we know | |
79 | * the prev/next entries already! | |
80 | */ | |
01743ee1 JA |
81 | static inline void __flist_del(struct flist_head *prev, |
82 | struct flist_head * next) | |
ebac4655 JA |
83 | { |
84 | next->prev = prev; | |
85 | prev->next = next; | |
86 | } | |
87 | ||
88 | /** | |
01743ee1 | 89 | * flist_del - deletes entry from list. |
ebac4655 | 90 | * @entry: the element to delete from the list. |
01743ee1 | 91 | * Note: flist_empty on entry does not return true after this, the entry is |
ebac4655 JA |
92 | * in an undefined state. |
93 | */ | |
01743ee1 | 94 | static inline void flist_del(struct flist_head *entry) |
ebac4655 | 95 | { |
01743ee1 | 96 | __flist_del(entry->prev, entry->next); |
ebac4655 JA |
97 | entry->next = NULL; |
98 | entry->prev = NULL; | |
99 | } | |
100 | ||
94a08ec6 | 101 | /** |
01743ee1 | 102 | * flist_del_init - deletes entry from list and reinitialize it. |
94a08ec6 JA |
103 | * @entry: the element to delete from the list. |
104 | */ | |
01743ee1 | 105 | static inline void flist_del_init(struct flist_head *entry) |
94a08ec6 | 106 | { |
01743ee1 JA |
107 | __flist_del(entry->prev, entry->next); |
108 | INIT_FLIST_HEAD(entry); | |
94a08ec6 JA |
109 | } |
110 | ||
ebac4655 | 111 | /** |
01743ee1 | 112 | * flist_empty - tests whether a list is empty |
ebac4655 JA |
113 | * @head: the list to test. |
114 | */ | |
01743ee1 | 115 | static inline int flist_empty(const struct flist_head *head) |
ebac4655 JA |
116 | { |
117 | return head->next == head; | |
118 | } | |
119 | ||
e53ab27c JA |
120 | static inline void __flist_splice(const struct flist_head *list, |
121 | struct flist_head *prev, | |
122 | struct flist_head *next) | |
123 | { | |
124 | struct flist_head *first = list->next; | |
125 | struct flist_head *last = list->prev; | |
126 | ||
127 | first->prev = prev; | |
128 | prev->next = first; | |
129 | ||
130 | last->next = next; | |
131 | next->prev = last; | |
132 | } | |
133 | ||
134 | static inline void flist_splice(const struct flist_head *list, | |
135 | struct flist_head *head) | |
136 | { | |
137 | if (!flist_empty(list)) | |
138 | __flist_splice(list, head, head->next); | |
139 | } | |
140 | ||
141 | static inline void flist_splice_init(struct flist_head *list, | |
142 | struct flist_head *head) | |
143 | { | |
144 | if (!flist_empty(list)) { | |
145 | __flist_splice(list, head, head->next); | |
146 | INIT_FLIST_HEAD(list); | |
147 | } | |
148 | } | |
149 | ||
ebac4655 | 150 | /** |
01743ee1 JA |
151 | * flist_entry - get the struct for this entry |
152 | * @ptr: the &struct flist_head pointer. | |
ebac4655 | 153 | * @type: the type of the struct this is embedded in. |
01743ee1 | 154 | * @member: the name of the flist_struct within the struct. |
ebac4655 | 155 | */ |
01743ee1 | 156 | #define flist_entry(ptr, type, member) \ |
ebac4655 JA |
157 | container_of(ptr, type, member) |
158 | ||
159 | /** | |
01743ee1 JA |
160 | * flist_for_each - iterate over a list |
161 | * @pos: the &struct flist_head to use as a loop counter. | |
ebac4655 JA |
162 | * @head: the head for your list. |
163 | */ | |
01743ee1 | 164 | #define flist_for_each(pos, head) \ |
ebac4655 JA |
165 | for (pos = (head)->next; pos != (head); pos = pos->next) |
166 | ||
167 | /** | |
01743ee1 JA |
168 | * flist_for_each_safe - iterate over a list safe against removal of list entry |
169 | * @pos: the &struct flist_head to use as a loop counter. | |
170 | * @n: another &struct flist_head to use as temporary storage | |
ebac4655 JA |
171 | * @head: the head for your list. |
172 | */ | |
01743ee1 | 173 | #define flist_for_each_safe(pos, n, head) \ |
ebac4655 JA |
174 | for (pos = (head)->next, n = pos->next; pos != (head); \ |
175 | pos = n, n = pos->next) | |
176 | ||
177 | #endif |