3 #include <linux/kernel.h>
4 #include <linux/module.h>
5 #include <linux/slab.h>
7 #include <linux/compat.h>
11 #include <linux/list.h>
12 #include <linux/list_sort.h>
14 #define MAX_LIST_LENGTH_BITS 20
17 * Returns a list organized in an intermediate format suited
18 * to chaining of merge() calls: null-terminated, no reserved or
19 * sentinel head node, "prev" links not maintained.
21 static struct list_head *merge(void *priv,
22 int (*cmp)(void *priv, struct list_head *a,
24 struct list_head *a, struct list_head *b)
26 struct list_head head, *tail = &head;
29 /* if equal, take 'a' -- important for sort stability */
30 if ((*cmp)(priv, a, b) <= 0) {
44 * Combine final list merge with restoration of standard doubly-linked
45 * list structure. This approach duplicates code from merge(), but
46 * runs faster than the tidier alternatives of either a separate final
47 * prev-link restoration pass, or maintaining the prev links
50 static void merge_and_restore_back_links(void *priv,
51 int (*cmp)(void *priv, struct list_head *a,
53 struct list_head *head,
54 struct list_head *a, struct list_head *b)
56 struct list_head *tail = head;
59 /* if equal, take 'a' -- important for sort stability */
60 if ((*cmp)(priv, a, b) <= 0) {
75 * In worst cases this loop may run many iterations.
76 * Continue callbacks to the client even though no
77 * element comparison is needed, so the client's cmp()
78 * routine can invoke cond_resched() periodically.
80 (*cmp)(priv, tail->next, tail->next);
82 tail->next->prev = tail;
91 * list_sort - sort a list
92 * @priv: private data, opaque to list_sort(), passed to @cmp
93 * @head: the list to sort
94 * @cmp: the elements comparison function
96 * This function implements "merge sort", which has O(nlog(n))
99 * The comparison function @cmp must return a negative value if @a
100 * should sort before @b, and a positive value if @a should sort after
101 * @b. If @a and @b are equivalent, and their original relative
102 * ordering is to be preserved, @cmp must return 0.
104 void list_sort(void *priv, struct list_head *head,
105 int (*cmp)(void *priv, struct list_head *a,
106 struct list_head *b))
108 struct list_head *part[MAX_LIST_LENGTH_BITS+1]; /* sorted partial lists
109 -- last slot is a sentinel */
110 int lev; /* index into part[] */
112 struct list_head *list;
114 if (list_empty(head))
117 memset(part, 0, sizeof(part));
119 head->prev->next = NULL;
123 struct list_head *cur = list;
127 for (lev = 0; part[lev]; lev++) {
128 cur = merge(priv, cmp, part[lev], cur);
132 if (unlikely(lev >= ARRAY_SIZE(part)-1)) {
133 printk_once(KERN_DEBUG "list passed to"
134 " list_sort() too long for"
143 for (lev = 0; lev < max_lev; lev++)
145 list = merge(priv, cmp, part[lev], list);
147 merge_and_restore_back_links(priv, cmp, head, part[max_lev], list);
149 EXPORT_SYMBOL(list_sort);
151 #ifdef CONFIG_TEST_LIST_SORT
153 #include <linux/random.h>
156 * The pattern of set bits in the list length determines which cases
157 * are hit in list_sort().
159 #define TEST_LIST_LEN (512+128+2) /* not including head */
161 #define TEST_POISON1 0xDEADBEEF
162 #define TEST_POISON2 0xA324354C
165 unsigned int poison1;
166 struct list_head list;
167 unsigned int poison2;
172 /* Array, containing pointers to all elements in the test list */
173 static struct debug_el **elts __initdata;
175 static int __init check(struct debug_el *ela, struct debug_el *elb)
177 if (ela->serial >= TEST_LIST_LEN) {
178 printk(KERN_ERR "list_sort_test: error: incorrect serial %d\n",
182 if (elb->serial >= TEST_LIST_LEN) {
183 printk(KERN_ERR "list_sort_test: error: incorrect serial %d\n",
187 if (elts[ela->serial] != ela || elts[elb->serial] != elb) {
188 printk(KERN_ERR "list_sort_test: error: phantom element\n");
191 if (ela->poison1 != TEST_POISON1 || ela->poison2 != TEST_POISON2) {
192 printk(KERN_ERR "list_sort_test: error: bad poison: %#x/%#x\n",
193 ela->poison1, ela->poison2);
196 if (elb->poison1 != TEST_POISON1 || elb->poison2 != TEST_POISON2) {
197 printk(KERN_ERR "list_sort_test: error: bad poison: %#x/%#x\n",
198 elb->poison1, elb->poison2);
204 static int __init cmp(void *priv, struct list_head *a, struct list_head *b)
206 struct debug_el *ela, *elb;
208 ela = container_of(a, struct debug_el, list);
209 elb = container_of(b, struct debug_el, list);
212 return ela->value - elb->value;
215 static int __init list_sort_test(void)
217 int i, count = 1, err = -EINVAL;
219 struct list_head *cur, *tmp;
222 printk(KERN_DEBUG "list_sort_test: start testing list_sort()\n");
224 elts = kmalloc(sizeof(void *) * TEST_LIST_LEN, GFP_KERNEL);
226 printk(KERN_ERR "list_sort_test: error: cannot allocate "
231 for (i = 0; i < TEST_LIST_LEN; i++) {
232 el = kmalloc(sizeof(*el), GFP_KERNEL);
234 printk(KERN_ERR "list_sort_test: error: cannot "
235 "allocate memory\n");
238 /* force some equivalencies */
239 el->value = prandom_u32() % (TEST_LIST_LEN / 3);
241 el->poison1 = TEST_POISON1;
242 el->poison2 = TEST_POISON2;
244 list_add_tail(&el->list, &head);
247 list_sort(NULL, &head, cmp);
249 for (cur = head.next; cur->next != &head; cur = cur->next) {
250 struct debug_el *el1;
253 if (cur->next->prev != cur) {
254 printk(KERN_ERR "list_sort_test: error: list is "
259 cmp_result = cmp(NULL, cur, cur->next);
260 if (cmp_result > 0) {
261 printk(KERN_ERR "list_sort_test: error: list is not "
266 el = container_of(cur, struct debug_el, list);
267 el1 = container_of(cur->next, struct debug_el, list);
268 if (cmp_result == 0 && el->serial >= el1->serial) {
269 printk(KERN_ERR "list_sort_test: error: order of "
270 "equivalent elements not preserved\n");
274 if (check(el, el1)) {
275 printk(KERN_ERR "list_sort_test: error: element check "
282 if (count != TEST_LIST_LEN) {
283 printk(KERN_ERR "list_sort_test: error: bad list length %d",
291 list_for_each_safe(cur, tmp, &head) {
293 kfree(container_of(cur, struct debug_el, list));
297 module_init(list_sort_test);
298 #endif /* CONFIG_TEST_LIST_SORT */