sort.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * A fast, small, non-recursive O(nlog n) sort for the Linux kernel
  3. *
  4. * Jan 23 2005 Matt Mackall <mpm@selenic.com>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/sort.h>
  9. #include <linux/slab.h>
  10. static void u32_swap(void *a, void *b, int size)
  11. {
  12. u32 t = *(u32 *)a;
  13. *(u32 *)a = *(u32 *)b;
  14. *(u32 *)b = t;
  15. }
  16. static void generic_swap(void *a, void *b, int size)
  17. {
  18. char t;
  19. do {
  20. t = *(char *)a;
  21. *(char *)a++ = *(char *)b;
  22. *(char *)b++ = t;
  23. } while (--size > 0);
  24. }
  25. /**
  26. * sort - sort an array of elements
  27. * @base: pointer to data to sort
  28. * @num: number of elements
  29. * @size: size of each element
  30. * @cmp_func: pointer to comparison function
  31. * @swap_func: pointer to swap function or NULL
  32. *
  33. * This function does a heapsort on the given array. You may provide a
  34. * swap_func function optimized to your element type.
  35. *
  36. * Sorting time is O(n log n) both on average and worst-case. While
  37. * qsort is about 20% faster on average, it suffers from exploitable
  38. * O(n*n) worst-case behavior and extra memory requirements that make
  39. * it less suitable for kernel use.
  40. */
  41. void sort(void *base, size_t num, size_t size,
  42. int (*cmp_func)(const void *, const void *),
  43. void (*swap_func)(void *, void *, int size))
  44. {
  45. /* pre-scale counters for performance */
  46. int i = (num/2 - 1) * size, n = num * size, c, r;
  47. if (!swap_func)
  48. swap_func = (size == 4 ? u32_swap : generic_swap);
  49. /* heapify */
  50. for ( ; i >= 0; i -= size) {
  51. for (r = i; r * 2 + size < n; r = c) {
  52. c = r * 2 + size;
  53. if (c < n - size &&
  54. cmp_func(base + c, base + c + size) < 0)
  55. c += size;
  56. if (cmp_func(base + r, base + c) >= 0)
  57. break;
  58. swap_func(base + r, base + c, size);
  59. }
  60. }
  61. /* sort */
  62. for (i = n - size; i > 0; i -= size) {
  63. swap_func(base, base + i, size);
  64. for (r = 0; r * 2 + size < i; r = c) {
  65. c = r * 2 + size;
  66. if (c < i - size &&
  67. cmp_func(base + c, base + c + size) < 0)
  68. c += size;
  69. if (cmp_func(base + r, base + c) >= 0)
  70. break;
  71. swap_func(base + r, base + c, size);
  72. }
  73. }
  74. }
  75. EXPORT_SYMBOL(sort);
  76. #if 0
  77. /* a simple boot-time regression test */
  78. int cmpint(const void *a, const void *b)
  79. {
  80. return *(int *)a - *(int *)b;
  81. }
  82. static int sort_test(void)
  83. {
  84. int *a, i, r = 1;
  85. a = kmalloc(1000 * sizeof(int), GFP_KERNEL);
  86. BUG_ON(!a);
  87. printk("testing sort()\n");
  88. for (i = 0; i < 1000; i++) {
  89. r = (r * 725861) % 6599;
  90. a[i] = r;
  91. }
  92. sort(a, 1000, sizeof(int), cmpint, NULL);
  93. for (i = 0; i < 999; i++)
  94. if (a[i] > a[i+1]) {
  95. printk("sort() failed!\n");
  96. break;
  97. }
  98. kfree(a);
  99. return 0;
  100. }
  101. module_init(sort_test);
  102. #endif