sort.c 2.9 KB

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