Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions sort.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ POSSIBILITY OF SUCH DAMAGE. */

#include "config.h"

#include <stdlib.h>
#include <stddef.h>
#include <sys/types.h>

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

static void
swap (char *a, char *b, size_t size)
swap (uintptr_t *a, uintptr_t *b, size_t size)
{
size_t i;

for (i = 0; i < size; i++, a++, b++)
{
char t;
uintptr_t t;

t = *a;
*a = *b;
*b = t;
}
}

void
backtrace_qsort (void *basearg, size_t count, size_t size,
static void
backtrace_qsort_impl (uintptr_t *base, size_t count, size_t size,
int (*compar) (const void *, const void *))
{
char *base = (char *) basearg;
size_t i;
size_t mid;

Expand Down Expand Up @@ -93,16 +93,29 @@ backtrace_qsort (void *basearg, size_t count, size_t size,
ensures that our maximum stack depth is log count. */
if (2 * mid < count)
{
backtrace_qsort (base, mid, size, compar);
backtrace_qsort_impl (base, mid, size, compar);
base += (mid + 1) * size;
count -= mid + 1;
goto tail_recurse;
}
else
{
backtrace_qsort (base + (mid + 1) * size, count - (mid + 1),
backtrace_qsort_impl (base + (mid + 1) * size, count - (mid + 1),
size, compar);
count = mid;
goto tail_recurse;
}
}

void
backtrace_qsort (void *basearg, size_t count, size_t size,
int (*compar) (const void *, const void *))
{
/* both base pointer and size should be sizeof(uintptr_t) aligned */
if (size % sizeof(uintptr_t) != 0 ||
(uintptr_t) basearg % sizeof(uintptr_t) != 0)
abort();

backtrace_qsort_impl ((uintptr_t *)(char *) basearg, count,
size / sizeof (void *), compar);
}
20 changes: 10 additions & 10 deletions stest.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ POSSIBILITY OF SUCH DAMAGE. */
struct test
{
size_t count;
int input[MAX];
int output[MAX];
uintptr_t input[MAX];
uintptr_t output[MAX];
};

static struct test tests[] =
Expand Down Expand Up @@ -103,31 +103,31 @@ static struct test tests[] =
static int
compare (const void *a, const void *b)
{
const int *ai = (const int *) a;
const int *bi = (const int *) b;
const uintptr_t *ai = (const uintptr_t *) a;
const uintptr_t *bi = (const uintptr_t *) b;

return *ai - *bi;
return *ai < *bi ? -1 : *ai > *bi;
}

int
main (int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED)
{
int failures;
size_t i;
int a[MAX];
uintptr_t a[MAX];

failures = 0;
for (i = 0; i < sizeof tests / sizeof tests[0]; i++)
{
memcpy (a, tests[i].input, tests[i].count * sizeof (int));
backtrace_qsort (a, tests[i].count, sizeof (int), compare);
if (memcmp (a, tests[i].output, tests[i].count * sizeof (int)) != 0)
memcpy (a, tests[i].input, tests[i].count * sizeof (uintptr_t));
backtrace_qsort (a, tests[i].count, sizeof (uintptr_t), compare);
if (memcmp (a, tests[i].output, tests[i].count * sizeof (uintptr_t)) != 0)
{
size_t j;

fprintf (stderr, "test %d failed:", (int) i);
for (j = 0; j < tests[i].count; j++)
fprintf (stderr, " %d", a[j]);
fprintf (stderr, " %d", (int)a[j]);
fprintf (stderr, "\n");
++failures;
}
Expand Down