Skip to content

Commit 6cea960

Browse files
committed
Apply upstream PR ianlancetaylor#102
1 parent d615358 commit 6cea960

2 files changed

Lines changed: 30 additions & 17 deletions

File tree

sort.c

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ POSSIBILITY OF SUCH DAMAGE. */
3232

3333
#include "config.h"
3434

35+
#include <stdlib.h>
3536
#include <stddef.h>
3637
#include <sys/types.h>
3738

@@ -43,25 +44,24 @@ POSSIBILITY OF SUCH DAMAGE. */
4344
sort. */
4445

4546
static void
46-
swap (char *a, char *b, size_t size)
47+
swap (uintptr_t *a, uintptr_t *b, size_t size)
4748
{
4849
size_t i;
4950

5051
for (i = 0; i < size; i++, a++, b++)
5152
{
52-
char t;
53+
uintptr_t t;
5354

5455
t = *a;
5556
*a = *b;
5657
*b = t;
5758
}
5859
}
5960

60-
void
61-
backtrace_qsort (void *basearg, size_t count, size_t size,
61+
static void
62+
backtrace_qsort_impl (uintptr_t *base, size_t count, size_t size,
6263
int (*compar) (const void *, const void *))
6364
{
64-
char *base = (char *) basearg;
6565
size_t i;
6666
size_t mid;
6767

@@ -93,16 +93,29 @@ backtrace_qsort (void *basearg, size_t count, size_t size,
9393
ensures that our maximum stack depth is log count. */
9494
if (2 * mid < count)
9595
{
96-
backtrace_qsort (base, mid, size, compar);
96+
backtrace_qsort_impl (base, mid, size, compar);
9797
base += (mid + 1) * size;
9898
count -= mid + 1;
9999
goto tail_recurse;
100100
}
101101
else
102102
{
103-
backtrace_qsort (base + (mid + 1) * size, count - (mid + 1),
103+
backtrace_qsort_impl (base + (mid + 1) * size, count - (mid + 1),
104104
size, compar);
105105
count = mid;
106106
goto tail_recurse;
107107
}
108108
}
109+
110+
void
111+
backtrace_qsort (void *basearg, size_t count, size_t size,
112+
int (*compar) (const void *, const void *))
113+
{
114+
/* both base pointer and size should be sizeof(uintptr_t) aligned */
115+
if (size % sizeof(uintptr_t) != 0 ||
116+
(uintptr_t) basearg % sizeof(uintptr_t) != 0)
117+
abort();
118+
119+
backtrace_qsort_impl ((uintptr_t *)(char *) basearg, count,
120+
size / sizeof (void *), compar);
121+
}

stest.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ POSSIBILITY OF SUCH DAMAGE. */
4747
struct test
4848
{
4949
size_t count;
50-
int input[MAX];
51-
int output[MAX];
50+
uintptr_t input[MAX];
51+
uintptr_t output[MAX];
5252
};
5353

5454
static struct test tests[] =
@@ -103,31 +103,31 @@ static struct test tests[] =
103103
static int
104104
compare (const void *a, const void *b)
105105
{
106-
const int *ai = (const int *) a;
107-
const int *bi = (const int *) b;
106+
const uintptr_t *ai = (const uintptr_t *) a;
107+
const uintptr_t *bi = (const uintptr_t *) b;
108108

109-
return *ai - *bi;
109+
return *ai < *bi ? -1 : *ai > *bi;
110110
}
111111

112112
int
113113
main (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
114114
{
115115
int failures;
116116
size_t i;
117-
int a[MAX];
117+
uintptr_t a[MAX];
118118

119119
failures = 0;
120120
for (i = 0; i < sizeof tests / sizeof tests[0]; i++)
121121
{
122-
memcpy (a, tests[i].input, tests[i].count * sizeof (int));
123-
backtrace_qsort (a, tests[i].count, sizeof (int), compare);
124-
if (memcmp (a, tests[i].output, tests[i].count * sizeof (int)) != 0)
122+
memcpy (a, tests[i].input, tests[i].count * sizeof (uintptr_t));
123+
backtrace_qsort (a, tests[i].count, sizeof (uintptr_t), compare);
124+
if (memcmp (a, tests[i].output, tests[i].count * sizeof (uintptr_t)) != 0)
125125
{
126126
size_t j;
127127

128128
fprintf (stderr, "test %d failed:", (int) i);
129129
for (j = 0; j < tests[i].count; j++)
130-
fprintf (stderr, " %d", a[j]);
130+
fprintf (stderr, " %d", (int)a[j]);
131131
fprintf (stderr, "\n");
132132
++failures;
133133
}

0 commit comments

Comments
 (0)