sort.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* sort.c -- Sort without allocating memory
  2. Copyright (C) 2012-2024 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Google.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. (1) Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. (2) Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. (3) The name of the author may not be used to
  14. endorse or promote products derived from this software without
  15. specific prior written permission.
  16. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  20. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  23. HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  24. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  25. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. POSSIBILITY OF SUCH DAMAGE. */
  27. #include "config.h"
  28. #include <stddef.h>
  29. #include <sys/types.h>
  30. #include "backtrace.h"
  31. #include "internal.h"
  32. /* The GNU glibc version of qsort allocates memory, which we must not
  33. do if we are invoked by a signal handler. So provide our own
  34. sort. */
  35. static void
  36. swap (char *a, char *b, size_t size)
  37. {
  38. size_t i;
  39. for (i = 0; i < size; i++, a++, b++)
  40. {
  41. char t;
  42. t = *a;
  43. *a = *b;
  44. *b = t;
  45. }
  46. }
  47. void
  48. backtrace_qsort (void *basearg, size_t count, size_t size,
  49. int (*compar) (const void *, const void *))
  50. {
  51. char *base = (char *) basearg;
  52. size_t i;
  53. size_t mid;
  54. tail_recurse:
  55. if (count < 2)
  56. return;
  57. /* The symbol table and DWARF tables, which is all we use this
  58. routine for, tend to be roughly sorted. Pick the middle element
  59. in the array as our pivot point, so that we are more likely to
  60. cut the array in half for each recursion step. */
  61. swap (base, base + (count / 2) * size, size);
  62. mid = 0;
  63. for (i = 1; i < count; i++)
  64. {
  65. if ((*compar) (base, base + i * size) > 0)
  66. {
  67. ++mid;
  68. if (i != mid)
  69. swap (base + mid * size, base + i * size, size);
  70. }
  71. }
  72. if (mid > 0)
  73. swap (base, base + mid * size, size);
  74. /* Recurse with the smaller array, loop with the larger one. That
  75. ensures that our maximum stack depth is log count. */
  76. if (2 * mid < count)
  77. {
  78. backtrace_qsort (base, mid, size, compar);
  79. base += (mid + 1) * size;
  80. count -= mid + 1;
  81. goto tail_recurse;
  82. }
  83. else
  84. {
  85. backtrace_qsort (base + (mid + 1) * size, count - (mid + 1),
  86. size, compar);
  87. count = mid;
  88. goto tail_recurse;
  89. }
  90. }