Merge branch 'next' of git://selinuxproject.org/~jmorris/linux-security
[linux-btrfs-devel.git] / lib / plist.c
bloba0a4da489c22598748d7cea6da7e444d3b95f579
1 /*
2 * lib/plist.c
4 * Descending-priority-sorted double-linked list
6 * (C) 2002-2003 Intel Corp
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>.
9 * 2001-2005 (c) MontaVista Software, Inc.
10 * Daniel Walker <dwalker@mvista.com>
12 * (C) 2005 Thomas Gleixner <tglx@linutronix.de>
14 * Simplifications of the original code by
15 * Oleg Nesterov <oleg@tv-sign.ru>
17 * Licensed under the FSF's GNU Public License v2 or later.
19 * Based on simple lists (include/linux/list.h).
21 * This file contains the add / del functions which are considered to
22 * be too large to inline. See include/linux/plist.h for further
23 * information.
26 #include <linux/plist.h>
27 #include <linux/spinlock.h>
29 #ifdef CONFIG_DEBUG_PI_LIST
31 static struct plist_head test_head;
33 static void plist_check_prev_next(struct list_head *t, struct list_head *p,
34 struct list_head *n)
36 WARN(n->prev != p || p->next != n,
37 "top: %p, n: %p, p: %p\n"
38 "prev: %p, n: %p, p: %p\n"
39 "next: %p, n: %p, p: %p\n",
40 t, t->next, t->prev,
41 p, p->next, p->prev,
42 n, n->next, n->prev);
45 static void plist_check_list(struct list_head *top)
47 struct list_head *prev = top, *next = top->next;
49 plist_check_prev_next(top, prev, next);
50 while (next != top) {
51 prev = next;
52 next = prev->next;
53 plist_check_prev_next(top, prev, next);
57 static void plist_check_head(struct plist_head *head)
59 if (!plist_head_empty(head))
60 plist_check_list(&plist_first(head)->prio_list);
61 plist_check_list(&head->node_list);
64 #else
65 # define plist_check_head(h) do { } while (0)
66 #endif
68 /**
69 * plist_add - add @node to @head
71 * @node: &struct plist_node pointer
72 * @head: &struct plist_head pointer
74 void plist_add(struct plist_node *node, struct plist_head *head)
76 struct plist_node *first, *iter, *prev = NULL;
77 struct list_head *node_next = &head->node_list;
79 plist_check_head(head);
80 WARN_ON(!plist_node_empty(node));
81 WARN_ON(!list_empty(&node->prio_list));
83 if (plist_head_empty(head))
84 goto ins_node;
86 first = iter = plist_first(head);
88 do {
89 if (node->prio < iter->prio) {
90 node_next = &iter->node_list;
91 break;
94 prev = iter;
95 iter = list_entry(iter->prio_list.next,
96 struct plist_node, prio_list);
97 } while (iter != first);
99 if (!prev || prev->prio != node->prio)
100 list_add_tail(&node->prio_list, &iter->prio_list);
101 ins_node:
102 list_add_tail(&node->node_list, node_next);
104 plist_check_head(head);
108 * plist_del - Remove a @node from plist.
110 * @node: &struct plist_node pointer - entry to be removed
111 * @head: &struct plist_head pointer - list head
113 void plist_del(struct plist_node *node, struct plist_head *head)
115 plist_check_head(head);
117 if (!list_empty(&node->prio_list)) {
118 if (node->node_list.next != &head->node_list) {
119 struct plist_node *next;
121 next = list_entry(node->node_list.next,
122 struct plist_node, node_list);
124 /* add the next plist_node into prio_list */
125 if (list_empty(&next->prio_list))
126 list_add(&next->prio_list, &node->prio_list);
128 list_del_init(&node->prio_list);
131 list_del_init(&node->node_list);
133 plist_check_head(head);
136 #ifdef CONFIG_DEBUG_PI_LIST
137 #include <linux/sched.h>
138 #include <linux/module.h>
139 #include <linux/init.h>
141 static struct plist_node __initdata test_node[241];
143 static void __init plist_test_check(int nr_expect)
145 struct plist_node *first, *prio_pos, *node_pos;
147 if (plist_head_empty(&test_head)) {
148 BUG_ON(nr_expect != 0);
149 return;
152 prio_pos = first = plist_first(&test_head);
153 plist_for_each(node_pos, &test_head) {
154 if (nr_expect-- < 0)
155 break;
156 if (node_pos == first)
157 continue;
158 if (node_pos->prio == prio_pos->prio) {
159 BUG_ON(!list_empty(&node_pos->prio_list));
160 continue;
163 BUG_ON(prio_pos->prio > node_pos->prio);
164 BUG_ON(prio_pos->prio_list.next != &node_pos->prio_list);
165 prio_pos = node_pos;
168 BUG_ON(nr_expect != 0);
169 BUG_ON(prio_pos->prio_list.next != &first->prio_list);
172 static int __init plist_test(void)
174 int nr_expect = 0, i, loop;
175 unsigned int r = local_clock();
177 printk(KERN_INFO "start plist test\n");
178 plist_head_init(&test_head);
179 for (i = 0; i < ARRAY_SIZE(test_node); i++)
180 plist_node_init(test_node + i, 0);
182 for (loop = 0; loop < 1000; loop++) {
183 r = r * 193939 % 47629;
184 i = r % ARRAY_SIZE(test_node);
185 if (plist_node_empty(test_node + i)) {
186 r = r * 193939 % 47629;
187 test_node[i].prio = r % 99;
188 plist_add(test_node + i, &test_head);
189 nr_expect++;
190 } else {
191 plist_del(test_node + i, &test_head);
192 nr_expect--;
194 plist_test_check(nr_expect);
197 for (i = 0; i < ARRAY_SIZE(test_node); i++) {
198 if (plist_node_empty(test_node + i))
199 continue;
200 plist_del(test_node + i, &test_head);
201 nr_expect--;
202 plist_test_check(nr_expect);
205 printk(KERN_INFO "end plist test\n");
206 return 0;
209 module_init(plist_test);
211 #endif