From 9820b03db6561744f53216f079528dad8dd55845 Mon Sep 17 00:00:00 2001 From: Michael Schroeder Date: Tue, 15 Feb 2011 12:20:34 +0100 Subject: [PATCH] - rename to USE_OWN_QSORT, add bsd qsort_r helper --- package/libsatsolver.spec.in | 2 +- src/qsort_r.c | 6 +++--- src/util.c | 37 +++++++++++++++++++++++++++++++------ 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/package/libsatsolver.spec.in b/package/libsatsolver.spec.in index 7b81049..627943b 100644 --- a/package/libsatsolver.spec.in +++ b/package/libsatsolver.spec.in @@ -111,7 +111,7 @@ CMAKE_FLAGS="-DFEDORA=1" %endif # Where does RHEL provide qsort_r ? %if 0%{?rhel_version} > 0 || 0%{?centos_version} > 0 -export CFLAGS="$RPM_OPT_FLAGS -DHAVE_OWN_QSORT=1" +export CFLAGS="$RPM_OPT_FLAGS -DUSE_OWN_QSORT=1" %endif cmake $CMAKE_FLAGS \ diff --git a/src/qsort_r.c b/src/qsort_r.c index ef46d7f..7911ec5 100644 --- a/src/qsort_r.c +++ b/src/qsort_r.c @@ -95,8 +95,8 @@ med3(char *a, char *b, char *c, cmp_t *cmp, void *thunk) :(CMP(thunk, b, c) > 0 ? b : (CMP(thunk, a, c) < 0 ? a : c )); } -static void -qsort_r(void *a, size_t n, size_t es, cmp_t *cmp, void *thunk) +void +sat_sort(void *a, size_t n, size_t es, cmp_t *cmp, void *thunk) { char *pa, *pb, *pc, *pd, *pl, *pm, *pn; size_t d, r; @@ -168,7 +168,7 @@ loop: SWAPINIT(a, es); r = min(pd - pc, pn - pd - es); vecswap(pb, pn - r, r); if ((r = pb - pa) > es) - qsort_r(a, r / es, es, cmp, thunk); + sat_sort(a, r / es, es, cmp, thunk); if ((r = pd - pc) > es) { /* Iterate rather than recurse to save stack space */ a = pn - r; diff --git a/src/util.c b/src/util.c index bfa18e3..f728e92 100644 --- a/src/util.c +++ b/src/util.c @@ -98,30 +98,55 @@ sat_timems(unsigned int subtract) return r - subtract; } -#ifdef HAVE_OWN_QSORT +#ifdef USE_OWN_QSORT #include "qsort_r.c" -#endif +#else /* bsd's qsort_r has different arguments, so we define our own version in case we need to do some clever mapping see also: http://sources.redhat.com/ml/libc-alpha/2008-12/msg00003.html */ +#if defined(__GLIBC__) + void sat_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard) { -#if defined(__GLIBC__) -# if __GLIBC_PREREQ(2, 8) || defined(HAVE_OWN_QSORT) +# if __GLIBC_PREREQ(2, 8) qsort_r(base, nmemb, size, compar, compard); # else /* backported for SLE10-SP2 */ __qsort_r(base, nmemb, size, compar, compard); # endif +} + #else -# error please add correct qsort_r call here, note different ordering on BSD -#endif + +struct sat_sort_data { + int (*compar)(const void *, const void *, void *); + void *compard; +}; + +static int +sat_sort_helper(void *compard, const void *a, const void *b) +{ + struct sat_sort_data *d = compard; + return (*d->compar)(a, b, d->compard); } +void +sat_sort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *compard) +{ + struct sat_sort_data d; + d.compar = compar; + d.compard = compard; + qsort_r(base, nmemb, size, &d, sat_sort_helper); +} + +#endif + +#endif /* USE_OWN_QSORT */ + char * sat_dupjoin(const char *str1, const char *str2, const char *str3) { -- 2.7.4