lib/plist: add helper functions
[deliverable/linux.git] / include / linux / plist.h
CommitLineData
77ba89c5
IM
1/*
2 * Descending-priority-sorted double-linked list
3 *
4 * (C) 2002-2003 Intel Corp
5 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>.
6 *
7 * 2001-2005 (c) MontaVista Software, Inc.
8 * Daniel Walker <dwalker@mvista.com>
9 *
10 * (C) 2005 Thomas Gleixner <tglx@linutronix.de>
11 *
12 * Simplifications of the original code by
13 * Oleg Nesterov <oleg@tv-sign.ru>
14 *
15 * Licensed under the FSF's GNU Public License v2 or later.
16 *
17 * Based on simple lists (include/linux/list.h).
18 *
19 * This is a priority-sorted list of nodes; each node has a
20 * priority from INT_MIN (highest) to INT_MAX (lowest).
21 *
22 * Addition is O(K), removal is O(1), change of priority of a node is
23 * O(K) and K is the number of RT priority levels used in the system.
24 * (1 <= K <= 99)
25 *
26 * This list is really a list of lists:
27 *
28 * - The tier 1 list is the prio_list, different priority nodes.
29 *
30 * - The tier 2 list is the node_list, serialized nodes.
31 *
32 * Simple ASCII art explanation:
33 *
bf6a9b83
LJ
34 * pl:prio_list (only for plist_node)
35 * nl:node_list
36 * HEAD| NODE(S)
37 * |
38 * ||------------------------------------|
39 * ||->|pl|<->|pl|<--------------->|pl|<-|
40 * | |10| |21| |21| |21| |40| (prio)
41 * | | | | | | | | | | |
42 * | | | | | | | | | | |
43 * |->|nl|<->|nl|<->|nl|<->|nl|<->|nl|<->|nl|<-|
44 * |-------------------------------------------|
77ba89c5
IM
45 *
46 * The nodes on the prio_list list are sorted by priority to simplify
47 * the insertion of new nodes. There are no nodes with duplicate
48 * priorites on the list.
49 *
9ca94d7c 50 * The nodes on the node_list are ordered by priority and can contain
77ba89c5
IM
51 * entries which have the same priority. Those entries are ordered
52 * FIFO
53 *
54 * Addition means: look for the prio_list node in the prio_list
55 * for the priority of the node and insert it before the node_list
56 * entry of the next prio_list node. If it is the first node of
57 * that priority, add it to the prio_list in the right position and
58 * insert it into the serialized node_list list
59 *
60 * Removal means remove it from the node_list and remove it from
61 * the prio_list if the node_list list_head is non empty. In case
62 * of removal from the prio_list it must be checked whether other
63 * entries of the same priority are on the list or not. If there
64 * is another entry of the same priority then this entry has to
65 * replace the removed entry on the prio_list. If the entry which
66 * is removed is the only entry of this priority then a simple
67 * remove from both list is sufficient.
68 *
69 * INT_MIN is the highest priority, 0 is the medium highest, INT_MAX
70 * is lowest priority.
71 *
72 * No locking is done, up to the caller.
73 *
74 */
75#ifndef _LINUX_PLIST_H_
76#define _LINUX_PLIST_H_
77
b4459790 78#include <linux/kernel.h>
77ba89c5 79#include <linux/list.h>
77ba89c5
IM
80
81struct plist_head {
77ba89c5 82 struct list_head node_list;
77ba89c5
IM
83};
84
85struct plist_node {
86 int prio;
bf6a9b83
LJ
87 struct list_head prio_list;
88 struct list_head node_list;
77ba89c5
IM
89};
90
77ba89c5 91/**
11265420 92 * PLIST_HEAD_INIT - static struct plist_head initializer
77ba89c5 93 * @head: struct plist_head variable name
a2672459 94 */
732375c6 95#define PLIST_HEAD_INIT(head) \
a2672459 96{ \
732375c6 97 .node_list = LIST_HEAD_INIT((head).node_list) \
a2672459
TG
98}
99
fd16618e
DS
100/**
101 * PLIST_HEAD - declare and init plist_head
102 * @head: name for struct plist_head variable
103 */
104#define PLIST_HEAD(head) \
105 struct plist_head head = PLIST_HEAD_INIT(head)
106
77ba89c5 107/**
11265420 108 * PLIST_NODE_INIT - static struct plist_node initializer
77ba89c5
IM
109 * @node: struct plist_node variable name
110 * @__prio: initial node priority
111 */
112#define PLIST_NODE_INIT(node, __prio) \
113{ \
114 .prio = (__prio), \
bf6a9b83
LJ
115 .prio_list = LIST_HEAD_INIT((node).prio_list), \
116 .node_list = LIST_HEAD_INIT((node).node_list), \
77ba89c5
IM
117}
118
119/**
120 * plist_head_init - dynamic struct plist_head initializer
77ba89c5 121 * @head: &struct plist_head pointer
a2672459
TG
122 */
123static inline void
732375c6 124plist_head_init(struct plist_head *head)
a2672459 125{
a2672459 126 INIT_LIST_HEAD(&head->node_list);
77ba89c5
IM
127}
128
129/**
130 * plist_node_init - Dynamic struct plist_node initializer
77ba89c5
IM
131 * @node: &struct plist_node pointer
132 * @prio: initial node priority
133 */
134static inline void plist_node_init(struct plist_node *node, int prio)
135{
136 node->prio = prio;
bf6a9b83
LJ
137 INIT_LIST_HEAD(&node->prio_list);
138 INIT_LIST_HEAD(&node->node_list);
77ba89c5
IM
139}
140
141extern void plist_add(struct plist_node *node, struct plist_head *head);
142extern void plist_del(struct plist_node *node, struct plist_head *head);
143
144/**
145 * plist_for_each - iterate over the plist
11265420
RD
146 * @pos: the type * to use as a loop counter
147 * @head: the head for your list
77ba89c5
IM
148 */
149#define plist_for_each(pos, head) \
bf6a9b83 150 list_for_each_entry(pos, &(head)->node_list, node_list)
77ba89c5 151
fd16618e
DS
152/**
153 * plist_for_each_continue - continue iteration over the plist
154 * @pos: the type * to use as a loop cursor
155 * @head: the head for your list
156 *
157 * Continue to iterate over plist, continuing after the current position.
158 */
159#define plist_for_each_continue(pos, head) \
160 list_for_each_entry_continue(pos, &(head)->node_list, node_list)
161
77ba89c5 162/**
11265420
RD
163 * plist_for_each_safe - iterate safely over a plist of given type
164 * @pos: the type * to use as a loop counter
165 * @n: another type * to use as temporary storage
166 * @head: the head for your list
77ba89c5 167 *
11265420 168 * Iterate over a plist of given type, safe against removal of list entry.
77ba89c5
IM
169 */
170#define plist_for_each_safe(pos, n, head) \
bf6a9b83 171 list_for_each_entry_safe(pos, n, &(head)->node_list, node_list)
77ba89c5
IM
172
173/**
174 * plist_for_each_entry - iterate over list of given type
11265420
RD
175 * @pos: the type * to use as a loop counter
176 * @head: the head for your list
177 * @mem: the name of the list_struct within the struct
77ba89c5
IM
178 */
179#define plist_for_each_entry(pos, head, mem) \
bf6a9b83 180 list_for_each_entry(pos, &(head)->node_list, mem.node_list)
77ba89c5 181
fd16618e
DS
182/**
183 * plist_for_each_entry_continue - continue iteration over list of given type
184 * @pos: the type * to use as a loop cursor
185 * @head: the head for your list
186 * @m: the name of the list_struct within the struct
187 *
188 * Continue to iterate over list of given type, continuing after
189 * the current position.
190 */
191#define plist_for_each_entry_continue(pos, head, m) \
192 list_for_each_entry_continue(pos, &(head)->node_list, m.node_list)
193
77ba89c5 194/**
11265420
RD
195 * plist_for_each_entry_safe - iterate safely over list of given type
196 * @pos: the type * to use as a loop counter
77ba89c5 197 * @n: another type * to use as temporary storage
11265420
RD
198 * @head: the head for your list
199 * @m: the name of the list_struct within the struct
200 *
201 * Iterate over list of given type, safe against removal of list entry.
77ba89c5
IM
202 */
203#define plist_for_each_entry_safe(pos, n, head, m) \
bf6a9b83 204 list_for_each_entry_safe(pos, n, &(head)->node_list, m.node_list)
77ba89c5
IM
205
206/**
207 * plist_head_empty - return !0 if a plist_head is empty
77ba89c5
IM
208 * @head: &struct plist_head pointer
209 */
210static inline int plist_head_empty(const struct plist_head *head)
211{
212 return list_empty(&head->node_list);
213}
214
215/**
216 * plist_node_empty - return !0 if plist_node is not on a list
77ba89c5
IM
217 * @node: &struct plist_node pointer
218 */
219static inline int plist_node_empty(const struct plist_node *node)
220{
bf6a9b83 221 return list_empty(&node->node_list);
77ba89c5
IM
222}
223
224/* All functions below assume the plist_head is not empty. */
225
226/**
227 * plist_first_entry - get the struct for the first entry
11265420
RD
228 * @head: the &struct plist_head pointer
229 * @type: the type of the struct this is embedded in
230 * @member: the name of the list_struct within the struct
77ba89c5
IM
231 */
232#ifdef CONFIG_DEBUG_PI_LIST
233# define plist_first_entry(head, type, member) \
234({ \
235 WARN_ON(plist_head_empty(head)); \
236 container_of(plist_first(head), type, member); \
237})
238#else
239# define plist_first_entry(head, type, member) \
240 container_of(plist_first(head), type, member)
241#endif
242
12e4d0cc
JB
243/**
244 * plist_last_entry - get the struct for the last entry
245 * @head: the &struct plist_head pointer
246 * @type: the type of the struct this is embedded in
247 * @member: the name of the list_struct within the struct
248 */
249#ifdef CONFIG_DEBUG_PI_LIST
250# define plist_last_entry(head, type, member) \
251({ \
252 WARN_ON(plist_head_empty(head)); \
253 container_of(plist_last(head), type, member); \
254})
255#else
256# define plist_last_entry(head, type, member) \
257 container_of(plist_last(head), type, member)
258#endif
259
fd16618e
DS
260/**
261 * plist_next - get the next entry in list
262 * @pos: the type * to cursor
263 */
264#define plist_next(pos) \
265 list_next_entry(pos, node_list)
266
267/**
268 * plist_prev - get the prev entry in list
269 * @pos: the type * to cursor
270 */
271#define plist_prev(pos) \
272 list_prev_entry(pos, node_list)
273
77ba89c5
IM
274/**
275 * plist_first - return the first node (and thus, highest priority)
77ba89c5
IM
276 * @head: the &struct plist_head pointer
277 *
278 * Assumes the plist is _not_ empty.
279 */
9ca94d7c 280static inline struct plist_node *plist_first(const struct plist_head *head)
77ba89c5
IM
281{
282 return list_entry(head->node_list.next,
bf6a9b83 283 struct plist_node, node_list);
77ba89c5
IM
284}
285
12e4d0cc
JB
286/**
287 * plist_last - return the last node (and thus, lowest priority)
288 * @head: the &struct plist_head pointer
289 *
290 * Assumes the plist is _not_ empty.
291 */
292static inline struct plist_node *plist_last(const struct plist_head *head)
293{
294 return list_entry(head->node_list.prev,
bf6a9b83 295 struct plist_node, node_list);
12e4d0cc
JB
296}
297
77ba89c5 298#endif
This page took 0.863743 seconds and 5 git commands to generate.