forked from ianlancetaylor/cgosymbolizer
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsort.c
More file actions
200 lines (169 loc) · 5.54 KB
/
Copy pathsort.c
File metadata and controls
200 lines (169 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* sort.c -- Sort without allocating memory
Copyright (C) 2012-2024 Free Software Foundation, Inc.
Written by Ian Lance Taylor, Google.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
(1) Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
(2) Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
(3) The name of the author may not be used to
endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE. */
#include "config.h"
#include <stddef.h>
#include <string.h>
#include <sys/types.h>
#include "backtrace.h"
#include "internal.h"
/* The GNU glibc version of qsort allocates memory, which we must not
do if we are invoked by a signal handler. So provide our own
sort.
Quicksort alone has no worst-case bound, and real symbol tables do
reach it: on one 300 MB Go binary the partition work is 122 times
n*log2(n), which is twelve seconds. Cap the recursion depth and
finish with an in-place heapsort past that, bounding the worst case
at O(n log n) for any input. Heapsort allocates nothing, so the
reason this file exists is preserved. */
static void
swap (char *a, char *b, size_t size)
{
size_t i;
/* Swap a machine word at a time when the element size permits. The
byte loop below cannot be widened by the compiler, as size is a
runtime value and the pointers are char *. memcpy rather than a
cast, so that no aliasing assumption is introduced; the compiler
emits a plain load and store for each word. */
if (size % sizeof (uintptr_t) == 0)
{
size_t nwords = size / sizeof (uintptr_t);
for (i = 0; i < nwords; i++)
{
uintptr_t ta, tb;
memcpy (&ta, a + i * sizeof ta, sizeof ta);
memcpy (&tb, b + i * sizeof tb, sizeof tb);
memcpy (a + i * sizeof tb, &tb, sizeof tb);
memcpy (b + i * sizeof ta, &ta, sizeof ta);
}
return;
}
for (i = 0; i < size; i++, a++, b++)
{
char t;
t = *a;
*a = *b;
*b = t;
}
}
/* Sift the element at index I down into the heap rooted at BASE. */
static void
sift_down (char *base, size_t i, size_t count, size_t size,
int (*compar) (const void *, const void *))
{
for (;;)
{
size_t child = 2 * i + 1;
if (child >= count)
break;
if (child + 1 < count
&& (*compar) (base + child * size, base + (child + 1) * size) < 0)
++child;
if ((*compar) (base + i * size, base + child * size) >= 0)
break;
swap (base + i * size, base + child * size, size);
i = child;
}
}
/* In-place heapsort, used once the quicksort recursion gets too deep.
Allocates nothing. */
static void
heap_sort (char *base, size_t count, size_t size,
int (*compar) (const void *, const void *))
{
size_t i;
if (count < 2)
return;
for (i = count / 2; i > 0; i--)
sift_down (base, i - 1, count, size, compar);
for (i = count - 1; i > 0; i--)
{
swap (base, base + i * size, size);
sift_down (base, 0, i, size, compar);
}
}
static void
qsort_limited (char *base, size_t count, size_t size,
int (*compar) (const void *, const void *), unsigned depth)
{
size_t i;
size_t mid;
tail_recurse:
if (count < 2)
return;
if (depth == 0)
{
heap_sort (base, count, size, compar);
return;
}
--depth;
/* The symbol table and DWARF tables, which is all we use this
routine for, tend to be roughly sorted. Pick the middle element
in the array as our pivot point, so that we are more likely to
cut the array in half for each recursion step. */
swap (base, base + (count / 2) * size, size);
mid = 0;
for (i = 1; i < count; i++)
{
if ((*compar) (base, base + i * size) > 0)
{
++mid;
if (i != mid)
swap (base + mid * size, base + i * size, size);
}
}
if (mid > 0)
swap (base, base + mid * size, size);
/* Recurse with the smaller array, loop with the larger one. That
ensures that our maximum stack depth is log count. */
if (2 * mid < count)
{
qsort_limited (base, mid, size, compar, depth);
base += (mid + 1) * size;
count -= mid + 1;
goto tail_recurse;
}
else
{
qsort_limited (base + (mid + 1) * size, count - (mid + 1),
size, compar, depth);
count = mid;
goto tail_recurse;
}
}
void
backtrace_qsort (void *basearg, size_t count, size_t size,
int (*compar) (const void *, const void *))
{
unsigned depth = 0;
size_t c = count;
while (c > 1)
{
++depth;
c >>= 1;
}
qsort_limited ((char *) basearg, count, size, compar, 2 * depth);
}