list_sort.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #include <linux/kernel.h>
  2. #include <linux/module.h>
  3. #include <linux/list_sort.h>
  4. #include <linux/slab.h>
  5. #include <linux/list.h>
  6. #define MAX_LIST_LENGTH_BITS 20
  7. /*
  8. * Returns a list organized in an intermediate format suited
  9. * to chaining of merge() calls: null-terminated, no reserved or
  10. * sentinel head node, "prev" links not maintained.
  11. */
  12. static struct list_head *merge(void *priv,
  13. int (*cmp)(void *priv, struct list_head *a,
  14. struct list_head *b),
  15. struct list_head *a, struct list_head *b)
  16. {
  17. struct list_head head, *tail = &head;
  18. while (a && b) {
  19. /* if equal, take 'a' -- important for sort stability */
  20. if ((*cmp)(priv, a, b) <= 0) {
  21. tail->next = a;
  22. a = a->next;
  23. } else {
  24. tail->next = b;
  25. b = b->next;
  26. }
  27. tail = tail->next;
  28. }
  29. tail->next = a?:b;
  30. return head.next;
  31. }
  32. /*
  33. * Combine final list merge with restoration of standard doubly-linked
  34. * list structure. This approach duplicates code from merge(), but
  35. * runs faster than the tidier alternatives of either a separate final
  36. * prev-link restoration pass, or maintaining the prev links
  37. * throughout.
  38. */
  39. static void merge_and_restore_back_links(void *priv,
  40. int (*cmp)(void *priv, struct list_head *a,
  41. struct list_head *b),
  42. struct list_head *head,
  43. struct list_head *a, struct list_head *b)
  44. {
  45. struct list_head *tail = head;
  46. while (a && b) {
  47. /* if equal, take 'a' -- important for sort stability */
  48. if ((*cmp)(priv, a, b) <= 0) {
  49. tail->next = a;
  50. a->prev = tail;
  51. a = a->next;
  52. } else {
  53. tail->next = b;
  54. b->prev = tail;
  55. b = b->next;
  56. }
  57. tail = tail->next;
  58. }
  59. tail->next = a ? : b;
  60. do {
  61. /*
  62. * In worst cases this loop may run many iterations.
  63. * Continue callbacks to the client even though no
  64. * element comparison is needed, so the client's cmp()
  65. * routine can invoke cond_resched() periodically.
  66. */
  67. (*cmp)(priv, tail, tail);
  68. tail->next->prev = tail;
  69. tail = tail->next;
  70. } while (tail->next);
  71. tail->next = head;
  72. head->prev = tail;
  73. }
  74. /**
  75. * list_sort - sort a list
  76. * @priv: private data, opaque to list_sort(), passed to @cmp
  77. * @head: the list to sort
  78. * @cmp: the elements comparison function
  79. *
  80. * This function implements "merge sort", which has O(nlog(n))
  81. * complexity.
  82. *
  83. * The comparison function @cmp must return a negative value if @a
  84. * should sort before @b, and a positive value if @a should sort after
  85. * @b. If @a and @b are equivalent, and their original relative
  86. * ordering is to be preserved, @cmp must return 0.
  87. */
  88. void list_sort(void *priv, struct list_head *head,
  89. int (*cmp)(void *priv, struct list_head *a,
  90. struct list_head *b))
  91. {
  92. struct list_head *part[MAX_LIST_LENGTH_BITS+1]; /* sorted partial lists
  93. -- last slot is a sentinel */
  94. int lev; /* index into part[] */
  95. int max_lev = 0;
  96. struct list_head *list;
  97. if (list_empty(head))
  98. return;
  99. memset(part, 0, sizeof(part));
  100. head->prev->next = NULL;
  101. list = head->next;
  102. while (list) {
  103. struct list_head *cur = list;
  104. list = list->next;
  105. cur->next = NULL;
  106. for (lev = 0; part[lev]; lev++) {
  107. cur = merge(priv, cmp, part[lev], cur);
  108. part[lev] = NULL;
  109. }
  110. if (lev > max_lev) {
  111. if (unlikely(lev >= ARRAY_SIZE(part)-1)) {
  112. printk_once(KERN_DEBUG "list passed to"
  113. " list_sort() too long for"
  114. " efficiency\n");
  115. lev--;
  116. }
  117. max_lev = lev;
  118. }
  119. part[lev] = cur;
  120. }
  121. for (lev = 0; lev < max_lev; lev++)
  122. if (part[lev])
  123. list = merge(priv, cmp, part[lev], list);
  124. merge_and_restore_back_links(priv, cmp, head, part[max_lev], list);
  125. }
  126. EXPORT_SYMBOL(list_sort);
  127. #ifdef DEBUG_LIST_SORT
  128. struct debug_el {
  129. struct list_head l_h;
  130. int value;
  131. unsigned serial;
  132. };
  133. static int cmp(void *priv, struct list_head *a, struct list_head *b)
  134. {
  135. return container_of(a, struct debug_el, l_h)->value
  136. - container_of(b, struct debug_el, l_h)->value;
  137. }
  138. /*
  139. * The pattern of set bits in the list length determines which cases
  140. * are hit in list_sort().
  141. */
  142. #define LIST_SORT_TEST_LENGTH (512+128+2) /* not including head */
  143. static int __init list_sort_test(void)
  144. {
  145. int i, r = 1, count;
  146. struct list_head *head = kmalloc(sizeof(*head), GFP_KERNEL);
  147. struct list_head *cur;
  148. printk(KERN_WARNING "testing list_sort()\n");
  149. cur = head;
  150. for (i = 0; i < LIST_SORT_TEST_LENGTH; i++) {
  151. struct debug_el *el = kmalloc(sizeof(*el), GFP_KERNEL);
  152. BUG_ON(!el);
  153. /* force some equivalencies */
  154. el->value = (r = (r * 725861) % 6599) % (LIST_SORT_TEST_LENGTH/3);
  155. el->serial = i;
  156. el->l_h.prev = cur;
  157. cur->next = &el->l_h;
  158. cur = cur->next;
  159. }
  160. head->prev = cur;
  161. list_sort(NULL, head, cmp);
  162. count = 1;
  163. for (cur = head->next; cur->next != head; cur = cur->next) {
  164. struct debug_el *el = container_of(cur, struct debug_el, l_h);
  165. int cmp_result = cmp(NULL, cur, cur->next);
  166. if (cur->next->prev != cur) {
  167. printk(KERN_EMERG "list_sort() returned "
  168. "a corrupted list!\n");
  169. return 1;
  170. } else if (cmp_result > 0) {
  171. printk(KERN_EMERG "list_sort() failed to sort!\n");
  172. return 1;
  173. } else if (cmp_result == 0 &&
  174. el->serial >= container_of(cur->next,
  175. struct debug_el, l_h)->serial) {
  176. printk(KERN_EMERG "list_sort() failed to preserve order"
  177. " of equivalent elements!\n");
  178. return 1;
  179. }
  180. kfree(cur->prev);
  181. count++;
  182. }
  183. kfree(cur);
  184. if (count != LIST_SORT_TEST_LENGTH) {
  185. printk(KERN_EMERG "list_sort() returned list of"
  186. "different length!\n");
  187. return 1;
  188. }
  189. return 0;
  190. }
  191. module_init(list_sort_test);
  192. #endif