summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-08-15 11:44:23 -0400
committerJakob Kaivo <jkk@ung.org>2020-08-15 11:44:23 -0400
commit933040452f1091b86192c08ad343cd5ae50d14d4 (patch)
tree9f53f5ea23c35a6d14835ae0019b064d91b74634 /src
parent05347c1760befb18a2a2396233bcff8c7d3e283e (diff)
do swaps byte-by-byte to remove the need for allocating buffers
Diffstat (limited to 'src')
-rw-r--r--src/stdlib/_qsort.h17
-rw-r--r--src/stdlib/qsort.c13
2 files changed, 10 insertions, 20 deletions
diff --git a/src/stdlib/_qsort.h b/src/stdlib/_qsort.h
index 298e28a1..28d273cd 100644
--- a/src/stdlib/_qsort.h
+++ b/src/stdlib/_qsort.h
@@ -1,19 +1,22 @@
#include <stdlib.h>
-#include <string.h>
struct __qs {
char *base;
size_t size;
int (*compar)(const void *, const void *);
- char buf[64];
- char *swap;
};
-static void __swap(struct __qs *a, size_t i, size_t j)
+static void __swap(struct __qs *a, size_t b, size_t c)
{
- memcpy(a->swap, a->base + (a->size * i), a->size);
- memcpy(a->base + (a->size * i), a->base + (a->size * j), a->size);
- memcpy(a->base + (a->size * j), a->swap, a->size);
+ char *x = a->base + (a->size * b);
+ char *y = a->base + (a->size * c);
+ size_t i;
+
+ for (i = 0; i < a->size; i++) {
+ char tmp = x[i];
+ x[i] = y[i];
+ y[i] = tmp;
+ }
}
static size_t __partition(struct __qs *a, size_t lo, size_t hi)
diff --git a/src/stdlib/qsort.c b/src/stdlib/qsort.c
index f0ccba33..535e8551 100644
--- a/src/stdlib/qsort.c
+++ b/src/stdlib/qsort.c
@@ -9,21 +9,8 @@ void qsort(void * base, size_t nmemb, size_t size, int (*compar)(const void *, c
base,
size,
compar,
- { 0 },
- NULL,
};
-
- if (size <= sizeof(qs.buf)) {
- qs.swap = qs.buf;
- } else {
- qs.swap = malloc(size);
- }
-
__qsort(&qs, 0, nmemb);
-
- if (qs.swap != qs.buf) {
- free(qs.swap);
- }
}
/***