summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorglebius <glebius@FreeBSD.org>2004-07-15 23:58:23 +0000
committerglebius <glebius@FreeBSD.org>2004-07-15 23:58:23 +0000
commit7597972ed77af33943e7cf02bffe395e692f99ba (patch)
tree6315345dc1641560e640bb10f261ca3dd93fee9f
parentf24213beb7fc2f5c6b8ef6f5dbf14a6d29bc4269 (diff)
downloadFreeBSD-src-7597972ed77af33943e7cf02bffe395e692f99ba.zip
FreeBSD-src-7597972ed77af33943e7cf02bffe395e692f99ba.tar.gz
Copy qsort_r(3) from libc to libkern.
Reviewed by: phk Approved by: julian (mentor)
-rw-r--r--sys/conf/files1
-rw-r--r--sys/libkern/qsort.c65
-rw-r--r--sys/libkern/qsort_r.c8
-rw-r--r--sys/sys/libkern.h2
4 files changed, 52 insertions, 24 deletions
diff --git a/sys/conf/files b/sys/conf/files
index aaf4274..0454d68 100644
--- a/sys/conf/files
+++ b/sys/conf/files
@@ -1208,6 +1208,7 @@ libkern/index.c standard
libkern/inet_ntoa.c standard
libkern/mcount.c optional profiling-routine
libkern/qsort.c standard
+libkern/qsort_r.c standard
libkern/fnmatch.c standard
libkern/random.c standard
libkern/rindex.c standard
diff --git a/sys/libkern/qsort.c b/sys/libkern/qsort.c
index fbd4aa6..bb0baee 100644
--- a/sys/libkern/qsort.c
+++ b/sys/libkern/qsort.c
@@ -30,10 +30,15 @@
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
+#include <sys/param.h>
#include <sys/libkern.h>
-typedef int cmp_t(const void *, const void *);
-static __inline char *med3(char *, char *, char *, cmp_t *);
+#ifdef I_AM_QSORT_R
+typedef int cmp_t(void *, const void *, const void *);
+#else
+typedef int cmp_t(const void *, const void *);
+#endif
+static __inline char *med3(char *, char *, char *, cmp_t *, void *);
static __inline void swapfunc(char *, char *, int, int);
#define min(a, b) (a) < (b) ? (a) : (b)
@@ -56,9 +61,7 @@ static __inline void swapfunc(char *, char *, int, int);
es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
static __inline void
-swapfunc(a, b, n, swaptype)
- char *a, *b;
- int n, swaptype;
+swapfunc(char *a, char *b, int n, int swaptype)
{
if(swaptype <= 1)
swapcode(long, a, b, n)
@@ -76,21 +79,32 @@ swapfunc(a, b, n, swaptype)
#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
+#ifdef I_AM_QSORT_R
+#define CMP(t, x, y) (cmp((t), (x), (y)))
+#else
+#define CMP(t, x, y) (cmp((x), (y)))
+#endif
+
static __inline char *
-med3(a, b, c, cmp)
- char *a, *b, *c;
- cmp_t *cmp;
+med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk
+#ifndef I_AM_QSORT_R
+__unused
+#endif
+)
{
- return cmp(a, b) < 0 ?
- (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
- :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
+ return CMP(thunk, a, b) < 0 ?
+ (CMP(thunk, b, c) < 0 ? b : (CMP(thunk, a, c) < 0 ? c : a ))
+ :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c ));
}
+#ifdef I_AM_QSORT_R
+void
+qsort_r(void *a, size_t n, size_t es, void *thunk, cmp_t *cmp)
+#else
+#define thunk NULL
void
-qsort(a, n, es, cmp)
- void *a;
- size_t n, es;
- cmp_t *cmp;
+qsort(void *a, size_t n, size_t es, cmp_t *cmp)
+#endif
{
char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
int d, r, swaptype, swap_cnt;
@@ -99,7 +113,7 @@ loop: SWAPINIT(a, es);
swap_cnt = 0;
if (n < 7) {
for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
- for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0;
+ for (pl = pm; pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
pl -= es)
swap(pl, pl - es);
return;
@@ -110,18 +124,18 @@ loop: SWAPINIT(a, es);
pn = (char *)a + (n - 1) * es;
if (n > 40) {
d = (n / 8) * es;
- pl = med3(pl, pl + d, pl + 2 * d, cmp);
- pm = med3(pm - d, pm, pm + d, cmp);
- pn = med3(pn - 2 * d, pn - d, pn, cmp);
+ pl = med3(pl, pl + d, pl + 2 * d, cmp, thunk);
+ pm = med3(pm - d, pm, pm + d, cmp, thunk);
+ pn = med3(pn - 2 * d, pn - d, pn, cmp, thunk);
}
- pm = med3(pl, pm, pn, cmp);
+ pm = med3(pl, pm, pn, cmp, thunk);
}
swap(a, pm);
pa = pb = (char *)a + es;
pc = pd = (char *)a + (n - 1) * es;
for (;;) {
- while (pb <= pc && (r = cmp(pb, a)) <= 0) {
+ while (pb <= pc && (r = CMP(thunk, pb, a)) <= 0) {
if (r == 0) {
swap_cnt = 1;
swap(pa, pb);
@@ -129,7 +143,7 @@ loop: SWAPINIT(a, es);
}
pb += es;
}
- while (pb <= pc && (r = cmp(pc, a)) >= 0) {
+ while (pb <= pc && (r = CMP(thunk, pc, a)) >= 0) {
if (r == 0) {
swap_cnt = 1;
swap(pc, pd);
@@ -146,7 +160,7 @@ loop: SWAPINIT(a, es);
}
if (swap_cnt == 0) { /* Switch to insertion sort */
for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
- for (pl = pm; pl > (char *)a && cmp(pl - es, pl) > 0;
+ for (pl = pm; pl > (char *)a && CMP(thunk, pl - es, pl) > 0;
pl -= es)
swap(pl, pl - es);
return;
@@ -158,12 +172,15 @@ loop: SWAPINIT(a, es);
r = min(pd - pc, pn - pd - es);
vecswap(pb, pn - r, r);
if ((r = pb - pa) > es)
+#ifdef I_AM_QSORT_R
+ qsort_r(a, r / es, es, thunk, cmp);
+#else
qsort(a, r / es, es, cmp);
+#endif
if ((r = pd - pc) > es) {
/* Iterate rather than recurse to save stack space */
a = pn - r;
n = r / es;
goto loop;
}
-/* qsort(pn - r, r / es, es, cmp);*/
}
diff --git a/sys/libkern/qsort_r.c b/sys/libkern/qsort_r.c
new file mode 100644
index 0000000..386d1cf
--- /dev/null
+++ b/sys/libkern/qsort_r.c
@@ -0,0 +1,8 @@
+/*
+ * This file is in the public domain. Originally written by Garrett
+ * A. Wollman.
+ *
+ * $FreeBSD$
+ */
+#define I_AM_QSORT_R
+#include "libkern/qsort.c"
diff --git a/sys/sys/libkern.h b/sys/sys/libkern.h
index 97c56cf..70d19a9 100644
--- a/sys/sys/libkern.h
+++ b/sys/sys/libkern.h
@@ -86,6 +86,8 @@ int fnmatch(const char *, const char *, int);
int locc(int, char *, u_int);
void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
+void qsort_r(void *base, size_t nmemb, size_t size, void *thunk,
+ int (*compar)(void *, const void *, const void *));
u_long random(void);
char *index(const char *, int);
char *rindex(const char *, int);
OpenPOWER on IntegriCloud