4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/slab.h>
8 #include <linux/compat.h>
11 #include <linux/printk.h>
13 #include <linux/list.h>
14 #include <linux/list_sort.h>
16 #define MAX_LIST_LENGTH_BITS 20
19 * Returns a list organized in an intermediate format suited
20 * to chaining of merge() calls: null-terminated, no reserved or
21 * sentinel head node, "prev" links not maintained.
23 static struct list_head *merge(void *priv,
24 int (*cmp)(void *priv, struct list_head *a,
26 struct list_head *a, struct list_head *b)
28 struct list_head head, *tail = &head;
31 /* if equal, take 'a' -- important for sort stability */
32 if ((*cmp)(priv, a, b) <= 0) {
46 * Combine final list merge with restoration of standard doubly-linked
47 * list structure. This approach duplicates code from merge(), but
48 * runs faster than the tidier alternatives of either a separate final
49 * prev-link restoration pass, or maintaining the prev links
52 static void merge_and_restore_back_links(void *priv,
53 int (*cmp)(void *priv, struct list_head *a,
55 struct list_head *head,
56 struct list_head *a, struct list_head *b)
58 struct list_head *tail = head;
61 /* if equal, take 'a' -- important for sort stability */
62 if ((*cmp)(priv, a, b) <= 0) {
77 * In worst cases this loop may run many iterations.
78 * Continue callbacks to the client even though no
79 * element comparison is needed, so the client's cmp()
80 * routine can invoke cond_resched() periodically.
82 (*cmp)(priv, tail->next, tail->next);
84 tail->next->prev = tail;
93 * list_sort - sort a list
94 * @priv: private data, opaque to list_sort(), passed to @cmp
95 * @head: the list to sort
96 * @cmp: the elements comparison function
98 * This function implements "merge sort", which has O(nlog(n))
101 * The comparison function @cmp must return a negative value if @a
102 * should sort before @b, and a positive value if @a should sort after
103 * @b. If @a and @b are equivalent, and their original relative
104 * ordering is to be preserved, @cmp must return 0.
106 void list_sort(void *priv, struct list_head *head,
107 int (*cmp)(void *priv, struct list_head *a,
108 struct list_head *b))
110 struct list_head *part[MAX_LIST_LENGTH_BITS+1]; /* sorted partial lists
111 -- last slot is a sentinel */
112 int lev; /* index into part[] */
114 struct list_head *list;
116 if (list_empty(head))
119 memset(part, 0, sizeof(part));
121 head->prev->next = NULL;
125 struct list_head *cur = list;
129 for (lev = 0; part[lev]; lev++) {
130 cur = merge(priv, cmp, part[lev], cur);
134 if (unlikely(lev >= ARRAY_SIZE(part)-1)) {
135 printk_once(KERN_DEBUG "list passed to"
136 " list_sort() too long for"
145 for (lev = 0; lev < max_lev; lev++)
147 list = merge(priv, cmp, part[lev], list);
149 merge_and_restore_back_links(priv, cmp, head, part[max_lev], list);
151 EXPORT_SYMBOL(list_sort);
153 #ifdef CONFIG_TEST_LIST_SORT
155 #include <linux/random.h>
158 * The pattern of set bits in the list length determines which cases
159 * are hit in list_sort().
161 #define TEST_LIST_LEN (512+128+2) /* not including head */
163 #define TEST_POISON1 0xDEADBEEF
164 #define TEST_POISON2 0xA324354C
167 unsigned int poison1;
168 struct list_head list;
169 unsigned int poison2;
174 /* Array, containing pointers to all elements in the test list */
175 static struct debug_el **elts __initdata;
177 static int __init check(struct debug_el *ela, struct debug_el *elb)
179 if (ela->serial >= TEST_LIST_LEN) {
180 printk(KERN_ERR "list_sort_test: error: incorrect serial %d\n",
184 if (elb->serial >= TEST_LIST_LEN) {
185 printk(KERN_ERR "list_sort_test: error: incorrect serial %d\n",
189 if (elts[ela->serial] != ela || elts[elb->serial] != elb) {
190 printk(KERN_ERR "list_sort_test: error: phantom element\n");
193 if (ela->poison1 != TEST_POISON1 || ela->poison2 != TEST_POISON2) {
194 printk(KERN_ERR "list_sort_test: error: bad poison: %#x/%#x\n",
195 ela->poison1, ela->poison2);
198 if (elb->poison1 != TEST_POISON1 || elb->poison2 != TEST_POISON2) {
199 printk(KERN_ERR "list_sort_test: error: bad poison: %#x/%#x\n",
200 elb->poison1, elb->poison2);
206 static int __init cmp(void *priv, struct list_head *a, struct list_head *b)
208 struct debug_el *ela, *elb;
210 ela = container_of(a, struct debug_el, list);
211 elb = container_of(b, struct debug_el, list);
214 return ela->value - elb->value;
217 static int __init list_sort_test(void)
219 int i, count = 1, err = -EINVAL;
221 struct list_head *cur, *tmp;
224 printk(KERN_DEBUG "list_sort_test: start testing list_sort()\n");
226 elts = kmalloc(sizeof(void *) * TEST_LIST_LEN, GFP_KERNEL);
228 printk(KERN_ERR "list_sort_test: error: cannot allocate "
233 for (i = 0; i < TEST_LIST_LEN; i++) {
234 el = kmalloc(sizeof(*el), GFP_KERNEL);
236 printk(KERN_ERR "list_sort_test: error: cannot "
237 "allocate memory\n");
240 /* force some equivalencies */
241 el->value = prandom_u32() % (TEST_LIST_LEN / 3);
243 el->poison1 = TEST_POISON1;
244 el->poison2 = TEST_POISON2;
246 list_add_tail(&el->list, &head);
249 list_sort(NULL, &head, cmp);
251 for (cur = head.next; cur->next != &head; cur = cur->next) {
252 struct debug_el *el1;
255 if (cur->next->prev != cur) {
256 printk(KERN_ERR "list_sort_test: error: list is "
261 cmp_result = cmp(NULL, cur, cur->next);
262 if (cmp_result > 0) {
263 printk(KERN_ERR "list_sort_test: error: list is not "
268 el = container_of(cur, struct debug_el, list);
269 el1 = container_of(cur->next, struct debug_el, list);
270 if (cmp_result == 0 && el->serial >= el1->serial) {
271 printk(KERN_ERR "list_sort_test: error: order of "
272 "equivalent elements not preserved\n");
276 if (check(el, el1)) {
277 printk(KERN_ERR "list_sort_test: error: element check "
284 if (count != TEST_LIST_LEN) {
285 printk(KERN_ERR "list_sort_test: error: bad list length %d",
293 list_for_each_safe(cur, tmp, &head) {
295 kfree(container_of(cur, struct debug_el, list));
299 module_init(list_sort_test);
300 #endif /* CONFIG_TEST_LIST_SORT */