annotate libbacktrace/sort.c @ 158:494b0b89df80 default tip

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