1 #include <linux/kernel.h>
2 #include <linux/module.h>
4 #include <linux/slab.h>
5 #include <linux/list.h>
7 #define MAX_LIST_LENGTH_BITS 20
23 if ((*cmp)(priv, a, b) <= 0) {
43 static void merge_and_restore_back_links(
void *priv,
53 if ((*cmp)(priv, a, b) <= 0) {
98 int (*cmp)(
void *priv,
struct list_head *a,
107 if (list_empty(head))
110 memset(part, 0,
sizeof(part));
120 for (lev = 0; part[lev]; lev++) {
121 cur = merge(priv, cmp, part[lev], cur);
127 " list_sort() too long for"
136 for (lev = 0; lev < max_lev; lev++)
138 list = merge(priv, cmp, part[lev], list);
140 merge_and_restore_back_links(priv, cmp, head, part[max_lev], list);
144 #ifdef CONFIG_TEST_LIST_SORT
146 #include <linux/random.h>
152 #define TEST_LIST_LEN (512+128+2)
154 #define TEST_POISON1 0xDEADBEEF
155 #define TEST_POISON2 0xA324354C
158 unsigned int poison1;
160 unsigned int poison2;
168 static int __init check(
struct debug_el *ela,
struct debug_el *elb)
170 if (ela->serial >= TEST_LIST_LEN) {
175 if (elb->serial >= TEST_LIST_LEN) {
180 if (elts[ela->serial] != ela || elts[elb->serial] != elb) {
184 if (ela->poison1 != TEST_POISON1 || ela->poison2 != TEST_POISON2) {
186 ela->poison1, ela->poison2);
189 if (elb->poison1 != TEST_POISON1 || elb->poison2 != TEST_POISON2) {
191 elb->poison1, elb->poison2);
199 struct debug_el *ela, *elb;
205 return ela->value - elb->value;
208 static int __init list_sort_test(
void)
224 for (i = 0; i < TEST_LIST_LEN; i++) {
228 "allocate memory\n");
232 el->value =
random32() % (TEST_LIST_LEN/3);
234 el->poison1 = TEST_POISON1;
235 el->poison2 = TEST_POISON2;
242 for (cur = head.
next; cur->
next != &head; cur = cur->
next) {
243 struct debug_el *el1;
246 if (cur->
next->prev != cur) {
252 cmp_result = cmp(
NULL, cur, cur->
next);
253 if (cmp_result > 0) {
261 if (cmp_result == 0 && el->serial >= el1->serial) {
263 "equivalent elements not preserved\n");
267 if (
check(el, el1)) {
275 if (count != TEST_LIST_LEN) {